Aarav Saini
Aarav Saini

Reputation: 13

How do you delete the discord bot's most recent message? - Discord.py

I tried to make a purge command, here is my code:

@bot.command(pass_context=True)
@commands.has_permissions(administrator=True)
async def purge(ctx, limit: int):
  await ctx.channel.purge(limit=limit)
  await ctx.message.delete()
  await ctx.send(f'Deleted {limit} messages. Done by {ctx.author.mention}.')
  await asyncio.sleep(2)
  await bot.delete.message.most_recent()

but when i try to run the command, it deletes the messages but gives this error when it tries to delete the message:

Ignoring exception in command purge:
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 312, in purge
    await ctx.message.delete()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/message.py", line 1023, in delete
    await self._state.http.delete_message(self.channel.id, self.id)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/http.py", line 250, in request
    raise NotFound(r, data)
discord.errors.NotFound: 404 Not Found (error code: 10008): Unknown Message

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NotFound: 404 Not Found (error code: 10008): Unknown Message

Any help?

Upvotes: 0

Views: 2740

Answers (2)

UnderGame
UnderGame

Reputation: 11

Late answer but I think this might help you. Using your code all what you have to do is save the message that you just sent into a variable and then, delete that message.

@bot.command()
@commands.has_permissions(administrator=True)
async def purge(ctx, limit: int):
    await ctx.channel.purge(limit=limit)
    await ctx.message.delete()
    message = await ctx.send(f'Deleted {limit} messages. Done by {ctx.author.mention}.')
    await asyncio.sleep(2)
    await message.delete()

Upvotes: 0

Programmer Ayush
Programmer Ayush

Reputation: 49

You can try this instead:

@bot.command(pass_context=True)
@commands.has_permissions(administrator=True)
async def purge(ctx, limit: int):
    await ctx.channel.purge(limit=limit)
    await ctx.message.delete()
    await ctx.send(f'Deleted {limit} messages. Done by {ctx.author.mention}.', delete_after = 2)

The delete_after deletes the message sent by bot after specific number of seconds.

Upvotes: 1

Related Questions