Discord.js反应角色收集器开关

时间:2020-03-25 16:51:54

标签: discord.js

你好Stackoverflow社区!

我正在尝试创建的反应收集器开关有一些问题,并被卡住了。我想要这段代码很简单;当人们对一种颜色做出反应时,他们将获得颜色角色,他们的反应将被删除,因此,如果他们想要更改,他们只会对一种新颜色做出反应,依此类推。 但是我似乎无法使其正常工作。我没有收到任何错误消息,我只是没有任何角色。先感谢您!!非常感谢!

代码:

const Discord = require("discord.js");

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

const RandomColour = ("000000" + Math.random().toString(16).slice(2, 8).toUpperCase()).slice(-6);

    message.delete();

    const black = message.guild.roles.get("595509263754067969");
    const white = message.guild.roles.get("595510495902040074");
    const magenta = message.guild.roles.get("595510495902040074");
    const purple = message.guild.roles.get("595507863355654154");
    const red = message.guild.roles.get("595506989212106802");
    const orange = message.guild.roles.get("595507331396009986");
    const yellow = message.guild.roles.get("595507180183093248");
    const green = message.guild.roles.get("595507124574879744");
    const blue = message.guild.roles.get("595507043629268993");

    const filter = (reaction, user) => reaction.emoji.name === [
      'black' , 
      'white' , 
      'magenta' , 
      'purple' , 
      'red' , 
      'orange' , 
      'yellow' , 
      'green' , 
      'blue'].includes(reaction.emoji.name) && user.id === message.member.id;

    const embed = new Discord.RichEmbed()
        .setAuthor("Available Roles")
        .setDescription(`

        Black       ${black.toString()}   | ⬛
        White       ${white.toString()}   | ⬜
        Magenta     ${magenta.toString()} | ?
        Purple      ${purple.toString()}  | ?
        Red         ${red.toString()}     | ?
        Orange      ${orange.toString()}  | ?
        Yellow      ${yellow.toString()}  | ?
        Green       ${green.toString()}   | ?
        Blue        ${blue.toString()}    | ?

        `)
        .setFooter("React on the right emoji to get a colour.")
        .setColor(RandomColour)
        .setTimestamp()

    message.channel.send(embed).then(async function (message) {

      await message.react("⬛");
      await message.react("⬜");
      await message.react("?");
      await message.react("?");
      await message.react("?");
      await message.react("?");
      await message.react("?");
      await message.react("?");
      await message.react("?");

      message.awaitReactions(filter).then(async function (collected) {

        const reaction = collected.first();

        switch (reaction.emoji.name) {
          case 'black':
            if (message.member.roles.has(black.id)) {
              return;
            } else {
              message.member.addRole(black).catch(err => {
                console.log(err);
                return message.member.send(`Error adding you to this role: **${err}**`)
                })
              message.member.send(`You've reacted on ⬛ and gained then **${black.name} role!`)
            }
              break;
          case 'white':
            if (message.member.roles.has(white.id)) {
              return;
            } else {
              message.member.addRole(white).catch(err => {
                console.log(err);
                return message.member.send(`Error adding you to this role: **${err}**`)
              })
              message.member.send(`You've reacted on ⬜ and gained then **${white.name} role!`)
            }
              break;
          case 'magenta':
            if (message.member.roles.has(magenta.id)) {
              return;
            } else {
              message.member.addRole(magenta).catch(err => {
                console.log(err);
                return message.member.send(`Error adding you to this role: **${err}**`)
              })
              message.member.send(`You've reacted on ? and gained then **${magenta.name} role!`)
            }
              break;
          case 'purple':
            if (message.member.roles.has(purple.id)) {
              return;
            } else {
              message.member.addRole(purple).catch(err => {
                console.log(err);
                return message.member.send(`Error adding you to this role: **${err}**`)
              })
              message.member.send(`You've reacted on ? and gained then **${purple.name} role!`)
            }
              break;
          case 'red':
            if (message.member.roles.has(red)) {
              return;
            } else {
              reaction.message.member.addRole(red).catch(err => {
                console.log(err);
                return message.member.send(`Error adding you to this role: **${err}**`)
              })
              message.member.send(`You've reacted on ? and gained then **${red.name} role!`)
            }
              break;
          case 'orange':
            if (message.member.roles.has(orange.id)) {
              return;
            } else {
              message.member.addRole(orange).catch(err => {
                console.log(err);
                return message.member.send(`Error adding you to this role: **${err}**`)
              })
              message.member.send(`You've reacted on ? and gained then **${orange.name} role!`)
            }
              break;
          case 'yellow':
            if (message.member.roles.has(yellow.id)) {
              return;
            } else {
              message.member.addRole(yellow).catch(err => {
                console.log(err);
                return message.member.send(`Error adding you to this role: **${err}**`)
              })
              message.member.send(`You've reacted on ? and gained then **${yellow.name} role!`)
            }
              break;
          case 'green':
            if (message.member.roles.has(green.id)) {
              return;
            } else {
              message.member.addRole(green).catch(err => {
                console.log(err);
                return message.member.send(`Error adding you to this role: **${err}**`)
              })
              message.member.send(`You've reacted on ? and gained then **${green.name} role!`)
            }
              break;
          case 'blue':
            if (message.member.roles.has(blue.id)) {
              return;
            } else {
              message.member.addRole(blue).catch(err => {
                console.log(err);
                return message.member.send(`Error adding you to this role: **${err}**`)
              })
              message.member.send(`You've reacted on ? and gained then **${blue.name} role!`)
            }
              break;
      }
    }).catch(collected => {
      return message.channel.send("I couldn't add you to this role. If this keeps occouring, contact staff.")
  }).catch(err => console.log(err))
}).catch(err => console.log(err))

}

module.exports.help = {
  name: "reactroles",
  aliases: []
}

1 个答案:

答案 0 :(得分:1)

问题是reaction.emoji.name === unicode reaction name 因此,白色将是⬜,而不是white

一种方法是没有hardCode,只需创建带有一些参数的array,然后检查它:)

const Discord = require("discord.js");

module.exports.run = async (bot, message, args, client) => {
 const reactionRoles = [
        {
            name: 'white',
            reaction: '⬜',
            reactionRole: message.guild.roles.get('595510495902040074'),
        },
        {
            name: 'black',
            reaction: '⬛',
            reactionRole: message.guild.roles.get('595509263754067969'),
        },
    ];
    const RandomColour = Math.floor(Math.random() * Math.floor(16777215));
    const embed = new Discord.RichEmbed()
        .setAuthor('Available Roles')
        .setDescription(`${reactionRoles.map(r => `${r.name} | ${r.reactionRole.name} | ${r.reaction}`).join('\n')}`)
        .setFooter('React on the right emoji to get a colour.')
        .setColor(RandomColour)
        .setTimestamp();
    message.channel
        .send(embed)
        .then(async msg => {
            for (const r of reactionRoles) {
                await msg.react(r.reaction);
            }
            const filter = (reaction, user) => {
                return reactionRoles.map(r => r.reaction).includes(reaction.emoji.name) && user.id === message.author.id;
            };
            msg.awaitReactions(filter, { max: 1 })
                .then(collected => {
                    const reaction = collected.first();
                    let role = reactionRoles.find(r => r.reaction === reaction.emoji.name);
                    if (message.member.roles.has(role.reactionRole)) return message.reply('You are already has this role');
                    message.member.addRole(role.reactionRole);
                })
                .catch(collected => {
                    return message.channel.send("I couldn't add you to this role. If this keeps occouring, contact staff.");
                })
                .catch(err => console.log(err));
        })
        .catch(err => console.log(err));
};