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

Discord Bot使用SlashCommandBuilder出现‘应用未响应’问题求助

问题原因与解决办法

核心问题

  1. 命令注册格式错误:直接将client.commands(Collection对象)传给Discord API,但API要求接收的是标准化的命令JSON结构数组,当前命令文件的结构不符合SlashCommand规范。
  2. Client初始化缺少必要权限:discord.js v13及以上版本创建Client时必须指定intents,未配置的话Bot无法接收interactionCreate事件,导致命令触发后无响应。
  3. 命令定义不规范:现有命令文件仅简单定义了名称和描述,未按照Discord要求使用SlashCommandBuilder构建标准命令结构。

分步解决

1. 补全Client的Intents配置

修改index.js中的Client初始化代码,添加必备的交互权限:

// 先导入GatewayIntentBits
const { Client, Collection, GatewayIntentBits } = require('discord.js');
// 创建Client时指定所需intents
const client = new Client({ 
  intents: [
    GatewayIntentBits.Guilds, // 必须,用于接收服务器交互事件
    GatewayIntentBits.GuildMessages // 可选,若需处理消息相关功能则添加
  ] 
});

2. 修正命令注册的格式逻辑

在ready事件中,将命令集合转换为API可识别的JSON数组:

client.once('ready', () => {
  console.log('Bot is ready!');
  const rest = new REST({ version: '9' }).setToken(config.token);
  // 将每个命令的标准结构转为JSON格式,组成数组
  const commandData = client.commands.map(cmd => cmd.data.toJSON());
  rest.put(
    Routes.applicationCommands(client.user.id),
    { body: commandData },
  )
    .then(() => console.log('Successfully registered commands'))
    .catch(error => console.error(error));
});

3. 规范命令文件的结构

修改commands/hello.js,使用SlashCommandBuilder定义标准命令结构:

const { SlashCommandBuilder } = require('discord.js');

module.exports = {
  data: new SlashCommandBuilder()
    .setName('hello')
    .setDescription('Replys with Hi'),
  // 改为异步方法,确保回复操作完成后再结束执行
  async execute(interaction) {
    await interaction.reply({ content: 'Hi!' });
  },
};

额外注意事项

  • 邀请Bot到服务器时,必须勾选applications.commands权限范围,否则Slash命令无法在服务器内正常使用。
  • 测试阶段建议优先注册服务器专属命令(使用Routes.applicationGuildCommands(client.user.id, 你的服务器ID)),全局命令生效通常需要1小时左右,服务器命令则即时生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 17:50:36