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

discord.js斜杠命令可用但终端报Unknown interaction错误求助

Discord斜杠命令10062 Unknown interaction报错排查

问题表现

斜杠命令plusbg在Discord客户端可正常触发、功能运行符合预期,但终端持续抛出Discord API返回的Unknown interaction错误,错误码10062,HTTP状态404。

相关代码

plusbg命令文件源码

module.exports = {
    name: "plusbg",
    description: "Qui est le plus beau de ce serveur ?",
    run: (client, message, args) => {
        const embed = new MessageEmbed()
        .setTitle('Le plus beau est Ratile')
        .setThumbnail(client.user.displayAvatarURL())
        .addField (
            { name: 'Latence', value: `\`${client.ws.ping}ms\``, inline: true },
            { name: 'Uptime', value: `<t:${parseInt(client.readyTimestamp / 1000)}:R>`, inline: true},
        )
        .setTimestamp()
        .setFooter({ text: message.author.username, iconURL: message.author.displayAvatarURL() });

            message.channel.send({ embeds: [embed]});
    },
    runSlash: (client, interaction) => {
        const embed = new MessageEmbed()
        .setColor('DARK_VIVID_PINK')
        .setTitle('Le plus beau est Ratile')
        .setThumbnail(client.user.displayAvatarURL())
        .addFields(
            { name: 'Latence', value: `\`${client.ws.ping}ms\``, inline: true },
            { name: 'Uptime', value: `<t:${parseInt(client.readyTimestamp / 1000)}:R>`, inline: true},
        )
        .setTimestamp()
        .setFooter({ text: interaction.user.username, iconURL: interaction.user.displayAvatarURL() });

        interaction.reply({ embeds: [embed]})
    },
};

命令内提示文本为法语

终端报错日志

-----
 Promise {
  <rejected> DiscordAPIError: Unknown interaction
      at RequestHandler.execute (C:\Users\ratil\Desktop\Bot - Skypiea\node_modules\discord.js\src\rest\RequestHandler.js:350:13)      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async RequestHandler.push (C:\Users\ratil\Desktop\Bot - Skypiea\node_modules\discord.js\src\rest\RequestHandler.js:51:14)
      at async CommandInteraction.reply (C:\Users\ratil\Desktop\Bot - Skypiea\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:99:5) {
    method: 'post',
    path: '/interactions/992792318635683912/aW50ZXJhY3Rpb246OTkyNzkyMzE4NjM1NjgzOTEyOkhUUnI5ZXF1bUw3WmxuR1RoN3gyU2N4eGFNYjYxWGgzb2JhWjhyOXVNTnlHSnQ4azhqRkxQaUF4QTVwZTFUZkxZNjVHdEhEOXh4WklTV2g0Yk9xM28yQURPUFBlS2Qwd050R3BSbHVGcFVFdDNXWWlHUW5xUXZZbFdpTzAyc3Ry/callback',
    code: 10062,
    httpStatus: 404,
    requestData: { json: [Object], files: [] }
  }
}

interactionCreate事件处理源码

module.exports = {
    name: "interactionCreate",
    once: false,
    async execute(client, interaction) {
        if (interaction.isCommand()) {
            const cmd = client.commands.get(interaction.commandName);
            if (!cmd) return interaction.reply(`Cette commande n'existe pas`);
            cmd.runSlash(client, interaction);
        }
    }
};

事件代码内提示文本为法语

故障原因

  • 核心触发逻辑:Discord对斜杠交互有强制3秒首次响应时限,超过时限后交互token会直接失效,此时再调用interaction.reply()就会返回10062错误。你能看到命令正常回复,是部分请求刚好卡到超时边界被客户端接收,其余超时的请求就会抛出错误。
  • 代码层面的隐患直接放大了超时概率:
    • interactionCreate事件方法本身是async,但调用cmd.runSlash时没有加await,异步执行顺序不受控,事件回调不等待命令执行完成就退出,reply请求的发送优先级被降低,很容易超过3秒时限。
    • 普通消息命令的run方法中addField用法错误,discord.js v13+版本中addField接收三个独立参数(name, value, inline),传入对象的写法会触发内部运行时异常,额外占用事件循环延迟,拖慢斜杠命令的响应速度。

修复方案

  1. 给事件中的命令调用加await,保证交互响应逻辑优先执行:
module.exports = {
    name: "interactionCreate",
    once: false,
    async execute(client, interaction) {
        if (interaction.isCommand()) {
            const cmd = client.commands.get(interaction.commandName);
            if (!cmd) return interaction.reply(`Cette commande n'existe pas`);
            // 等待命令执行完成再退出事件回调
            await cmd.runSlash(client, interaction);
        }
    }
};
  1. 将命令的runSlash方法改为async函数,对reply操作加await;如果后续命令有数据库查询、第三方API请求等耗时逻辑,提前调用deferReply将响应时限延长到15分钟:
runSlash: async (client, interaction) => {
    // 有耗时操作时取消下一行注释
    // await interaction.deferReply();
    const embed = new MessageEmbed()
    .setColor('DARK_VIVID_PINK')
    .setTitle('Le plus beau est Ratile')
    .setThumbnail(client.user.displayAvatarURL())
    .addFields(
        { name: 'Latence', value: `\`${client.ws.ping}ms\``, inline: true },
        { name: 'Uptime', value: `<t:${parseInt(client.readyTimestamp / 1000)}:R>`, inline: true},
    )
    .setTimestamp()
    .setFooter({ text: interaction.user.username, iconURL: interaction.user.displayAvatarURL() });
    // 等待回复请求发送完成
    await interaction.reply({ embeds: [embed]})
},
  1. 修复普通消息命令的方法调用错误,将run方法中的.addField(改为.addFields(,避免不必要的运行时异常拖慢程序效率。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 03:57:20