如何根据消息反应添加角色?

时间:2020-01-19 19:42:41

标签: javascript bots discord discord.js

此代码是否存在任何错误? 机器人发送消息后,不会弹出错误,也没有反应。 任何帮助将不胜感激,在此先感谢您!

const a = msg.guild.roles.get('666712822985654322'); //Verified User
// the role constants are in the same chronological order as below.
const filter = (reaction,user) => ['668236468384169986'].includes(reaction.emoji.name);

const embed = new Discord.RichEmbed()
  .setColor(0x00FF00)
  .setTitle('Rules')
  .setDescription(`
    In order to gain access to the rest of the server you must read and abide by these rules: 
    By reacting with :white_check_mark: you agree to these rules
    Roles:
    :white_check_mark: ${a.toString()}`)
  .setThumbnail(msg.author.avatarURL)
  .addField('Rule #1:You do not talk about fight club', 'Second Rule: You do not TALK about fight club')
  .setFooter("Use \'!command list\' to get aquainted with Peb 3000");

  msg.channel.send(embed).then(async message => {

    await message.react('668236468384169986'); //white check mark

    message.awaitReaction(filter, {})
      .then(collected =>{

        const reaction = collected.first();

        switch(reaction.emoji.name) {
          case('\:white_check_mark:'):
            message.member.addRole(a).catch(err => {
              console.log(err);
              return message.channel.send(`Error adding you to this role: **${err.message}**`);
            });
            message.channel.send(`You have been added to the **${a.name}** role!`).then(m => m.delete(3000));
            break;
        }
      }).catch(collected => {
        return msg.collected.send(`I couldn't add you to this role!`)
      })
});

2 个答案:

答案 0 :(得分:0)

我建议您同时阅读An Idiot's Guide - Using EmojisDiscord.js Guide - Reactions,因为您当前的Unicode表情符号方法行不通。

在您的情况下,:white_check_mark:应该是或等效值。

答案 1 :(得分:0)

收集器结束收集时,收集的选项将触发,您未提供任何停止收集器的选项,因此永远不会发生。 在响应中赋予成员角色的1种方法遵循此代码。但是,当bot重新启动时,您需要再次使用此命令并创建收集器。

const roleToAdd = msg.guild.roles.get('666712822985654322'); //Verified Role
if(!roleToAdd) return

          let embed = new Discord.RichEmbed()
            .setColor(0x00FF00)
            .setTitle('Rules')
            .setDescription(`
            In order to gain access to the rest of the server you must read and abide by these rules: 
            By reacting with :white_check_mark: you agree to these rules
            Roles:
            :white_check_mark: ${a.toString()}`)
            .setThumbnail(msg.author.avatarURL)
            .addField('Rule #1:You do not talk about fight club', 'Second Rule: You do not TALK about fight club')
            .setFooter("Use \'!command list\' to get aquainted with Peb 3000");
            msg.channel.send(embed).then(message => {
                const filter = (reaction, user) => reaction.emoji.name === '✅' && user.id === msg.author.id && reaction.message.id = message.id
                const collector = message.createReactionCollector(filter, {});
                collector.on('collect', r => {
                    if(r.emoji.name === '✅') {
                        let member = message.guild.members.get(reaction.users.last().id)
                        member.addRole(roleToAdd)
                        .then( member => {
                            message.channel.send(`You have been added to the **${roleToAdd.name}** role!`).then(m => m.delete(3000));
                        })
                        .catch(console.error)
                    }
                })
                collector.on('end', collected => console.log(`Collected ${collected.size} items`));
            })

方法2的监听反应添加事件。重新启动后即可使用。

    bot.on('messageReactionAdd', (reaction, user) => {
        if(reaction.message.id === 'YOUR MESSAGE ID' && reaction.emoji.name === 'youreactionname') {
let meber = reaction.message.guild.members.get(user.id)
member.addRole('yourRole')
} 

        .catch(console.error)
      });