删除反应后,如何让我的discord.js机器人删除角色?

时间:2020-07-31 18:36:08

标签: bots discord.js

我有一个代码,允许用户添加响应以获取角色。但是,如果希望删除反应,我希望我的机器人删除该角色,并且我不知道该怎么做。这是我当前的代码。

const Discord = require('discord.js');
const keepAlive = require('./server');
const client = new Discord.Client();

client.on('message', message => {
    if (message.author !== null && message.author.bot) return;
    if (message.toString().toLowerCase().includes('start reaction roles')) {
        message.delete()
        const embed = new Discord.MessageEmbed()
            .setColor('#0099ff')
            .setTitle('React to get notified')
            .setDescription(`Hello! Please react to this message to get a role. Here are the roles that you can get by reacting:\n> ? | React with this emoji to get <@&727665971443269635> notifications.\n> ⚠️ | React with this emoji to get <@&727633811709362187>.\nYou can chose one of the emojis, or both of them.\n\n**NOTE:**\nCurrently, this command does not support removing your roles. If you want to remove your role, click on the emoji. Then, click on your profile, and click the X next to the role you want to remove. You can always get it back by reacting.`)
        message.channel.send(embed).then(sentMessage => {
            sentMessage.react("?").then(() => sentMessage.react("⚠️")).then(() => {
                const filter = (reaction, user) => {
                    return true;
                }
                const collector = sentMessage.createReactionCollector(filter);
                collector.on('collect', (reaction, user) => {
                    if (reaction.emoji.name === '?'){
                        role = message.guild.roles.cache.find(role => role.name === "ne1 here");
                        message.member.roles.add(role);
                    }
                    else if (reaction.emoji.name === '⚠️'){
                        role = message.guild.roles.cache.find(role => role.name === "HR notifications");
                        message.member.roles.add(role);
                    }
                })
            })
            .catch(() => console.error('One of the emojis failed to react.'));
        })
    }
});

keepAlive();
client.login(process.env.TOKEN);

如果有人可以告诉我该怎么做,那太好了。

1 个答案:

答案 0 :(得分:0)

我不知道如何仅使用discord.js做到这一点,在文档中找不到关于此的任何信息。但是您可以尝试使用dataset npm模块。并使用npm模块的this。我认为那是更快,更容易。

希望我能帮上忙。