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

Discord机器人Slash命令报错:Interaction has already been acknowledged

解决Discord机器人“Interaction has already been acknowledged.”错误

问题根源

  • 赋值语句误用作条件判断:代码中if ((interaction.commandName = "hello"))使用赋值运算符=而非相等判断符===,导致每次触发interactionCreate事件时,都会执行该分支的interaction.reply(),即使当前命令不是hello,重复回复引发错误。
  • 未过滤非命令交互:未先判断交互是否为Slash命令,可能对按钮、消息组件等其他类型交互执行回复操作,引发状态冲突。
  • 逻辑冗余矛盾:在food选项等于gi的分支内,又判断food != "gi",这部分代码永远不会执行,且核心的重复回复问题未解决。
  • 错误修改内部状态:手动修改interaction.replied属性无效,Discord.js会自行管理该状态,强行修改会导致状态混乱。

修正后的代码

import {
   Client,
   EmbedBuilder,
   GatewayIntentBits,
   Routes,
} from "discord.js";
import { config } from "dotenv";
import { REST } from "@discordjs/rest";

config();

const client = new Client({
   intents: [
      GatewayIntentBits.Guilds,
      GatewayIntentBits.GuildMessages,
      GatewayIntentBits.MessageContent,
   ],
});

const token = process.env.token;
const rest = new REST({ version: "10" }).setToken(token);
const CLIENT_ID = process.env.CLIENT_ID;

client.on("ready", () => {
   console.log(`${client.user.tag} is ready!`);
});

const embed = new EmbedBuilder()
   .setTitle("noobs.")
   .setColor("Aqua")
   .setDescription("hm.");

client.on("interactionCreate", async (interaction) => {
   // 只处理Slash命令交互
   if (!interaction.isChatInputCommand()) return;

   const { commandName } = interaction;

   if (commandName === "ping") {
      const foodOption = interaction.options.getString("food");
      if (foodOption === "gi") {
         await interaction.reply({ content: "pong" });
         // 如需追加内容,使用followUp而非重复reply
         // await interaction.followUp({ content: "额外内容" });
      } else {
         await interaction.reply({ embeds: [embed] });
      }
   } else if (commandName === "hello") {
      await interaction.reply({ content: "*gives love* hi" });
   }
});

const commands = [
   {
      name: "ping",
      description: "Replies with Pong!",
      options: [
         {
            name: "food",
            description: "ur mom",
            type: 3,
            required: true,
         },
      ],
   },
   {
      name: "hello",
      description: "Greets you with love.",
   },
];

async function main() {
   try {
      console.log("Loading application (/) commands.");
      await rest.put(Routes.applicationCommands(CLIENT_ID), { body: commands });
      console.log("Successfully reloaded application (/) commands.");
   } catch (err) {
      console.error(err);
   }
}

main();

client.login(token);

关键修改说明

  1. 添加命令交互过滤:用interaction.isChatInputCommand()确保只处理Slash命令,避免无关交互干扰。
  2. 修正条件判断运算符:将=改为===,正确匹配命令名称。
  3. 重构命令分支逻辑:用if-else if结构组织命令处理,确保每个命令只会触发一个回复分支。
  4. 正确处理多需求回复:如需在回复后追加内容,使用interaction.followUp();如需修改已发送回复,使用interaction.editReply()。
  5. 添加async/await:Discord.js交互方法为异步操作,用await确保操作顺序执行,避免状态冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 13:05:40