Python开发Discord Bot的clear命令异常问题求助
Python开发Discord Bot的clear命令异常问题求助
各位大佬好,我最近开发了一个简单的Discord机器人,其中的clear命令负责批量删除频道消息,但目前遇到了两个棘手的问题:
- 执行
clear命令后,Discord客户端会弹出**“应用未响应”**的警告,而不是预期显示{len(deleted)} messages have been deleted.的提示 - 同时控制台会抛出以下错误:
discord.app_commands.errors.CommandInvokeError: Command 'clear' raised an exception: NotFound: 404 Not Found (error code: 10062): Unknown interaction
我尝试过用interaction.response.defer()来延迟响应处理,这时候“应用未响应”的警告确实消失了,但新问题来了——命令执行后完全没有任何反馈,既不会告知我删除了多少条消息。之后我又试了两种方法:用interaction.followup.send()发送后续通知,以及用interaction.edit_original_response()编辑初始响应,但这两种操作都没有效果。
我的代码片段如下:
import discord from discord import app_commands bot = discord.Client(intents=discord.Intents.all()) tree = app_commands.CommandTree(bot) @bot.event async def on_ready(): await bot.load_extension("admin") try: synced = await bot.tree.sync() print(f"已同步 {len(synced)} 条命令") except Exception as e: print(f"同步命令出错:{e}") # 以下是admin扩展中的clear命令代码 @tree.command(name="clear", description="批量删除频道消息") async def clear(interaction: discord.Interaction, amount: int): # 删除指定数量的消息(+1是包含命令本身) deleted = await interaction.channel.purge(limit=amount + 1) # 尝试发送删除结果通知 await interaction.response.send_message(f"已删除 {len(deleted)-1} 条消息")
有没有朋友能帮我分析下问题出在哪,或者给个可行的解决思路呀?
备注:内容来源于stack exchange,提问作者Aktuğ İ.
相关产品推荐
相关产品推荐

