Discord JS //通过响应消息来尝试添加角色和删除角色

时间:2020-04-21 02:33:47

标签: discord discord.js

我正在尝试为我的不和谐机器人编写角色反应。当前,我现在停留在如何做到这一点上,以便多个人可以对消息做出反应以接收一个角色或删除一个角色。我也希望嵌入消息是恒定消息。

任何指导都将对您有所帮助,因为我已经尝试将其编码几天,而我却无法理解。

这是我当前的代码:

const Discord = require("discord.js");
const colors = require("../colors");
const botconfig = require("../botconfig")

module.exports.run = async (bot, message, args) => {


    const eRole = message.guild.roles.get('688477690344374283'); // Events
    const uRole = message.guild.roles.get('688477558982836344'); // Updates
    const pRole = message.guild.roles.get('688477868078137369'); // Polls
    const smRole = message.guild.roles.get('687498488295981069'); // Social Media
    const qRole = message.guild.roles.get('688477467840872452'); // QOTD

   const filter = (reaction, user) => ['❤️', '?', '?', '?','?'].includes(reaction.emoji.name) && user.id === message.author.id;

  let embed = new Discord.RichEmbed()
        .setTitle('Pinged Roles')
        .setDescription(`

      ❤️ ${eRole.toString()}
      ? ${uRole.toString()}
      ? ${pRole.toString()}
      ? ${smRole.toString()}
      ? ${qRole.toString()}
        `)
        .addField("**TIP:**", "Double react to remove a role")
        .setColor(colors.aqua)
        .setTimestamp()
        .setFooter(`mc.advancius.net`,bot.user.displayAvatarURL)

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

        await msg.react('❤️');
        await msg.react('?');
        await msg.react('?');
        await msg.react('?');
        await msg.react('?');

      msg.awaitReactions(filter, {

        max:1,


        }).then(collected => {

            const reaction = collected.first();

            switch (reaction.emoji.name) {
                case '❤️':
                    if (message.member.roles.has(eRole.id)) {
                        return message.channel.send('You are already in this role!').then(m => m.delete(3000));
                    }
                    message.member.addRole(eRole).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 **${eRole.name}** role!`).then(m => m.delete(3000));

                    break;
                case '?':
                    if (message.member.roles.has(uRole.id)) {

                        return message.channel.send('You are already in this role!').then(m => m.delete(3000));
                    }
                    message.member.addRole(uRole).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 **${uRole.name}** role!`).then(m => m.delete(3000));

                    break;
                case '?':
                    if (message.member.roles.has(pRole.id)) {

                        return message.channel.send('You are already in this role!').then(m => m.delete(3000));
                    }
                    message.member.addRole(pRole).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 **${pRole.name}** role!`).then(m => m.delete(3000));

                    break;
              case '?':
                    if (message.member.roles.has(smRole.id)) {

                        return message.channel.send('You are already in this role!').then(m => m.delete(3000));
                    }
                    message.member.addRole(smRole).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 **${smRole.name}** role!`).then(m => m.delete(3000));

                    break;
              case '?':
                    if (message.member.roles.has(qRole.id)) {
                        return message.channel.send('You are already in this role!').then(m => m.delete(3000));
                    }
                    message.member.addRole(qRole).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 **${qRole.name}** role!`).then(m => m.delete(3000));

                    break;
          };
      });
  })             
};

exports.help = {
    name: 'roles2'
};

1 个答案:

答案 0 :(得分:0)

我建议您将消息的对象或ID而不是收集器保存为JSON,并侦听messageReactionAddmessageReactionRemove事件以检查用于添加和删除角色的表情符号。

示例:

bot.on("messageReactionAdd", (reaction, user) => {

     if (reaction.message.id != "stored_msg_id") return; // or you can compare the message objects

     const eRole = message.guild.roles.get('688477690344374283');
     switch (reaction.emoji.name) { 
          case "❤️": reaction.message.guild.member(user).addRole(eRole); break;
          // etc.
     }
});

根据您的discord.js版本,情况可能会发生变化