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

Discord Bot在线但无响应、新命令不显示问题求助

解决Discord Bot的「应用未响应」与命令不更新问题

一、命令不更新的原因与解决

你的Bot存在命令未同步到Discord平台后台的问题——Discord的Slash命令不会自动同步,必须主动注册,因此新增命令无法显示、已删除的旧命令仍残留。

1. 补充同步所需模块

在代码顶部添加Discord.js的REST和Routes模块:

import { REST, Routes } from 'discord.js';

2. 添加命令同步逻辑

在ready事件中加入命令同步代码,开发阶段推荐用服务器级同步(即时生效),上线后可切换为全局同步(1小时左右生效):

client.once('ready', async () => {
  console.log(`Logged in as ${client.user.tag}!`);

  // 整理要同步的命令数据
  const commands = [];
  for (const file of commandFiles) {
    const commandModule = await import(`./src/commands/${file}`);
    commands.push(commandModule.default.data.toJSON());
  }

  // 初始化REST客户端
  const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN);

  // 同步命令到指定测试服务器(替换为你的服务器ID)
  try {
    console.log('开始同步应用命令...');
    await rest.put(
      Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.GUILD_ID),
      { body: commands }
    );
    console.log('应用命令同步成功!');
  } catch (error) {
    console.error('同步命令出错:', error);
  }
});

需要在.env文件中新增两个变量:

  • CLIENT_ID:你的Bot应用ID(在Discord开发者后台查看)
  • GUILD_ID:测试服务器的ID

3. 清理残留旧命令

如果要彻底删除Discord后台的旧命令,可临时在ready事件中执行以下代码(执行一次后注释掉,避免每次启动清空命令):

await rest.put(
  Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.GUILD_ID),
  { body: [] }
);

二、「应用未响应」的原因与解决

当用户触发了本地已删除但Discord后台仍存在的命令时,你的代码直接return未发送任何回复,导致Discord判定Bot未响应。修改interactionCreate事件:

client.on('interactionCreate', async interaction => {
  if (!interaction.isCommand()) return;

  const command = client.commands.get(interaction.commandName);
  if (!command) {
    // 找不到命令时回复用户,避免平台提示未响应
    await interaction.reply({ content: '该命令已被移除或不存在。', ephemeral: true });
    return;
  }

  try {
    await command.execute(interaction);
  } catch (error) {
    console.error(error);
    // 处理已回复/延迟回复的情况,避免重复回复报错
    if (interaction.replied || interaction.deferred) {
      await interaction.followUp({ content: '执行命令时发生错误。', ephemeral: true });
    } else {
      await interaction.reply({ content: '执行命令时发生错误。', ephemeral: true });
    }
  }
});

三、修改后的完整代码

import { Client, Collection, GatewayIntentBits, REST, Routes } from 'discord.js';
import fs from 'fs';
import dotenv from 'dotenv';
dotenv.config();

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

client.commands = new Collection();

const commandsPath = './src/commands';
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));

// 改用异步加载确保命令全部加载完成
(async () => {
  for (const file of commandFiles) {
    const commandModule = await import(`./src/commands/${file}`);
    client.commands.set(commandModule.default.data.name, commandModule.default);
  }
})();

client.once('ready', async () => {
  console.log(`Logged in as ${client.user.tag}!`);

  const commands = [];
  for (const file of commandFiles) {
    const commandModule = await import(`./src/commands/${file}`);
    commands.push(commandModule.default.data.toJSON());
  }

  const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN);

  try {
    console.log('开始同步应用命令...');
    await rest.put(
      Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.GUILD_ID),
      { body: commands }
    );
    console.log('应用命令同步成功!');
  } catch (error) {
    console.error('同步命令出错:', error);
  }
});

client.on('interactionCreate', async interaction => {
  if (!interaction.isCommand()) return;

  const command = client.commands.get(interaction.commandName);
  if (!command) {
    await interaction.reply({ content: '该命令已被移除或不存在。', ephemeral: true });
    return;
  }

  try {
    await command.execute(interaction);
  } catch (error) {
    console.error(error);
    if (interaction.replied || interaction.deferred) {
      await interaction.followUp({ content: '执行命令时发生错误。', ephemeral: true });
    } else {
      await interaction.reply({ content: '执行命令时发生错误。', ephemeral: true });
    }
  }
});

client.login(process.env.DISCORD_TOKEN);

额外注意事项

  • 确保.env文件包含DISCORD_TOKEN、CLIENT_ID、GUILD_ID三个变量
  • 开发阶段用服务器级同步,上线前可切换为全局同步(将Routes.applicationGuildCommands替换为Routes.applicationCommands(process.env.CLIENT_ID))
  • 修改命令后需重启Bot触发同步

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 04:36:34