简体   繁体   中英

When user reacts to message, bot sends another message in discord.js

I want to know how I can do it that if someone reacts to a specific emoji, the bot sends another message. It is for a help command. I have done the command itself and that the bot reacts to his own message and I also have added embeds for the other messages, I just don't know how to make it work.

Here is my code:

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();

  }
}

You can use reaction collectors . I would store the commands in an array of objects though, so you can use a lot less code.

I've created a categories array that contains the basic categories, their emojis, name, color, title, description, and commands. This way, you can loop over this array any time needed (eg when the bot sends first reaction, etc).

I've added a few comments below:

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.`);
      });
    });
  },
};

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM