编辑 Select 菜单 | Discord.js

Editing a Select Menu | Discord.js

我正在尝试获取公会中的所有文本频道并将它们列在 Select 菜单中,但我遇到了一些麻烦。

这是我的代码:

const { PermissionOverwrites, Permissions, Collection, MessageEmbed, MessageActionRow, MessageSelectMenu } = require("discord.js");
const dotenv = require("dotenv");

dotenv.config();

module.exports = {
    data: {
        name: `select-raid-options`
    },
    async execute (interaction) {
        let guild = interaction.client.guilds.cache.get(process.env.GUILD_ID);
        switch (interaction.values[0]) {
            case 'raid_channel':
                let raidChannel = guild.channels.cache.find(channel => channel.name === 'raid');
                raidChannel.send('Raid');
                break;
            case 'select_channel':
                // Create the Embed Message
                const channelSelectorMessage = new MessageEmbed()
                    .setColor('#e4e8eb')
                    .setTitle('Veuillez sélectionnez le channel où vous souhaitez publier votre raid.')
                    .setTimestamp()

                // Create the Select Menu
                let channelSelectorSelect = new MessageSelectMenu()
                    .setCustomId('select-raid-channel')
                    .setPlaceholder('Nothing selected')
                    .addOptions([
                        {
                            label: `Annuler`,
                            description: 'Annulez la publication de votre raid.',
                            value: 'cancel',
                        },
                    ]);

                // Get all channels in the guild
                guild.channels.cache.forEach(channel => {
                    // Check if the channel is not the raid channel
                    if (channel.name !== 'raid') {
                        // add channel to the select menu
                        channelSelectorSelect.addOptions([
                            {
                                label: `${channel.name}`, // Option label
                                description: `${channel.name}`, // Option description
                                value: `${channel.id}`, // Option value
                            },
                        ]);
                    }
                })

                console.log(channelSelectorSelect); // Debug to see the select menu
                interaction.user.send({embeds: [channelSelectorMessage], components: [channelSelectorSelect]}); // Send the message with the select menu to the user
            }
        await interaction.reply({ content:'Information envoyée !', ephemeral: true });
    }  
}

频道的console.logSelect或Selectreturn我这个:

MessageSelectMenu {
  type: 'SELECT_MENU',
  customId: 'select-raid-channel',
  placeholder: 'Nothing selected',
  minValues: null,
  maxValues: null,
  options: [
    {
      label: 'Annuler',
      value: 'cancel',
      description: 'Annulez la publication de votre raid.',
      emoji: null,
      default: false
    },
    {
      label: '- ',
      value: '953674264408653825',
      description: '- ',
      emoji: null,
      default: false
    },
    {
      label: '̀',
      value: '953674264408653826',
      description: '̀',
      emoji: null,
      default: false
    },
    {
      label: '',
      value: '953674264408653827',
      description: '',
      emoji: null,
      default: false
    },
    {
      label: ' -  ',
      value: '954015397336735807',
      description: ' -  ',
      emoji: null,
      default: false
    },
    {
      label: '',
      value: '954015876892475433',
      description: '',
      emoji: null,
      default: false
    },
    {
      label: '́',
      value: '954015934081794128',
      description: '́',
      emoji: null,
      default: false
    },
    {
      label: '',
      value: '954016142844895302',
      description: '',
      emoji: null,
      default: false
    },
    {
      label: '',
      value: '954016204966727680',
      description: '',
      emoji: null,
      default: false
    },
    {
      label: ' -  ',
      value: '954020253757739018',
      description: ' -  ',
      emoji: null,
      default: false
    },
    {
      label: '3wa',
      value: '963018402132267048',
      description: '3wa',
      emoji: null,
      default: false
    },
    {
      label: 'tickets',
      value: '963018417936420895',
      description: 'tickets',
      emoji: null,
      default: false
    },
    {
      label: 'Degré Zero',
      value: '973526523200016384',
      description: 'Degré Zero',
      emoji: null,
      default: false
    },
    {
      label: 'test-commands',
      value: '973528693714595860',
      description: 'test-commands',
      emoji: null,
      default: false
    }
  ],
  disabled: false
}

当我使用选择器发送消息时,出现此错误:

DiscordAPIError: Invalid Form Body components[0]: The specified component type is invalid in this context

如果有人知道如何修复它?

谢谢

您需要将 select 菜单添加到操作行。如果您不知道如何访问 Discord.js 指南

中的 page

另一个答案是正确的,我想我应该放代码,因为那个页面没有演示如何动态添加选项。

// Create the Message Action row
let channelSelectorSelect = new MessageActionRow()
    .addComponents(
        new MessageSelectMenu()
        .setCustomId('select-raid-channel')
        .setPlaceholder('Nothing selected')
        .addOptions([{
            label: `Annuler`,
            description: 'Annulez la publication de votre raid.',
            value: 'cancel',
        }]);
    )

guild.channels.cache.forEach(channel => {
    if (channel.name !== 'raid') {
        // add channel to the select menu - the below line was changed
        channelSelectorSelect.components[0].addOptions([{
            label: `${channel.name}`,
            description: `${channel.name}`,
            value: `${channel.id}`,
        }]);
    }
})