【问题标题】:Auto remove users reactions自动删除用户反应
【发布时间】:2020-08-29 21:20:18
【问题描述】:

我正在使用下面的代码。该代码有效,但我想要它,所以只要用户对它做出反应,表情符号反应就会在他们对它做出反应时重置,如果这甚至有意义的话。

有人知道最好的方法吗?或者如果我需要做其他事情来代替?

if(message.embeds)
        {
            const embedMsg = message.embeds.find(msg => msg.title === 'Select the games that you play (you can choose multiple)');
            if(embedMsg)
            {
                message.react('746208061550624860')
                .then(reaction => reaction.message.react('746203917272219891'))
                .then(reaction => reaction.message.react('746207267728392232')
                .catch(err => console.error);
            }
        }
        return;
    }

    if(message.content.toLowerCase() === '?roles')
    {
        message.delete()
        if(message.member.hasPermission('MANAGE_MESSAGES')) {
        const embed = new Discord.MessageEmbed();
        embed.setTitle('Select the games that you play (you can choose multiple)')
        embed.setColor(colors.blue);
        embed.setDescription(
        "NOTE: When you select any of these you will have access to the community chat of the game\n"+
        "<:cod:746208061550624860> - COD\n" +
        "<:valoran:746203917272219891> - VALORAN\n" +
        "<:warframe:746208599021191190> - WARFRAME\n");
        message.channel.send(embed)
        
    } else {
        message.reply("You don't have permission to use this command.");
    }
    }
});

bot.on('messageReactionAdd', (reaction, user) => {
    if(user.bot)
    return;

    var roleName = reaction.emoji.name;
    var role = reaction.message.guild.roles.cache.find(role => role.name.toLowerCase() === roleName.toLowerCase());
    var member = reaction.message.guild.members.cache.find(member => member.id === user.id);

    if(member.roles.cache.has(role.id))
    {
        member.roles.remove(role.id).then(member => {
            console.log("Removed" + member.user.username + "from the" + role.name + "name.");
        }).catch(err => console.error);
    } 
    else {
        member.roles.add(role.id).then(member => {
            console.log('Added' + member.user.username + 'to the' + role.name + ' role.');
        }).catch(err => console.error);
    }
    ```

【问题讨论】:

    标签: discord.js bots


    【解决方案1】:

    使用remove() 方法假设您将user 定义为做出反应的人 reaction.users.remove(user);

    【讨论】:

      【解决方案2】:
      message.reactions.cache.get('ur_role_id').remove().catch(error => console.error('Failed to remove reactions: ', error));
      

      【讨论】:

        【解决方案3】:

        你可以使用MessageReaction.remove():

        reaction.remove(user)
        

        另外,你把这行代码复杂化了:

        var member = reaction.message.guild.members.cache.find(member => member.id === user.id);
        

        相反,您可以使用Guild.member() 函数:

        var member = reaction.message.guild.member(user);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-06-04
          • 2021-01-05
          • 2022-06-15
          • 2021-09-22
          • 1970-01-01
          • 2020-03-21
          • 1970-01-01
          • 2022-01-21
          相关资源
          最近更新 更多