你如何删除不和谐机器人的最新消息? - 不和谐.py

Posted

技术标签:

【中文标题】你如何删除不和谐机器人的最新消息? - 不和谐.py【英文标题】:How do you delete the discord bot's most recent message? - Discord.py 【发布时间】:2021-09-26 09:45:03 【问题描述】:

我尝试发出清除命令,这是我的代码:

@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()

但是当我尝试运行该命令时,它会删除消息,但在尝试删除消息时会出现此错误:

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

有什么帮助吗?

【问题讨论】:

【参考方案1】:

你可以试试这个:

@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)

delete_after 在特定秒数后删除 bot 发送的消息。

【讨论】:

你不再需要 (pass_context=True),那已经过时了 @Programmer Ayush 我仍然遇到同样的错误【参考方案2】:

迟到的答案,但我认为这可能会对您有所帮助。使用您的代码,您只需将刚刚发送的消息保存到变量中,然后删除该消息。

@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()

【讨论】:

以上是关于你如何删除不和谐机器人的最新消息? - 不和谐.py的主要内容,如果未能解决你的问题,请参考以下文章

我如何编码不和谐机器人以删除“用户固定消息”消息?

如何删除我的不和谐机器人在特定频道中发送的先前消息?

如何使不和谐机器人将消息发送到不和谐 Node js 的特定频道机器人

创建一个记录所有消息的不和谐机器人

如何在不和谐上发送私人消息[关闭]

你如何让一个不和谐的机器人告诉 ping?