簡體   English   中英

您如何刪除不和諧機器人的最新消息? - 不和諧.py

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

我試圖做一個清除命令,這是我的代碼:

@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

有什么幫助嗎?

你可以試試這個:

@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 發送的消息。

遲到的答案,但我認為這可能對您有所幫助。 使用您的代碼,您要做的就是將剛剛發送的消息保存到變量中,然后刪除該消息。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM