【问题标题】:How do I make a message get edited when someone reacts with an emoji (discord.js)?当有人对表情符号(discord.js)做出反应时,如何编辑消息?
【发布时间】:2021-04-04 00:51:35
【问题描述】:

我想创建一个帮助屏幕,您可以使用反应滚动浏览。


例如,

  1. 如果您对信息表情符号做出反应,机器人的消息将被编辑以显示信息页面。
  2. 现在,当有人发送以“;help”开头的消息时,我可以发送帮助消息,并且我可以让机器人对消息做出反应。

但是,我无法让我的机器人删除用户的反应,也无法让其编辑我的消息。

这里是代码。提前感谢您的帮助!

client.on('message', message => {
    if (message.author !== null && message.author.bot) return;
    if (message.toString().startsWith(';help')) {
        const helpPi = new Discord.MessageEmbed()
        .setTitle('Help and commands')
        .setColor('0004ff')
        .setDescription('This is the help menu for available commands. Here is the table of contents:\n> 1 - Commands\n> 2 - More info.')
        const helpP1 = new Discord.MessageEmbed()
        .setTitle('Help and commands')
        .setColor('0004ff')
        .setDescription('Command A\nCommand B')
        const helpP2 = new Discord.MessageEmbed()
        .setTitle('Help and commands')
        .setColor('0004ff')
        .setDescription('More info.')
        message.channel.send(helpPi).then(sentMessage => {
            sentMessage.react('ℹ️')
            sentMessage.react('1️⃣')
            sentMessage.react('2️⃣')
            client.on('messageReactionAdd', reaction => {
                if (reaction == '1️⃣') {
                    sentMessage.edit(helpP1)
                }
                else if (reaction == '2️⃣') {
                    sentMessage.edit(helpP2)
                }
                else if (reaction == 'ℹ️') {
                    sentMessage.edit(helpPi)
                }
            })
        })
    }
});

【问题讨论】:

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


    【解决方案1】:

    这可以使用reactionCollectors 来完成。

    let mainEmbed;
    let penguinEmbed;
    let sparklesEmbed;
    
    const sentEmbed = await channel.send(mainEmbed)
    await sentEmbed.react('?')
    await sentEmbed.react('?')
    await sentEmbed.react('✨')
    // Adding await will guarantee that everything is in the right order, but it also slows down the operation
    
    // This filter: only accepts the following emojis, and rejects the bot's own reactions
    const filter = (reaction, user) => (['?', '?', '✨'].includes(reaction.emoji.name) && !user.bot)
    // Define a collector based on that filter for 60s
    const collector = sentEmbed.createReactionCollector(filter, {
      time: 60000
    }) // for 60s
    
    // Launch it!
    collector.on("collect", (reaction, user) => {
      switch (reaction.emoji.name) {
        case '?':
          sentEmbed.edit(mainEmbed);
          break;
        case '?':
          sentEmbed.edit(penguinEmbed);
          break;
        case '✨':
          sentEmbed.edit(sparklesEmbed);
          break;
      }
    });
    
    // When it ends, do something.
    collector.on("end", (collected) => {
    
      sentEmbed.edit(
        sentEmbed.embeds[0].setFooter(`This embed is inactive`)
      );
    
      sentEmbed.reactions.removeAll(); // You can also just remove your bot's reactions, or anything else...
    });
    

    这是我很久以前写的代码。我没有编写很多基本的东西来保持代码尽可能小(例如嵌入的定义)

    【讨论】:

    • 每当我对我的信息做出反应时,什么都没有发生。我没有收到任何错误,也没有看到任何异常。我的新代码在这里:repl.it/@DSASimon/Example#index.js 另外,如何消除反应?编辑:我尝试使用console.log() 来查看我的代码是否达到了那么远。但是,控制台中没有发生任何事情(如果您试图在我提供的链接上找到它,它在第 28 行)。
    • 我认为错误来自您添加的过滤器中的这些条件message.author !== null && message.author.bot。首先,消息的作者永远不会为空。其次,您刚刚指出过滤器将只接受反应......如果用户的消息是由机器人发送的。您应该用我在上面的消息中编辑的新过滤器替换此代码。另外,不是我写的,但可以随意将switch 替换为if/else,这样调试起来可能不会那么混乱。
    • 我已尝试修复您的建议,但现在它会在屏幕上停留一两秒钟,然后转到第二页 (helpP2)。如果您需要查看我的代码,请在 repl.it/@DSASimon/Example#index.js 。
    • 我认为是你的switch。您似乎忘记在每个 case 的末尾添加 breaks。你也可以在编辑操作前加上return。
    • 噢噢噢,好的,谢谢!如何消除反应?我试过使用 react.remove() 但这会删除机器人和用户的反应。我只想删除用户的反应。
    猜你喜欢
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 2020-11-14
    • 2021-08-11
    • 2021-05-05
    • 2020-11-21
    相关资源
    最近更新 更多