根据已推送的反应/应用程序自动程序将用户添加到角色

时间:2020-07-23 18:47:48

标签: node.js discord.js

我有一个正在使用的应用程序漫游器,它将把用户的问题发送到主持人频道,我们在该频道上对申请人进行投票,因此我希望以此为基础。我希望能够根据我们单击反应为用户赋予角色。喜欢,说他们被批准了。点击反应,用户将获得所需的角色。

这是我的代码:

const { MessageEmbed } = require("discord.js");
module.exports.run = async (bot, message, args) => {
  let userApplications = {};

  bot.on("message", function (message) {
    if (message.author.equals(bot.user)) return;

    let authorId = message.author.id;

    if (message.content === "!app") {
      console.log(`Apply begin for authorId ${authorId}`);
      // User is not already in a registration process
      if (!(authorId in userApplications)) {
        userApplications[authorId] = { step: 1 };

        message.author.send(
          "```We need to ask some questions so  we can know a litte bit about yourself```"
        );
        message.author.send(
          "```What is your light level and the total amount of hours you have played destiny?``` https://wastedondestiny.com/"
        );
      }
    } else {
      if (message.channel.type === "dm" && authorId in userApplications) {
        let authorApplication = userApplications[authorId];

        if (authorApplication.step == 1) {
          authorApplication.answer1 = message.content;
          message.author.send("```Whats is your timezone?```");
          authorApplication.step++;
        } else if (authorApplication.step == 2) {
          authorApplication.answer2 = message.content;
          message.author.send(
            "```How many days of the week do you play destiny?```"
          );
          authorApplication.step++;
        } else if (authorApplication.step == 3) {
          authorApplication.answer3 = message.content;
          message.author.send(
            "```What raids have you completed and how many times?``` https://raid.report/"
          );
          authorApplication.step++;
        } else if (authorApplication.step == 4) {
          authorApplication.answer4 = message.content;
          message.author.send(
            "```Do you belive in helping others even if you get nothing in return?```"
          );
          authorApplication.step++;
        } else if (authorApplication.step == 5) {
          authorApplication.answer5 = message.content;
          message.author.send(
            "```What is your reason for wanting to join FearTheWise?```"
          );
          authorApplication.step++;
        } else if (authorApplication.step == 6) {
          authorApplication.answer6 = message.content;
          message.author.send("```What is your PSN?```");
          authorApplication.step++;
        } else if (authorApplication.step == 7) {
          authorApplication.answer7 = message.content;
          message.author.send("```Are you over 18 years of age?```");
          authorApplication.step++;
        } else if (authorApplication.step == 8) {
          authorApplication.answer8 = message.content;
          message.author.send(
            "```Thanks for your registration. Type %apply to register again```"
          );

          let embed = new MessageEmbed()
            .setTitle(
              "<:fearthewise:637422744610537492>-------Clan Application-------<:fearthewise:637422744610537492>"
            )
            .setColor("#ffd700")
            .setThumbnail("https://i.imgur.com/eCYcee3.png")
            .addFields({
              name:
                "What is your light level and the total amount of hours you have played destiny? https://wastedondestiny.com/ ",
              value: `${authorApplication.answer1}`,
            })
            .addField("\u200b")
            .addFields({
              name: "Whats is your timezone? ",
              value: `${authorApplication.answer2}`,
            });
          ​.addFields({
            name: "How many days of the week do you play destiny?",
            value: `${authorApplication.answer3}`,
          });
          ​.addFields({
            name:
              "What raids have you completed and how many times? https://raid.report/",
            value: `${authorApplication.answer4}`,
          });
          ​.addFields({
            name:
              "Do you belive in helping others even if you get nothing in return?",
            value: `${authorApplication.answer5}`,
          });
          ​.addFields({
            name: "What is your reason for wanting to join FearTheWise?",
            value: `${authorApplication.answer6}`,
          });
          ​.addFields({
            name: "What is your PSN?",
            value: `${authorApplication.answer7}`,
          });
          ​.addFields({
            name: "Are you over 18 years of age?",
            value: `${authorApplication.answer8}`,
          });
          ​.addField(
            "Applicant Username",
            `${message.author.username}#${message.author.discriminator}`
          );
          ​.setFooter("Bot created by James (Rock)₇₇₇");

          //before deleting, you can send the answers to a specific channel by ID
          bot.channels.cache
            .get("616852008837709844")
            .send(embed)
            .then(async (msg) => {
              await msg.react("?");
              await msg.react("?");
              await msg.react("?");
            });
          delete userApplications[authorId];
        }
      }
    }
  });
};

module.exports.help = {
  name: "app",
};

1 个答案:

答案 0 :(得分:0)

const filter = (reaction) =>
      ['?', '?', ?'].includes(reaction.emoji.name); // create an emoji filter

   msg
      .awaitReactions(filter, { max: 1 }) // accept one reaction,  
      .then((collected) => {

      if (collected.first().emoji.name === '?') {

         // <member>.roles.add(role)

      } else if (collected.first().emoji.name === '?') {

         // <member>.roles.add(role)
         // you get the idea

      }   
      }).catch((err) => console.log(err);

.awaitReactions() method

GuildMember object (not a user object!)的占位符。 您可以使用guild.fetchMember()或(guild.member())获取GuildMember,如果该成员已经被缓存。

要添加/删除角色,可以使用GuildMember .add() method