繁体   English   中英

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

[英]How do you add a role based on a message reaction?

这段代码有什么远程错误吗? 没有错误弹出,一旦机器人发送消息就没有反应。 任何帮助将不胜感激,在此先感谢您!

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!`)
      })
});

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

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

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

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

方法二是监听reactionadd事件。 重新启动后它会工作。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM