每次我在 Discord 机器人上运行命令“purge”时,在命令运行并删除有限消息后,我总是看到“应用程序没有响应”。我正在使用discord.app_commands(我相信)和树作为我的命令,以便它们可以作为斜杠命令运行。
这是我的进口:
import discord
from discord import app_commands
from discord.ext import commands
from asyncio import sleep
命令如下:
@tree.command(
name="purge",
description="Deletes a specified number of messages from the channel."
)
async def purge(interaction, limit: int):
try:
if interaction.user.guild_permissions.manage_messages:
await interaction.channel.purge(limit=limit + 1)
await sleep(2)
await interaction.response.send_message(f"{limit} messages deleted!", ephemeral=True)
else:
await interaction.response.send_message("You don't have permission to use this command.", ephemeral=True)
except discord.errors.NotFound:
# Handle the case where the interaction is not found
await interaction.response.send_message("An error occurred while processing the command. Please try again later.", ephemeral=True)
except discord.errors.Forbidden:
# Handle the case where the bot doesn't have permission to perform the action
await interaction.response.send_message("I don't have permission to perform this action.", ephemeral=True)
except Exception as e:
# Handle any other exceptions that might occur
await interaction.response.send_message(f"An unexpected error occurred: {str(e)}", ephemeral=True)
仅当我运行清除时才会出现此错误,我没有使用其他命令导致此错误。我使用任何整数的限制块执行了“/purge”,并且在机器人删除特定数量的消息后,我预计它会显示“{limit}条消息已删除!”但相反,它显示“应用程序没有响应”。我尝试让机器人等待 2 秒再发送消息,但也不起作用。
此外,我的服务器控制台的回溯:
2024-04-09 00:22:43 ERROR discord.app_commands.tree Ignoring exception in command 'purge'
Traceback (most recent call last):
File "/home/container/bot_commands.py", line 27, in purge
await interaction.response.send_message(f"{limit} messages deleted!", ephemeral=True)
File "/home/container/.local/lib/python3.12/site-packages/discord/interactions.py", line 801, in send_message
await adapter.create_interaction_response(
File "/home/container/.local/lib/python3.12/site-packages/discord/webhook/async_.py", line 219, in request
raise NotFound(response, data)
discord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/container/.local/lib/python3.12/site-packages/discord/app_commands/commands.py", line 828, in _do_call
return await self._callback(interaction, **params) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/container/bot_commands.py", line 33, in purge
await interaction.response.send_message("An error occurred while processing the command. Please try again later.", ephemeral=True)
File "/home/container/.local/lib/python3.12/site-packages/discord/interactions.py", line 801, in send_message
await adapter.create_interaction_response(
File "/home/container/.local/lib/python3.12/site-packages/discord/webhook/async_.py", line 219, in request
raise NotFound(response, data)
discord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/container/.local/lib/python3.12/site-packages/discord/app_commands/tree.py", line 1248, in _call
await command._invoke_with_namespace(interaction, namespace)
File "/home/container/.local/lib/python3.12/site-packages/discord/app_commands/commands.py", line 853, in _invoke_with_namespace
return await self._do_call(interaction, transformed_values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/container/.local/lib/python3.12/site-packages/discord/app_commands/commands.py", line 846, in _do_call
raise CommandInvokeError(self, e) from e
discord.app_commands.errors.CommandInvokeError: Command 'purge' raised an exception: NotFound: 404 Not Found (error code: 10062): Unknown interaction
您必须在功能开始时使用
interaction.defer()
,因为交互可以在3秒内回复,而清除则需要超过3秒。但使用 interaction.defer()
会将 3 秒限制增加到 15 分钟。
更新代码:
@tree.command(
name="purge",
description="Deletes a specified number of messages from the channel."
)
async def purge(interaction, limit: int):
await interaction.defer() # here
try:
if interaction.user.guild_permissions.manage_messages:
await interaction.channel.purge(limit=limit + 1)
await sleep(2)
await interaction.response.send_message(f"{limit} messages deleted!", ephemeral=True)
else:
await interaction.response.send_message("You don't have permission to use this command.", ephemeral=True)
except discord.errors.NotFound:
# Handle the case where the interaction is not found
await interaction.response.send_message("An error occurred while processing the command. Please try again later.", ephemeral=True)
except discord.errors.Forbidden:
# Handle the case where the bot doesn't have permission to perform the action
await interaction.response.send_message("I don't have permission to perform this action.", ephemeral=True)
except Exception as e:
# Handle any other exceptions that might occur
await interaction.response.send_message(f"An unexpected error occurred: {str(e)}", ephemeral=True)
文档参考: