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

Nextcord on_application_command_error问题:权限错误无法通知用户

解决Nextcord Slash命令权限错误无用户提示问题

核心问题分析

  1. 异常类型不匹配:你使用的commands.MissingPermissions等属于前缀命令的异常类,Slash命令(应用命令)的权限缺失异常是nextcord.ApplicationMissingPermissions(用户无权限)和nextcord.BotApplicationMissingPermissions(机器人无权限),这是你找不到对应属性的根本原因。
  2. 互动响应状态问题:部分错误触发时,互动可能已处于不可直接用response.send_message的状态,强行调用会导致终端报错且用户收不到提示。

修正后的错误处理代码

async def on_application_command_error(interaction: nextcord.Interaction, error: nextcord.DiscordException):
    # 处理Slash命令专属权限异常
    if isinstance(error, nextcord.ApplicationMissingPermissions):
        try:
            await interaction.response.send_message("你没有使用该命令的权限。", ephemeral=True)
        except nextcord.InteractionResponded:
            await interaction.followup.send("你没有使用该命令的权限。", ephemeral=True)
    elif isinstance(error, nextcord.BotApplicationMissingPermissions):
        try:
            await interaction.response.send_message("我没有执行该命令的权限,请检查我的角色权限。", ephemeral=True)
        except nextcord.InteractionResponded:
            await interaction.followup.send("我没有执行该命令的权限,请检查我的角色权限。", ephemeral=True)
    # 处理其他应用命令常见错误
    elif isinstance(error, nextcord.ApplicationCommandNotFound):
        try:
            await interaction.response.send_message("未找到该命令。", ephemeral=True)
        except nextcord.InteractionResponded:
            await interaction.followup.send("未找到该命令。", ephemeral=True)
    elif isinstance(error, nextcord.ApplicationCommandInvokeError):
        try:
            await interaction.response.send_message("执行命令时发生错误。", ephemeral=True)
        except nextcord.InteractionResponded:
            await interaction.followup.send("执行命令时发生错误。", ephemeral=True)
    # 兼容前缀命令的权限异常(如果仍在使用前缀命令)
    elif isinstance(error, commands.MissingPermissions):
        try:
            await interaction.response.send_message("你没有使用该命令的权限。", ephemeral=True)
        except nextcord.InteractionResponded:
            await interaction.followup.send("你没有使用该命令的权限。", ephemeral=True)
    elif isinstance(error, commands.BotMissingPermissions):
        try:
            await interaction.response.send_message("我没有执行该命令的权限,请检查我的角色权限。", ephemeral=True)
        except nextcord.InteractionResponded:
            await interaction.followup.send("我没有执行该命令的权限,请检查我的角色权限。", ephemeral=True)
    # 兜底处理未知错误
    else:
        print(f"未处理的应用命令错误: {error}")
        try:
            await interaction.response.send_message("发生了未知错误。", ephemeral=True)
        except nextcord.InteractionResponded:
            await interaction.followup.send("发生了未知错误。", ephemeral=True)

关键说明

  • 异常类型替换:必须使用nextcord.ApplicationMissingPermissions类捕获Slash命令的用户权限缺失错误,原commands模块下的异常仅适用于前缀命令。
  • 响应状态兼容:通过try-except捕获InteractionResponded异常,确保互动已响应时自动切换为跟进消息发送提示,避免终端报错。
  • 临时消息优化:添加ephemeral=True参数,让错误提示仅触发命令的用户可见,不会干扰频道其他成员。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 16:13:27