当用户对消息做出反应时,bot 在 discord.js 中发送另一条消息

时间:2021-03-14 17:35:17

标签: javascript node.js discord discord.js bots

我想知道如何做到这一点,如果有人对特定表情符号做出反应,机器人会发送另一条消息。它用于帮助命令。我已经完成了命令本身,机器人对他自己的消息做出了反应,我还为其他消息添加了嵌入,我只是不知道如何使其工作。

这是我的代码:

const { DiscordAPIError } = require("discord.js");
const Discord = require('discord.js');
const client = new Discord.Client();

module.exports = {
  name: 'help',
  description: "Overview of all commands!",
  execute(message, args) {
    const embed = new Discord.MessageEmbed()
      .setColor("#171b20")
      .setTitle("Help")
      .setDescription("React to the message to see commands of a specific category!")
      .addFields({
        name: ":red_square:" + "   Moderation",
        value: '\u200b'
      }, {
        name: ":orange_square:" + "   Interaction",
        value: '\u200b'
      }, {
        name: ":yellow_square:" + "   Fun",
        value: '\u200b'
      }, {
        name: ":purple_square:" + "   Games",
        value: '\u200b'
      }, {
        name: ":brown_square:" + "   NSFW",
        value: '\u200b'
      }, {
        name: ":green_square:" + "   Information",
        value: '\u200b'
      })
      .setFooter(message.author.username)
      .setTimestamp();
    message.channel.send(embed).then(embedMsg => {
      embedMsg.react("?")
        .then(reaction => embedMsg.react('?'))
        .then(reaction => embedMsg.react('?'))
        .then(reaction => embedMsg.react('?'))
        .then(reaction => embedMsg.react('?'))
        .then(reaction => embedMsg.react('?'))
    })

    const moderation = new Discord.MessageEmbed()
      .setColor("#ff0000")
      .setTitle("Help")
      .setDescription("Below you can see the moderation commands.")
      .addFields({
        name: "test",
        value: "this is a test"
      })
      .setFooter(message.author.username)
      .setTimestamp();

    const interaction = new Discord.MessageEmbed()
      .setColor("#ffa500")
      .setTitle("Help")
      .setDescription("Below you can see the interaction commands.")
      .addFields({
        name: "test",
        value: "this is a test"
      })
      .setFooter(message.author.username)
      .setTimestamp();

    const fun = new Discord.MessageEmbed()
      .setColor("#ffff00")
      .setTitle("Help")
      .setDescription("Below you can see the fun commands.")
      .addFields({
        name: "test",
        value: "this is a test"
      })
      .setFooter(message.author.username)
      .setTimestamp();

    const games = new Discord.MessageEmbed()
      .setColor("#e600e6")
      .setTitle("Help")
      .setDescription("Below you can see the games commands.")
      .addFields({
        name: "test",
        value: "this is a test"
      })
      .setFooter(message.author.username)
      .setTimestamp();

    const nsfw = new Discord.MessageEmbed()
      .setColor("#4d0000")
      .setTitle("Help")
      .setDescription("Below you can see the nsfw commands.")
      .addFields({
        name: "test",
        value: "this is a test"
      })
      .setFooter(message.author.username)
      .setTimestamp();

    const information = new Discord.MessageEmbed()
      .setColor("#008000")
      .setTitle("Help")
      .setDescription("Below you can see the information commands.")
      .addFields({
        name: "test",
        value: "this is a test"
      })
      .setFooter(message.author.username)
      .setTimestamp();

  }
}

1 个答案:

答案 0 :(得分:1)

您可以使用 reaction collectors。不过,我会将命令存储在一个对象数组中,这样您就可以使用更少的代码。

我创建了一个 categories 数组,其中包含基本类别、它们的表情符号、名称、颜色、标题、描述和命令。这样,您可以在任何需要的时候(例如,当机器人发送第一反应时等)遍历这个数组。

我在下面添加了一些评论:

const categories = [
  {
    emoji: '?',
    name: 'Moderation',
    color: '#ff0000',
    title: 'Help',
    description: 'Below you can see the moderation commands.',
    commands: [
      {
        name: 'Moderation 1',
        value: 'to moderate stuff',
      },
      {
        name: 'Moderation 2',
        value: 'to moderate other stuff',
      },
    ],
  },
  {
    emoji: '?',
    name: 'Interaction',
    color: '#ffa500',
    title: 'Help',
    description: 'Below you can see the interaction commands.',
    commands: [
      {
        name: 'Interaction 1',
        value: 'to interact 1',
      },
      {
        name: 'Interaction 2',
        value: 'to interact 2',
      },
    ],
  },
  {
    emoji: '?',
    name: 'Fun',
    color: '#ffff00',
    title: 'Help',
    description: 'Below you can see the fun commands.',
    commands: [
      {
        name: 'Fun 1',
        value: 'have fun 1',
      },
      {
        name: 'Fun 2',
        value: 'have fun 2',
      },
    ],
  },
  {
    emoji: '?',
    name: 'Games',
    color: '#e600e6',
    title: 'Help',
    description: 'Below you can see the games commands.',
    commands: [
      {
        name: 'Game 1',
        value: 'to play game 1',
      },
      {
        name: 'Game 2',
        value: 'to play game 2',
      },
      {
        name: 'Game 3',
        value: 'to play game 3',
      },
    ],
  },
  {
    emoji: '?',
    name: 'NSFW',
    color: '#4d0000',
    title: 'Help',
    description: 'Below you can see the NSFW commands.',
    commands: [
      {
        name: 'NSFW 1',
        value: 'nsfw 1',
      },
      {
        name: 'NSFW 2',
        value: 'nsfw 2',
      },
    ],
  },
  {
    emoji: '?',
    name: 'Information',
    color: '#008000',
    title: 'Help',
    description: 'Below you can see the information commands.',
    commands: [
      {
        name: 'Info 1',
        value: 'to get info 1',
      },
    ],
  },
];

module.exports = {
  name: 'help',
  description: 'Overview of all commands!',
  execute(message, args) {
    const embed = new Discord.MessageEmbed()
      .setColor('#171b20')
      .setTitle('Help')
      .setDescription('React to the message to see commands of a specific category!')
      .addFields(
        // add fields for each category
        categories.map((cat) => ({
          name: `${cat.emoji}   ${cat.name}`,
          value: '\u200b',
        }))
      )
      .setFooter(message.author.username)
      .setTimestamp();

    message.channel.send(embed).then((embedMsg) => {
      // send reactions for each emojis
      const emojis = categories.map((cat) => cat.emoji);
      emojis.forEach((emoji) => embedMsg.react(emoji));

      // the filter checks if the reaction emoji is in the categories
      // it also checks if the person who reacted shares the same id
      // as the author of the original message
      const filter = (reaction, user) =>
        emojis.includes(reaction.emoji.name) && user.id === message.author.id;

      const collector = embedMsg.createReactionCollector(filter, {
        // max number of reactions is the number of categories
        max: emojis.length,
        // it won't accept reactions after 60 seconds
        // optional, you can remove/change it
        time: 60000,
      });

      collector.on('collect', (reaction, user) => {
        // find the category by its emoji
        const selectedCategory = categories.find(
          (category) => category.emoji === reaction.emoji.name,
        );

        if (!selectedCategory) {
          return message.channel.send('Oops, there was an error... Try again?!');
        }

        const embed = new Discord.MessageEmbed()
          .setColor(selectedCategory.color)
          .setTitle(selectedCategory.title)
          .setDescription(selectedCategory.description)
          .addFields(selectedCategory.commands)
          .setFooter(message.author.username)
          .setTimestamp();

        message.channel.send(embed);
      });

      collector.on('end', (collected, reason) => {
        // reactions are no longer collected
        // if the user clicked on every available emoji
        if (reason === 'limit')
          return message.channel.send(`You've checked every emoji, ${message.author}. I won't accept any more reactions.`);

        // if it's timeout
        return message.channel.send(`It's been a minute now, so I won't accept any more reactions.`);
      });
    });
  },
};

enter image description here