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

Discord.js v14选择菜单交互失败:选选项仅显报错无响应

问题原因及修复方案

核心问题

  1. 选择菜单初始被禁用:调用components(true)时,传入的true会触发setDisabled(state)将菜单设为禁用状态,用户无法正常触发交互(若你能点击菜单,大概率是代码粘贴时的转义问题,实际代码中箭头函数应为=>而非=>)。
  2. Collector绑定对象错误:使用message.channel.createMessageComponentCollector会监听整个频道的所有选择菜单,而非仅监听你发送的帮助消息,导致交互无法被正确捕获。
  3. 交互响应无容错处理:未确保交互在Discord要求的3秒内完成响应,也未处理潜在的响应失败情况。

修复后的代码

// 需提前导入ComponentType:const { ComponentType } = require('discord.js');
if (!args[0]) {
  const home = new EmbedBuilder()
    .setAuthor({
      name: `Welcome to the help panel.`,
      iconURL: client.user.displayAvatarURL({ dynamic: true, size: 512 }),
    })
    .setColor(COLORS)
    .setDescription(
      `You can start streaming music straight away without any setup, just run the \`${PREFIX}play\` command, give it something to search for and you're good to go! \n\nUnlock exclusive benefits by purchasing a premium membership: **Buy Premium** \n\n**All Link:** WebSite | Vote | Support`,
    )
    .setFooter({
      text: `© MihaiT`,
      iconURL: message.author.displayAvatarURL(),
    });

  const info = new EmbedBuilder()
    .setColor(COLORS)
    .setAuthor({
      name: `Eli's Commands`,
      iconURL: client.user.displayAvatarURL({ dynamic: true, size: 512 }),
    })
    .addFields(
      {
        name: `<:Eli_Bot:972786663078105128> Info`,
        value: `Detailed help for an order: \`${PREFIX}help [commands]\` \nFor more help, visit our support server`,
      },
      {
        name: `<:Eli_ADD:972784975172751380> Comandos`,
        value: `\`${PREFIX}about\`: Stats and bots details. \n\`${PREFIX}help\`: Help commands. \n\`${PREFIX}invite\`: You receive a link so you can invite me to any server you manage. \n\`${PREFIX}vote\`: Vote for Nakano on Top.gg \n\`${PREFIX}support\`: To join communities.`,
      },
    )
    .setFooter({
      text: `© MihaiT`,
      iconURL: message.author.displayAvatarURL(),
    });

  const components = (state) => [
    new ActionRowBuilder().addComponents(
      new SelectMenuBuilder()
        .setCustomId('help-menu')
        .setPlaceholder('Please Select a Category')
        .setDisabled(state)
        .addOptions([
          {
            label: `Home`,
            value: `home`,
            description: `Go home`,
            emoji: `<:EliOfficial:972798080393052191>`,
          },
          {
            label: `Info`,
            value: `info`,
            description: `See info commands`,
            emoji: `<:Eli_Bot:972786663078105128>`,
          },
        ]),
    ),
  ];

  // 保存回复消息对象,用于绑定专属collector
  const replyMsg = await message.reply({
    embeds: [home],
    allowedMentions: { repliedUser: false },
    components: components(false), // 传入false启用菜单
  });

  const filter = (interaction) => interaction.user.id === message.author.id;

  // 仅监听当前回复消息的交互
  const collector = replyMsg.createMessageComponentCollector({
    filter,
    componentType: ComponentType.SelectMenu,
    max: 10,
    time: 60000, // 60秒超时自动结束
  });

  collector.on('collect', async (interaction) => {
    try {
      // 更新原消息内容(若需发送临时消息,替换为interaction.reply({...ephemeral: true}))
      if (interaction.values[0] === 'home') {
        await interaction.update({ embeds: [home], components: components(false) });
      } else if (interaction.values[0] === 'info') {
        await interaction.update({ embeds: [info], components: components(false) });
      }
    } catch (err) {
      console.error(err);
      // 兜底处理响应失败的情况
      await interaction.reply({ content: '处理请求时出错', ephemeral: true }).catch(() => {});
    }
  });

  // 收集结束后禁用菜单
  collector.on('end', () => {
    replyMsg.edit({ components: components(true) }).catch(() => {});
  });
}

关键修改说明

  • 启用选择菜单:将components(true)改为components(false),让菜单可交互
  • 绑定专属Collector:使用回复消息对象创建Collector,确保只监听当前帮助消息的交互
  • 补充响应容错:添加try/catch捕获异常,避免因响应超时或其他问题导致交互失败
  • 使用ComponentType枚举:Discord.js v14要求明确指定组件类型,需提前导入

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 04:24:17