NodeJS Discord.js v14单击时交互失败,按钮仍然有效

wswtfjt7  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(146)

请帮帮忙
const { Client,Message,MessageEmbed,MessageActionRow,MessageSelectMenu } = require('discord.js');
module.exports = { name:'help',说明:“帮助命令”,别名:【'h'】,表情:'|',userperm:['SEND_MESSAGES'],botperm:['SEND_MESSAGES'],/***@param {Client} client * @param {Message} message * @param {String[]} args */ run:async(client,message,args)=> { try { const emojis = { config:''️,开发者:'🔧',好玩:'😅',游戏:'🎮',公会:'📫',信息:'️',练级:'',节制:''️,所有者:'👑',搜索:'🔍',用户:'👤',实用程序:'📀',欢迎:“👋”,}; const directories = [...new Set(client.commands.map(cmd => cmd.directory))]; const formatString = str => ${str[0].toUpperCase()}${str.slice(1).toLowerCase()} ;

const categories = directories.map(dir => {
            const getCommands = client.commands
                .filter(cmd => cmd.directory === dir)
                .map(cmd => {
                    return {
                        name: cmd.name || 'No Name',
                        description: cmd.description || 'No Description Provided',
                        emoji: cmd.emoji || '',
                    };
                });

            return {
                directory: formatString(dir),
                commands: getCommands,
            };
        });

        const color = message.guild.me.displayHexColor;
        const embed = new MessageEmbed()
            .setTitle('Help Desk')
            .setThumbnail(client.user.displayAvatarURL({ size: 512 }))
            .setDescription(
                'Please choose a category in the dropdown menu!\n\nFor information about usage, or something about commands, You can [Join Server](#) for more info!'
            )
            .setColor(color);

        const components = state => [
            new MessageActionRow().addComponents(
                new MessageSelectMenu()
                    .setCustomId('help-menu')
                    .setPlaceholder('Please select a category')
                    .setDisabled(state)
                    .addOptions(
                        categories.map(cmd => {
                            return {
                                label: cmd.directory,
                                value: cmd.directory.toLowerCase(),
                                description: `Commands from ${cmd.directory} category`,
                                emoji: { name: emojis[cmd.directory.toLowerCase()] || null },
                            };
                        })
                    )
            ),
        ];

        const initialMessage = await message.channel.send({
            embeds: [embed],
            components: components(false),
        });

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

        const collector = message.channel.createMessageComponentCollector({
            filter,
            componentType: 'SELECT_MENU',
            max: 13,
        });

        collector.on('collect', interaction => {
            const [directory] = interaction.values;
            const category = categories.find(x => x.directory.toLowerCase() === directory);

            const categoryEmbed = new MessageEmbed()
                .setTitle(`${emojis[directory.toLowerCase()]} ${formatString(directory)} Commands`)
                .setThumbnail(client.user.displayAvatarURL({ dynamic: 512 }))
                .setDescription(`Here are the list of commands!`)
                .setColor(color)
                .addFields(
                    category.commands.map(cmd => {
                        return {
                            name: `${cmd.emoji} \`${cmd.name}\``,
                            value: cmd.description,
                            inline: true,
                        };
                    })
                )
                .setTimestamp();

            interaction.update({ embeds: [categoryEmbed] });
        });
    } catch (err) {
        console.log(err);
        message.channel.send({
            content: 'Uh oh! Something unexcepted. Try to running command again!',
        });
    }
},

};
我不知道错误在哪里

qncylg1j

qncylg1j1#

你需要推迟更新,当一个按钮被点击它更新,所以你必须做

// Your code ...
collector.on('collect', async interaction => {
  await interaction.deferUpdate();
// Your code ...
});

// Your code ...

添加await interaction.deferUpdate();后,不应该说交互失败,还有一件重要的事情,这一行必须是collect事件侦听器的第一行,因为在更新或发送任何内容之前,它需要被延迟,因为交互限制为3秒

相关问题