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

Discord Bot /help斜杠命令不显示且无法使用求助

Discord Bot /help斜杠命令不显示的解决方法

核心问题分析

你的代码从未在Bot启动阶段注册/help斜杠命令,仅在点击exit按钮时才执行client.application.commands.create(helpCommand),这导致Discord服务器完全无法识别该命令,自然不会在命令列表中显示。

此外还有几个次要问题:

  • 重复导入discord.js组件(如MessageActionRow和ActionRowBuilder重复引入)
  • 按钮收集器基于频道创建,不适用于临时消息(ephemeral)
  • 硬编码的频道ID、Token未替换为实际值

分步解决方案

1. 正确注册斜杠命令

在client.on('ready')事件中完成命令注册,而非按钮触发时。支持两种注册方式:

  • 全局注册:所有服务器生效,需1小时左右同步
  • 单服务器注册:仅目标服务器生效,即时同步(适合测试)

2. 清理重复依赖

删除重复的discord.js组件导入,使用官方推荐的Builder类创建按钮,而非原始JSON。

3. 修复交互逻辑

临时消息(ephemeral)仅能由发起用户操作,收集器需基于回复消息创建,而非频道。同时需先延迟更新交互,避免Discord超时报错。

4. 确认权限配置

邀请Bot时必须勾选applications.commands权限,否则无法创建斜杠命令。

修改后的完整代码

// Import required modules
const { Client, GatewayIntentBits, ActionRowBuilder, ButtonBuilder, ButtonStyle, ComponentType } = require('discord.js');
const { SlashCommandBuilder } = require('@discordjs/builders');

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

// 替换为你的实际配置
const token = "你的Bot Token";
const ALLOWED_CHANNEL_ID = "允许使用命令的频道ID";
const TEST_GUILD_ID = "测试服务器ID"; // 单服务器注册时使用

// 注册斜杠命令
async function registerCommands() {
  const helpCommand = new SlashCommandBuilder()
    .setName('help')
    .setDescription('获取各类问题的帮助');

  // 选择注册方式:二选一
  // 1. 全局注册(所有服务器生效,需1小时同步)
  await client.application.commands.create(helpCommand);
  // 2. 单服务器注册(仅指定服务器生效,即时同步)
  // const guild = client.guilds.cache.get(TEST_GUILD_ID);
  // await guild.commands.create(helpCommand);

  console.log("斜杠命令注册完成");
}

client.on('ready', async () => {
  console.log(`已登录为 ${client.user.tag}`);
  // Bot启动时立即注册命令
  await registerCommands().catch(console.error);
});

// 处理交互事件
client.on('interactionCreate', async (interaction) => {
  // 处理/help命令
  if (interaction.isChatInputCommand() && interaction.commandName === 'help') {
    // 检查频道权限
    if (interaction.channelId !== ALLOWED_CHANNEL_ID) {
      await interaction.reply({ content: '该命令仅允许在指定频道使用', ephemeral: true });
      return;
    }

    // 使用Builder类创建按钮组件
    const row1 = new ActionRowBuilder()
      .addComponents(
        new ButtonBuilder().setCustomId('ban-button').setLabel('Ban Info').setStyle(ButtonStyle.Success),
        new ButtonBuilder().setCustomId('first-button').setLabel('Tools Status').setStyle(ButtonStyle.Success),
        new ButtonBuilder().setCustomId('second-button').setLabel('Program Not Running').setStyle(ButtonStyle.Primary),
        new ButtonBuilder().setCustomId('third-button').setLabel('Failed Error').setStyle(ButtonStyle.Primary)
      );

    const row2 = new ActionRowBuilder()
      .addComponents(
        new ButtonBuilder().setCustomId('fourth-button').setLabel('Cracked').setStyle(ButtonStyle.Primary),
        new ButtonBuilder().setCustomId('fifth-button').setLabel('DLC Cars').setStyle(ButtonStyle.Primary),
        new ButtonBuilder().setCustomId('sixth-button').setLabel('Credits').setStyle(ButtonStyle.Primary),
        new ButtonBuilder().setCustomId('seventh-button').setLabel('Add XP').setStyle(ButtonStyle.Primary)
      );

    const row3 = new ActionRowBuilder()
      .addComponents(
        new ButtonBuilder().setCustomId('eighth-button').setLabel('Add Wheel-spins').setStyle(ButtonStyle.Primary),
        new ButtonBuilder().setCustomId('ninth-button').setLabel('Disable AC Error').setStyle(ButtonStyle.Primary),
        new ButtonBuilder().setCustomId('tenth-button').setLabel('AIO V2 Virus?').setStyle(ButtonStyle.Primary),
        new ButtonBuilder().setCustomId('exit-button').setLabel('Exit').setStyle(ButtonStyle.Secondary)
      );

    // 发送带按钮的临时回复,开启fetchReply用于创建收集器
    const reply = await interaction.reply({
      content: '已识别帮助命令\n如果你遇到问题或需要解决方案,请点击下方按钮获取对应答案',
      components: [row1, row2, row3],
      ephemeral: true,
      fetchReply: true
    });

    // 基于回复消息创建按钮收集器
    const collector = reply.createMessageComponentCollector({ componentType: ComponentType.Button, time: 60000 });

    collector.on('collect', async (buttonInteraction) => {
      // 先延迟更新,避免Discord超时
      await buttonInteraction.deferUpdate();

      let response = '';
      switch (buttonInteraction.customId) {
        case 'ban-button': response = 'Ban Info按钮已点击'; break;
        case 'first-button': response = 'Tools Status按钮已点击'; break;
        case 'second-button': response = 'Program Not Running按钮已点击'; break;
        case 'third-button': response = 'Failed Error按钮已点击'; break;
        case 'fourth-button': response = 'Cracked按钮已点击'; break;
        case 'fifth-button': response = 'DLC Cars按钮已点击'; break;
        case 'sixth-button': response = 'Credits按钮已点击'; break;
        case 'seventh-button': response = 'Add XP按钮已点击'; break;
        case 'eighth-button': response = 'Add Wheel-spins按钮已点击'; break;
        case 'ninth-button': response = 'Disable AC Error按钮已点击'; break;
        case 'tenth-button': response = 'AIO V2 Virus?按钮已点击'; break;
        case 'exit-button': 
          response = '正在退出...';
          collector.stop();
          break;
      }

      await buttonInteraction.editReply({ content: response, components: [] });
    });

    collector.on('end', async () => {
      try {
        await interaction.editReply({ content: '超时,请重新尝试', components: [] });
        setTimeout(async () => await interaction.deleteReply(), 3000);
      } catch (err) {
        // 忽略已被删除的消息错误
      }
    });
  }
});

client.login(token);

额外注意事项

  • 全局注册命令后,Discord需要约1小时同步到所有服务器,测试阶段建议使用单服务器注册
  • 邀请Bot时,必须在Discord开发者门户OAuth2页面勾选bot和applications.commands权限
  • 替换代码中所有占位符为实际值,否则命令无法正常运行

内容的提问来源于stack exchange,提问作者Hahahaha You trash

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 10:23:21