【问题标题】:How would I get my discord.js bot to add a role when someone reacts to a message?当有人对消息做出反应时,我如何让我的 discord.js 机器人添加角色?
【发布时间】:2020-11-21 11:18:20
【问题描述】:

现在,我正在尝试制作一个反应角色机器人,但我不断收到错误消息。这是我的代码。

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(); 的部分不起作用。如果有人可以帮我解决这个问题,那就太好了。

【问题讨论】:

    标签: bots discord.js


    【解决方案1】:

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

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

    我希望这是有用的。

    【讨论】:

      猜你喜欢
      • 2020-12-05
      • 2020-12-13
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 2021-07-02
      • 2021-11-13
      • 1970-01-01
      • 2021-08-14
      相关资源
      最近更新 更多