Discord模态框添加SelectMenu报错DiscordAPIError[50035]求解决方案
Discord模态框添加选择菜单报错的解决办法
错误原因
Discord官方API明确规定:模态框(Modal)仅支持文本输入组件(TextInput,对应API类型4),无法直接添加选择菜单(SelectMenu)这类交互组件。你代码里把SelectMenu放进了Modal的ActionRow中,触发了API的类型校验错误,所以抛出DiscordAPIError[50035]。
解决方案
根据需求选择以下两种处理方式:
1. 移除模态框中的选择菜单(仅保留合法的文本输入)
如果你的验证流程不需要选择菜单,直接移除SelectMenu组件,保留Modal里的文本输入即可:
const modal = new client.discord.ModalBuilder() .setCustomId('verification-modal') .setTitle('Verify yourself') .addComponents([ new client.discord.ActionRowBuilder().addComponents( new client.discord.TextInputBuilder() .setCustomId('verification-input') .setLabel('Answer') .setStyle(client.discord.TextInputStyle.Short) .setMinLength(4) .setMaxLength(12) .setPlaceholder('ABCDEF') .setRequired(true), ), ]); await interaction.showModal(modal);
2. 拆分流程:先展示选择菜单,再打开模态框
如果必须同时用到选择和输入,需要把选择菜单放在普通消息组件中,用户选择后再触发Modal:
// 第一步:发送带选择菜单的消息 const selectMenuRow = new client.discord.ActionRowBuilder().addComponents( new client.discord.SelectMenuBuilder() .setCustomId('verification-select') .setPlaceholder('Select an option') .addOptions([ { label: 'Select me', description: 'This is a description', value: 'first_option' }, { label: 'You can select me too', description: 'This is also a description', value: 'second_option' }, ]) ); await interaction.reply({ components: [selectMenuRow], ephemeral: true }); // 监听选择菜单的交互事件 const collector = interaction.channel.createMessageComponentCollector({ time: 60000 }); collector.on('collect', async i => { if (i.customId === 'verification-select') { // 第二步:打开模态框让用户输入文本 const modal = new client.discord.ModalBuilder() .setCustomId('verification-modal') .setTitle('Verify yourself') .addComponents([ new client.discord.ActionRowBuilder().addComponents( new client.discord.TextInputBuilder() .setCustomId('verification-input') .setLabel('Answer') .setStyle(client.discord.TextInputStyle.Short) .setMinLength(4) .setMaxLength(12) .setPlaceholder('ABCDEF') .setRequired(true), ), ]); await i.showModal(modal); collector.stop(); } });
内容的提问来源于stack exchange,提问作者JohannC
相关产品推荐
相关产品推荐

