python - 你如何删除不和谐机器人的最新消息?- 不和谐.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
特定秒数后删除机器人发送的消息。
推荐阅读
- prometheus - 在 Prometheus 警报规则中使用和运算符与布尔类型属性
- .htaccess - 拒绝访问所有文件 接受 index.php 和带有 .htaccess 的域名
- android - 在文本输入布局中更改文本提示样式
- java - 如何将数组数组发送到 DialogFragment?
- ubuntu - 如何在 Apache Superset 中使用 Google 登录
- raspberry-pi - 树莓派 - Qbittorrent 下载到 NAS
- codeigniter - Codeigniter 4 set_cookie 未设置
- go - 在 Golang 中执行 bpftrace shell 命令的奇怪行为?
- javascript - 使用 jQuery 插件向 HTML 行添加类?
- angular - Angular Universal SSR:开发和构建工作,服务不