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

discord.js多选菜单选中多选项时仅执行单条逻辑无法多次回复交互问题

问题根因
  • 交互响应方法i.reply()对同一个交互只能调用1次,第一次调用后交互就被标记为已响应,后续调用会直接失效,这就是多选时仅第一个选项生效、多次调用i.reply()仅收到1条回复的核心原因
  • 代码中重复创建了选择菜单的collector,在全局创建collector后又在collect事件回调中二次创建同类型collector,逻辑完全混乱
  • 第一个collector的filter写法错误,既没有给i.deferUpdate加调用括号,也没有返回用户ID匹配的判断结果,过滤器完全失效
  • 权限常量拼写错误:Permissions.FLAGS.ADMINSTRATTOR是错误写法,正确应为ADMINISTRATOR
  • createMessageComponentCollector不是异步方法,不需要加await调用
修复后代码
const { MessageEmbed, MessageActionRow, MessageSelectMenu, Permissions } = require("discord.js");
const color_roles = require("quick.db")

module.exports = {
  name: "color-roles", 
  category: "Settings", 
  aliases: ["colorroles"], 
  cooldown: 1, 
  usage: "color-roles", 
  description: "Set color roles", 
  memberpermissions: ["ADMINISTRATOR"],
  requiredroles: [], 
  alloweduserids: [], 
  minargs: 0, 
  maxargs: 0,
  minplusargs: 0, 
  maxplusargs: 0, 
  argsmissing_message: "", 
  argstoomany_message: "", 
  run: async (client, message, args, plusArgs, cmdUser, text, prefix) => {
    if (!message.guild.me.permissions.has(Permissions.FLAGS.MANAGE_ROLES)) return message.reply("<:pepe_no:900701245419974686> **I don't have** `MANAGE_ROLES` **permission in this server which is needed to setup the color roles**")
    if(message.guild.me.roles.highest.position < 15) return message.reply("<:pepe_no:900701245419974686> **My role need to be higher than 15 so that i can setup the color roles!**")
    if (!message.guild.me.permissions.has(Permissions.FLAGS.ADMINISTRATOR)) return message.reply("<:pepe_no:900701245419974686> **I don't have** `ADMINISTRATOR` **permission in this server which is needed to setup the color roles**")
    if (color_roles.has(message.guild.id+"_")) return message.reply("<:pepe_no:900701245419974686> **There is already have my color roles function running in this server, disable it to run a new one**")
    if (message.guild.roles.cache.size > 246) return message.reply("<:pepe_no:900701245419974686> **I couldn't create color roles if the server roles count is too close to the limit.**")
    
    const row = new MessageActionRow()
      .addComponents(
        new MessageSelectMenu()
          .setCustomId('setup_option_of_color_roles')
          .setMinValues(1)
          .setMaxValues(11)
          .setPlaceholder('Enable or disable a color')
          .addOptions([
            { label: 'Red', emoji: { id: "904319794180931624"}, value: 'red' },
            { label: 'Magenta', emoji: {id: "904309828271345714"}, value: 'magenta' },
            { label: 'Aqua', emoji: {id: "904323361079967785"}, value: 'aqua' },
            { label: 'Morning Glory', emoji: {id: "904320738574618685"}, value: 'morning_glory' },
            { label: 'Lime', emoji: {id: "904321773233930260"}, value: 'lime' },
            { label: 'Forest Green', emoji: {id: "904321409713602561"}, value: 'forest_green' },
            { label: 'Yellow', emoji: {id: "904322467122798603"}, value: 'yellow' },
            { label: 'Gold', emoji: {id: "904322921168769064"}, value: 'gold' },
            { label: 'Sandy Brown', emoji: {id: "904324015081017384"}, value: 'sandy_brown' },
            { label: 'Silver', emoji: {id: "904324628485378068"}, value: 'silver' },
            { label: 'Turmeric', emoji: {id: "904325941000568903"}, value: 'turmeric' },
          ]),
      );
    
    let theembed = new MessageEmbed()
      .setTitle("Color Roles")
      .addField("Red","disabled")
      .addField("Magenta","disabled")
    
    let msg = await message.reply({
      content:"halo",
      embeds: [theembed],
      components:[row],
    })
      
    const filter = i => i.user.id === message.author.id;
    const collector = message.channel.createMessageComponentCollector({ filter, componentType: 'SELECT_MENU', time: 60000 });

    collector.on('collect', async i => {
      // 先收集所有回复内容 最后统一响应
      let replyContents = [];
      i.values.forEach(value => {
        if (value === "red") replyContents.push("red");
        if (value === "magenta") replyContents.push("mag");
        // 其他颜色的逻辑同理添加
      })
      replyContents.push(`${i.user.id} clicked on the ${i.customId} menu.`);
      // 统一一次性回复
      await i.reply(replyContents.join("\n"));
    });

    collector.on('end', collected => {
      console.log(`Collected ${collected.size} interactions.`);
    });
  }
}
额外说明

如果需要发送多条独立消息,可以先调用await i.deferReply()标记交互已延迟响应,之后在循环中调用i.followUp(内容)发送多条消息即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 01:36:04