渴望通过反应发挥作用

时间:2019-01-13 22:55:28

标签: javascript discord.js

if(!message.content.startsWith(config.prefix)) return;

const FPP = message.guild.roles.find(FPP => FPP.name === "FPP");
const TPP = message.guild.roles.find(TPP => TPP.name === "TPP");

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


if(command === "roles") {
const embed = new RichEmbed()
  .setTitle('Verfügbare Rollen')
  .setDescription(`

    ${FPP.toString()}
    ${TPP.toString()}

  `)
  .setColor(0xdd9323)
  .setFooter(`ID: ${message.author.id}`);

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

    await msg.react('');
    await msg.react('');

    msg.awaitReactions(filter, {
      max: 5,
      time: 15000,
      error: ['time']
    }).then(collected => {

      const reaction = collected.first();

      switch (reaction.emoji.name) {
        case '':
            message.member.addRole(FPP).catch(err => {
              console.log(err);
              return message.channel.send(`Error: Rolle konnte nicht hinzugefügt werden. Wende dich bitte an den Admin dieses Servers.`)
            });
            message.channel.send(`Du hast dir die Rolle **${FPP.name}** hinzugefügt`).then(m => m.delete(3000));
            break;
        case '':
            message.member.addRole(TPP).catch(err => {
             console.log(err);
             return message.channel.send(`Error: Rolle konnte nicht hinzugefügt werden. Wende dich bitte an den Admin dieses Servers.`)
            });
            message.channel.send(`Du hast dir die Rolle **${TPP.name}** hinzugefügt`).then(m => m.delete(3000));
            break;
      }

    }).catch(collected => {
      return message.channel.send(`Konnte dich der Rolle nicht hinzufügen.`)
    })

  })
}

这是我正在处理的命令。不知何故,但效果并不理想。 我希望Discord上的用户可以为其分配角色。 目前,只有使用此命令的用户才能通过反应分配自己的角色。

可能是因为这一行。

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

由于某种原因,该角色只能在30秒而不是即时之后分配。

但是我真正想要的是对这个命令做出反应的每个人都扮演这个角色。不仅是作者。我已经试图摆脱这一部分。但是随后该命令不再起作用。

知道我能做什么吗?

1 个答案:

答案 0 :(得分:2)

这仅用于在单个服务器上使用。您需要对其进行修改以供多服务器使用。

<CLient>.on("messageReactionAdd", (reaction, user) => {
  if (user.bot) return;
  const member = reaction.message.member
  switch (reaction.name) {
    case "emoji_name_1":
    member.addRole("roleID").then((res) => {
      // You can do something like this, or nothing at all. Your choice.
      reaction.message.channel.send(`You've been given the \`${res.name}\` role!`)
    }).catch(console.error);
    break;
    case "emoji_name_2":
    member.addRole("someOtherRole").then((res) => {
      reaction.message.channel.send(`You've been given the \`${res.name}\` role!`)
    }).catch(console.error);
  };
})

<CLient>.on("messageReactionRemove", (reaction, user) => {
  if (user.bot) return;
  const member = reaction.message.member
  switch (reaction.name) {
    case "emoji_name_1":
    member.removeRole("roleID").then((res) => {
      // You can do something like this, or nothing at all. Your choice.
      reaction.message.channel.send(`You've been removed from the \`${res.name}\` role!`)
    }).catch(console.error);
    break;
    case "emoji_name_2":
    member.removeRole("someOtherRole").then((res) => {
      reaction.message.channel.send(`You've been removed from the \`${res.name}\` role!`)
    }).catch(console.error);
  };
})

当然,这实际上是最基本的,因此您仍然需要进行修改。它会检查是否添加/删除了反应,而不仅仅是在一条消息上。我建议存储消息ID或要检查的内容。如果您按照自己的方式进行操作,除非您希望这样做,否则它在机器人掉下来并备份后不会跟踪反应。