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

Posted

技术标签:

【中文标题】当用户对消息做出反应时,机器人会在 discord.js 中发送另一条消息【英文标题】:When user reacts to message, bot sends another message in discord.js 【发布时间】:2021-06-12 02:32:22 【问题描述】:

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

这是我的代码:

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】:

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

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

我在下面添加了几个 cmets:

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

【讨论】:

以上是关于当用户对消息做出反应时,机器人会在 discord.js 中发送另一条消息的主要内容,如果未能解决你的问题,请参考以下文章

当有人对消息做出反应时,我如何让我的 discord.js 机器人添加角色?

当用户做出反应时,Discord Bot 不发送消息

Discord js v12 如果有人对嵌入做出反应,则发送消息

(discord.js) 如何提及对机器人消息做出反应的用户?

如何让我的 Discord.JS 机器人对其自己的直接消息做出反应?

Discord js - 如何在给定的时间段内继续对消息做出反应并编辑嵌入