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

Discord.js v14中/rank命令报Unknown interaction(code:10062)求助

问题描述

在Discord.js v14环境中,所有其他命令运行正常,但使用/rank命令时,仅当参数(用户ID、等级ID)合法无错误时,会出现Unknown interaction(code:10062)错误;参数非法时命令反而正常。希望找到基于.then/.catch的修复方案,不使用try{}。

相关代码:

if (interaction.commandName === "rank") {
    const tuser = interaction.options.getNumber("targetuser");
    const trank = interaction.options.getNumber("targetrank");

    noblox
      .changeRank(5156922, tuser, trank)
      .then(async () => {
        const username = await noblox.getUsernameFromId(tuser);
        const grn = await noblox.getRankNameInGroup(5156922, tuser);
        interaction.reply(
          `The rank of ${username} has been updated and they have been ranked to **${grn}**`
        );
      })
      .catch((error) => {
        console.error(error.stack);
        interaction.respond({
          content: `Something went wrong, Here is what can have gone wrong: 

- **The user id does not exist or isn't in the group**
- **The rank number does not exist**
Please double check the fields. Thank you!`,
          ephemeral: true,
        });
      });
  }

错误信息:

rawError: { message: 'Unknown interaction', code: 10062 },
  code: 10062,
  status: 404,
  method: 'POST',
  url: 'https://discord.com/api/v10/interactions/1193921932316377109/aW50ZXJhY3Rpb246MTE5MzkyMTkzMjMxNjM3NzEwOTpjRkVPQTZlSzUxNE1RQmxzM0M4Y3h1TFN6VU45ZmpkVEtXTXk2cThCalpGUWtiSXZHVnVtVVBxTzR2U01HcDZRb0RRT3RxeXAwYmZxTlpicDZIMjAwb3RreFQ4Wm5CaEN4MXVKdmx1cGZpZGRWdW5YUXo4Q2Zxd3NDZmNOM05WNQ/callback'
}
原因分析

Unknown interaction(code:10062)错误的核心原因是:当参数合法时,noblox.changeRank、getUsernameFromId、getRankNameInGroup这一系列异步操作的总耗时超过了Discord交互的3秒超时限制,导致交互令牌失效;而参数非法时,noblox.changeRank会立即抛出错误进入catch块,能在超时前完成响应,因此不会触发错误。

修复方案

通过提前调用deferReply告知Discord将延迟响应,延长交互的有效时间,之后再使用followUp发送最终结果。以下是基于.then/.catch的修改代码:

if (interaction.commandName === "rank") {
    const tuser = interaction.options.getNumber("targetuser");
    const trank = interaction.options.getNumber("targetrank");

    // 先延迟响应,避免超时
    interaction.deferReply()
      .then(() => {
        // 执行改等级操作
        return noblox.changeRank(5156922, tuser, trank);
      })
      .then(async () => {
        // 获取用户名和新等级名称
        const username = await noblox.getUsernameFromId(tuser);
        const grn = await noblox.getRankNameInGroup(5156922, tuser);
        // 发送最终结果
        return interaction.followUp(
          `已更新${username}的等级,当前等级为**${grn}**`
        );
      })
      .catch((error) => {
        console.error(error.stack);
        // 根据是否已延迟响应,选择合适的回复方式
        const replyMethod = interaction.deferred ? interaction.followUp : interaction.reply;
        replyMethod({
          content: `操作失败,可能的原因:

- **用户ID不存在或未加入群组**
- **等级编号不存在**
请检查参数后重试,谢谢!`,
          ephemeral: true,
        });
      });
}
关键修改说明
  1. 新增interaction.deferReply():在执行耗时操作前,立即向Discord发送延迟响应的信号,重置交互超时计时器。
  2. 将原interaction.reply改为interaction.followUp:延迟响应后,必须使用followUp发送最终内容。
  3. 统一错误处理逻辑:通过判断interaction.deferred状态,选择使用followUp或reply发送错误提示,确保无论何时出错都能正确响应。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 16:33:31