Discord slash commands 提示'Interaction failed'随机报错问题求助
问题根因分析
- Discord交互3秒硬限制:Discord官方要求所有斜杠命令交互必须在触发后3秒内返回响应,超过阈值就会直接返回「The Interaction failed」报错。初始触发时Bot负载低、网络状况好可以正常响应,后续运行过程中出现网络波动、Bot负载升高就会出现随机超时的情况。
- discord-slash库已停止维护:你使用的
discord_slash第三方库(即discord-py-slash-command)早已停止更新,无法适配Discord最新的交互API规则,接口兼容性问题会导致随机响应失败。 - 自动同步触发限流:初始化
SlashCommand时设置sync_commands=True会让Bot每次启动都全量同步全局斜杠命令,频繁的同步请求会触发Discord的API限流,后续命令请求会被拦截导致超时。 - 网关权限配置缺失:如果未在Discord开发者后台的Bot管理页开启对应特权网关意图(Privileged Gateway Intents),会导致Bot与Discord的网关连接不稳定,事件推送延迟超过3秒就会触发报错。
解决方案
临时修复方案(继续使用旧discord-slash库)
先调用defer()方法抢占3秒响应窗口,同时关闭自动命令同步,手动按需触发同步,修改后代码如下:
from discord_slash import SlashCommand client = commands.Bot(command_prefix='!', intents=intents) # 关闭自动同步,避免频繁触发限流 slash = SlashCommand(client, sync_commands=False) @slash.slash(description='Ping.') async def ping(ctx): # 先发送延迟响应,抢占3秒响应窗口 await ctx.defer() await ctx.send(f'Current speed - {round(client.latency * 1000)}ms')
如需同步命令,仅在首次部署、命令修改时手动调用slash.sync_all_commands()即可,不要每次启动都同步。
长期稳定方案(更换discord.py原生斜杠命令)
discord.py 2.0及以上版本已经原生支持斜杠命令,无需引入第三方库,兼容性和稳定性更高,实现示例如下:
import discord from discord import app_commands # 确保intents配置和开发者后台开启的权限一致 intents = discord.Intents.default() client = discord.Client(intents=intents) tree = app_commands.CommandTree(client) @tree.command(name="ping", description="Ping.") async def ping(interaction: discord.Interaction): await interaction.response.send_message(f'Current speed - {round(client.latency * 1000)}ms') @client.event async def on_ready(): # 开发阶段建议指定单服务器同步,速度更快且不会触发全局限流 # await tree.sync(guild=discord.Object(id=你的服务器ID)) # 上线后需要全局生效再调用全量同步 await tree.sync() print("Bot启动完成") client.run("你的Bot Token")
另外需要确认Discord开发者后台的Bot管理页中,已经开启了所有你用到的特权网关意图开关。
内容的提问来源于stack exchange,提问作者Ala
相关产品推荐
相关产品推荐

