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

Discord.js SelectMenu多选时仅处理首个对应角色的问题修复

问题根源

你的代码存在4个核心错误导致多选失效:

  • 取值错误:只通过interaction.values[0]获取了选中数组的第一个元素,没有读取全部选中值
  • 逻辑中断:每个角色处理分支内都写了return语句,处理完第一个值就直接终止整个函数,剩余选中值完全不会被执行
  • 重复回复:每个分支都调用interaction.reply(),Discord 单次交互仅允许回复一次,第一次调用后后续逻辑直接报错
  • 逻辑缺失:下拉菜单返回的interaction.values是用户本次提交后所有处于选中状态的选项集合,不是本次新勾选的选项,原逻辑没有处理「用户取消勾选的选项需要移除角色」的场景

修复方案

你原来的MessageSelectMenu组件配置不需要改动,setMaxValues(5)、setMinValues(0)的设置本身是正确的。
首先在文件顶部建立选项值和角色ID的映射表,后续新增选项只需要补全对应键值对即可,不用重复写冗余的if/else判断:

// 选项value -> 对应角色ID 映射表,补全剩余3个选项的角色ID即可使用
const ROLE_MAP = {
  changement: "988719545667702814",
  annonce: "988719685300285520",
  suggestion: "", // 填入suggestion选项对应的角色ID
  confession: "", // 填入confession选项对应的角色ID
  anniversaire: "" // 填入anniversaire选项对应的角色ID
}

将原有的interactionCreate事件处理代码替换为以下内容:

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

const ROLE_MAP = {
  changement: "988719545667702814",
  annonce: "988719685300285520",
  suggestion: "",
  confession: "",
  anniversaire: ""
}

module.exports = {
  name: 'interactionCreate',
  async execute(interaction) {
    if (!interaction.isSelectMenu() || interaction.customId !== 'notifications-select') return;

    const addedRoles = [];
    const removedRoles = [];
    const member = interaction.member;
    const selectedValues = interaction.values;

    // 遍历所有配置的通知角色,逐个判断加/卸
    for (const [optionValue, roleId] of Object.entries(ROLE_MAP)) {
      if (!roleId) continue;
      const role = interaction.guild.roles.cache.get(roleId);
      if (!role) continue;

      const isSelected = selectedValues.includes(optionValue);
      const hasRole = member.roles.cache.has(roleId);

      if (isSelected && !hasRole) {
        await member.roles.add(role);
        addedRoles.push(role.name);
        console.log('\x1b[32m', `🏷 [ Roles ] ⇢ [ ${interaction.user.tag} (${interaction.user.id}) ] s'est ajouté [${role.name}]`);
      } else if (!isSelected && hasRole) {
        await member.roles.remove(role);
        removedRoles.push(role.name);
        console.log('\x1b[32m', `🏷 [ Roles ] ⇢ [ ${interaction.user.tag} (${interaction.user.id}) ] s'est retiré [${role.name}]`);
      }
    }

    // 汇总所有操作结果,统一回复一次
    const descriptionLines = [];
    if (addedRoles.length) {
      descriptionLines.push(`✅ Rôles \`ajoutés\` avec succès : ${addedRoles.map(r => `@${r}`).join(', ')}`);
    }
    if (removedRoles.length) {
      descriptionLines.push(`❌ Rôles \`retirés\` avec succès : ${removedRoles.map(r => `@${r}`).join(', ')}`);
    }
    if (!descriptionLines.length) {
      descriptionLines.push("Aucun changement de rôle n'a été effectué.");
    }

    const confirmationEmbed = new MessageEmbed()
      .setColor(addedRoles.length ? '#43B17E' : '#F04848')
      .setAuthor({
        name: 'Mise à jour de ton profil',
        iconURL: 'https://media.discordapp.net/attachments/947816618824724480/948574842087690250/tada_animated.gif'
      })
      .setDescription(descriptionLines.join('\n'));
    
    await interaction.reply({
      embeds: [confirmationEmbed],
      ephemeral: true
    });
  },
};

逻辑说明
  • 自动支持任意数量的选项组合选择,不管选1个还是5个,都能正确授予对应角色
  • 用户取消勾选的选项会自动移除对应角色,不需要额外写判断
  • 所有角色处理完成后才会发送一次回复,不会触发交互重复回复报错
  • 后续新增下拉选项只需要在ROLE_MAP中补充对应value和角色ID的映射,不需要修改核心逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 20:24:28