Discord.js 从反应中获取用户 **已更新**

时间:2020-12-18 21:03:12

标签: javascript node.js discord.js bots

更新

我已经更新了当前对我有用的代码,它现在将创建频道,然后检查频道是否已创建。它只允许 1 个用户创建频道,您将无法以同一用户身份创建 T5 频道和 T4 频道。

谢谢你们,希望你们都有一个美好的圣诞节。

Client.on("message", async (message) => {
  if (message.author.Client || message.channel.type === "dm") return;

  let messageArray = message.content.split(" ");
  let cmd = messageArray[0];
  let args = message.content.substring(message.content.indexOf(" ") + 1);

  if (cmd === `${prefix}battle`) {
    let embed = new Discord.MessageEmbed()
      .setTitle("⚔️ 1657 Battles! ⚔️")
      .setDescription("React to this message to join the battle.")
      .addFields(
        { name: "?", value: "T5" },
        { name: "?", value: "T4" },
        { name: "?", value: "T3" },
        { name: "?", value: "T2" },
        { name: "?", value: "T1" }
      )
      .setColor("#0099ff")
      .setTimestamp()
      .setFooter("Please pick the troop type you want to battle with!");

    let msgEmbed = await message.channel.send(embed);
    msgEmbed.react("?"); // T5
    msgEmbed.react("?"); // T4
    msgEmbed.react("?"); // T3
    msgEmbed.react("?"); // T2
    msgEmbed.react("?"); // T1
  }
});

Client.on("messageReactionAdd", async (reaction, user, message) => {
  //Add an event listener
  if (reaction.message.partial) await reaction.message.fetch();
  if (user.id === Client.user.id) return; //If the reaction was from the bot, return
  if (!reaction.message.guild) return; //If the reaction was not in a guild
  const guild = Client.guilds.cache.get("644295524404428832");
  if (
    guild.channels.cache.find((channel) => channel.name === "t5-battle-channel")
  )
    return;

  if (reaction.emoji.name === "?") {
    let guild = reaction.message.guild;
    guild.channels.create("T5 Battle Channel", {
      //Creating the channel
      type: "text", //Make sure the channel type is text
      permissionOverwrites: [
        //Set overwrites
        {
          id: guild.id,
          deny: "VIEW_CHANNEL",
        },
        {
          id: "788400016736780338",
          allow: ["VIEW_CHANNEL"],
        },
      ],
    });
  }
  if (
    guild.channels.cache.find((channel) => channel.name === "t4-battle-channel")
  )
    return;

  if (reaction.emoji.name === "?") {
    let guild = reaction.message.guild;
    guild.channels.create("T4 Battle Channel", {
      //Creating the channel
      type: "text", //Make sure the channel type is text
      permissionOverwrites: [
        //Set overwrites
        {
          id: guild.id,
          deny: "VIEW_CHANNEL",
        },
        {
          id: "788400619114463275",
          allow: ["VIEW_CHANNEL"],
        },
      ],
    });
  }
  if (
    guild.channels.cache.find((channel) => channel.name === "t3-battle-channel")
  )
    return;

  if (reaction.emoji.name === "?") {
    let guild = reaction.message.guild;
    guild.channels.create("T3 Battle Channel", {
      //Creating the channel
      type: "text", //Make sure the channel type is text
      permissionOverwrites: [
        //Set overwrites
        {
          id: guild.id,
          deny: "VIEW_CHANNEL",
        },
        {
          id: "788400701130670110",
          allow: ["VIEW_CHANNEL"],
        },
      ],
    });
  }
  if (
    guild.channels.cache.find((channel) => channel.name === "t2-battle-channel")
  )
    return;

  if (reaction.emoji.name === "?") {
    let guild = reaction.message.guild;
    guild.channels.create("T2 Battle Channel", {
      //Creating the channel
      type: "text", //Make sure the channel type is text
      permissionOverwrites: [
        //Set overwrites
        {
          id: guild.id,
          deny: "VIEW_CHANNEL",
        },
        {
          id: "788400738727624704",
          allow: ["VIEW_CHANNEL"],
        },
      ],
    });
  }
  if (
    guild.channels.cache.find((channel) => channel.name === "t1-battle-channel")
  )
    return;

  if (reaction.emoji.name === "?") {
    let guild = reaction.message.guild;
    guild.channels.create("T1 Battle Channel", {
      //Creating the channel
      type: "text", //Make sure the channel type is text
      permissionOverwrites: [
        //Set overwrites
        {
          id: guild.id,
          deny: "VIEW_CHANNEL",
        },
        {
          id: "788400784420372490",
          allow: ["VIEW_CHANNEL"],
        },
      ],
    });
  }
});

2 个答案:

答案 0 :(得分:3)

如果您想获得对消息的所有反应,可以在 reactions 上使用 message 属性(如果您已经拥有消息对象)。这将返回一个您可以使用的 ReactionManager

答案 1 :(得分:1)

您应该使用 reaction collector

<块引用>

它们的工作方式与消息收集器非常相似,除了您 将它们应用于消息而不是频道。以下是一个 示例取自文档,变量稍好 名称以供澄清。过滤器将检查 ? 表情符号 - in 特别是默认肤色,所以要小心。它也会 检查做出反应的人是否与作者拥有相同的 ID 收集器被分配到的原始消息。

const filter = (reaction, user) => {
    return reaction.emoji.name === '?' && user.id === message.author.id;
};

const collector = message.createReactionCollector(filter, { time: 15000 });

collector.on('collect', (reaction, user) => {
    console.log(`Collected ${reaction.emoji.name} from ${user.tag}`);
});

collector.on('end', collected => {
    console.log(`Collected ${collected.size} items`);
});

阅读它的文档 here

<块引用>

我还想知道是否可以将每个对特定角色做出反应的用户随机配对。

collected 属性对于此任务应该很方便。 至于向 GuildMember 添加一个/多个角色,请使用 add 方法。了解如何向成员 here 添加角色。