反应角色 (JS)

时间:2021-07-26 12:16:15

标签: javascript discord.js

所以我一直在为自己制作一个 Discord Bot,不要误会我的意思,我仍处于 JS 的学习阶段,但我知道一点点让我开始。我一直在制作一个反应角色命令,只是想知道是否有办法让它一次只能对一件事情做出反应。

例如,我有一个颜色选择器,但只希望成员一次选择 1 个,如果他们对不同的颜色做出反应,则不会对他们选择的第一种颜色做出反应

module.exports = {
    name: 'reactioncolor',
    description: "Sets up a reaction role message!",
    async execute(client, message, args, Discord) {
        const channel = '865154605356285962';
        const redTeamRole = message.guild.roles.cache.find(role => role.name === "Red");
        const orangeTeamRole = message.guild.roles.cache.find(role => role.name === "Orange");
        const yellowTeamRole = message.guild.roles.cache.find(role => role.name === "Yellow");
        const blueTeamRole = message.guild.roles.cache.find(role => role.name === "Blue");
 
        const redTeamEmoji = '?';
        const orangeTeamEmoji = '?';
        const yellowTeamEmoji = '?';
        const blueTeamEmoji = '?';
 
        let embed = new Discord.MessageEmbed()
            .setColor('#e42643')
            .setTitle('Choose your colour!')
            .setDescription('\n\n'
                + `${redTeamEmoji} ➜ <@&865163260500377602>\n`
                + `${orangeTeamEmoji} ➜ <@&865163175091503104>\n`
                + `${yellowTeamEmoji} ➜ <@&865163103469961226>\n`
                + `${blueTeamEmoji} ➜ <@&866988159162515527>\n`);
 
        let messageEmbed = await message.channel.send(embed);
        messageEmbed.react(redTeamEmoji);
        messageEmbed.react(orangeTeamEmoji);
        messageEmbed.react(yellowTeamEmoji);
        messageEmbed.react(blueTeamEmoji);
 
        client.on('messageReactionAdd', async (reaction, user) => {
            if (reaction.message.partial) await reaction.message.fetch();
            if (reaction.partial) await reaction.fetch();
            if (user.bot) return;
            if (!reaction.message.guild) return;
 
            if (reaction.message.channel.id == channel) {
                if (reaction.emoji.name === redTeamEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(redTeamRole);
                }
                if (reaction.emoji.name === orangeTeamEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(orangeTeamRole);
                }
                if (reaction.emoji.name === yellowTeamEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(yellowTeamRole);
                }
                if (reaction.emoji.name === blueTeamEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(blueTeamRole);
                }
            } else {
                return;
            }
 
        });
 
        client.on('messageReactionRemove', async (reaction, user) => {
 
            if (reaction.message.partial) await reaction.message.fetch();
            if (reaction.partial) await reaction.fetch();
            if (user.bot) return;
            if (!reaction.message.guild) return;
 
 
            if (reaction.message.channel.id == channel) {
                if (reaction.emoji.name === redTeamEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(redTeamRole);
                }
                if (reaction.emoji.name === orangeTeamEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(orangeTeamRole);
                }
                if (reaction.emoji.name === yellowTeamEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(yellowTeamRole);
                }
                if (reaction.emoji.name === blueTeamEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(blueTeamRole);
                }
            } else {
                return;
            }
        });
    }
 
}   

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

如果用户点击新的反应,您可以通过这种方式删除他们的旧反应。

client.on("messageReactionAdd", async (reaction, user) => {

    if (reaction.message.partial) await reaction.message.fetch();
    if (reaction.partial) await reaction.fetch();
    if (user.bot) return;
    if (!reaction.message.guild) return;

    // ...

    reaction.message.reactions.cache.forEach((otherReaction) => {
        if (reaction.emoji.name == otherReaction.emoji.name) return;
        if (otherReaction.users.cache.some(otherUser => user.id == otherUser.id)) {
            otherReaction.users.remove(user);
        }
    });

    // ...

}

请注意,这仅在 .cache 是最新的情况下才有效。否则,您将需要获取它们。

但正如@Viriato 建议的那样,Select Menus 绝对是最好的解决方案。