当有人对消息做出反应时,我如何让我的discord.js机器人添加角色?

时间:2020-07-31 15:24:07

标签: 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.`)
        message.channel.send(embed).then(sentMessage => {
            sentMessage.react("?")
            .then(() => sentMessage.react("⚠️"))
            .catch(() => console.error('One of the emojis failed to react.'));
        })
        const collector = sentMessage.createReactionCollector();
        collector.on('collect', (reaction, user) => {
            if (reaction.emoji.name === '?'){
                role = message.guild.roles.find(role => role.name === "ne1 here");
                message.member.addRole(role);
            }
            else if (reaction.emoji.name === '⚠️'){
                role = message.guild.roles.find(role => role.name === "HR notifications");
                message.member.addRole(role);
            }
        })
    }
});

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

显示const collector = sentMessage.createReactionCollector();的部分不起作用。如果有人可以帮助我,那就太好了。

1 个答案:

答案 0 :(得分:0)

我只是想出了如何使其运行。这是您要使用的代码(这是我的全部代码。如果需要,请删除一些部分)。

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);

我希望这是有用的。