You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

discord.py purge斜杠命令报错Unknown message,后续响应发送失败

问题分析

报错NotFound: 404 Not Found (error code: 10008): Unknown Message的核心原因:

  • 调用await interaction.response.defer()后,Discord会在频道发送一条临时的“Bot is thinking...”消息;
  • 后续执行purge(limit=amount +1)时,这条临时消息被纳入删除范围并被删除;
  • 后续的followup.send()依赖这条临时消息对应的交互会话,会话被删除后触发“未知消息”错误;
  • 错误处理函数误用了前缀命令的异常类(commands模块),斜杠命令的异常属于app_commands模块,导致错误处理逻辑失效。
解决方案

1. 修改Defer参数,避免临时消息被删除

将defer设置为ephemeral=True,临时消息仅对命令执行者可见,不会出现在公共频道消息列表中,自然不会被purge命令删除。

2. 调整Purge的消息数量

斜杠命令的交互不属于普通频道消息,无需额外+1来删除命令消息,直接使用用户指定的amount即可。

3. 修正错误处理的异常类

将commands模块的异常替换为app_commands模块对应的异常,确保错误处理逻辑生效。

修正后的完整代码
from discord import Interaction
from discord.app_commands import tree, CommandInvokeError, MissingPermissions, MissingRequiredArgument, BadArgument

@tree.command(
    name="purge",
    description="Deletes a specified number of messages from the channel."
)
async def purge(interaction: Interaction, amount: int):
    # 使用ephemeral=True,临时消息仅执行者可见,不会被purge删除
    await interaction.response.defer(ephemeral=True)
    try:
        # 直接删除指定数量的消息,无需+1
        deleted = await interaction.channel.purge(limit=amount)
        # 发送ephemeral的后续响应,仅执行者可见
        await interaction.followup.send(f"Purged {len(deleted)} messages.", ephemeral=True)
    except Exception as e:
        await interaction.followup.send(f"An error occurred: {str(e)}", ephemeral=True)

@purge.error
async def purge_error(interaction: Interaction, error):
    try:
        if interaction.response.is_done():
            await interaction.followup.send("An error occurred while executing this command.", ephemeral=True)
        else:
            # 使用app_commands模块的异常类
            if isinstance(error, MissingPermissions):
                await interaction.response.send_message("You don't have permission to use this command.", ephemeral=True)
            elif isinstance(error, MissingRequiredArgument):
                await interaction.response.send_message("Please specify the number of messages to purge.", ephemeral=True)
            elif isinstance(error, BadArgument):
                await interaction.response.send_message("Please provide a valid number.", ephemeral=True)
            elif isinstance(error, CommandInvokeError):
                # 捕获命令执行时的异常
                await interaction.response.send_message(f"Command execution failed: {str(error.original)}", ephemeral=True)
            else:
                await interaction.response.send_message("An error occurred while executing this command.", ephemeral=True)
    except Exception as e:
        print(f"Failed to send error message: {str(e)}")
额外说明
  • 使用ephemeral=True的响应不会被频道内其他成员看到,也不会被purge命令影响,是解决交互会话被删除问题的最优方案;
  • 如果希望响应消息对所有成员可见,可以在followup.send()中去掉ephemeral=True,但必须修改purge的check参数排除临时消息:
    deleted = await interaction.channel.purge(limit=amount, check=lambda m: m.id != interaction.response.message.id)
    
    这种方式需要确保能获取到临时消息的ID,相对ephemeral方案复杂度更高。

内容的提问来源于stack exchange,提问作者Opressive Extenz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 17:38:11