【问题标题】:Bot reacting to emojis机器人对表情符号做出反应
【发布时间】:2020-07-30 07:45:00
【问题描述】:

所以,我得到了我的代码,它可以按照我的意愿工作。弹出的消息改变了一切,非常完美。
现在我想添加,以便机器人知道我何时对其消息做出反应,然后执行其他操作。我的意思是:机器人发送一条带有反应的消息,每当一些用户点击反应时,就会发生一些事情,但我不知道该怎么做。

我尝试了很多类似if (reaction.emoji.name === ':bomb:') 的方法,但弹出了多个错误,我不知道如何解决。代码如下:

const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
var lastbuffer;
lastbuffer = 0;
const client = new Discord.Client();
client.once('ready', () => {
  console.log('Ready!');

});
client.on('message', message => {
  if(message.content.startsWith(`${prefix}start`)){
    message.delete()
    setInterval(function(){
      lastbuffer++;
      const Buffer = new Discord.MessageEmbed()
      .setColor('#8300FF')
      .setTitle("**It's time to check buffers!**")
      .setDescription("**It's been **" + "`" + lastbuffer + " Hour" + "`" + "** since last buffercheck, <@&675688526460878848>**." + " **Check now!**")
      .setThumbnail('https://art.pixilart.com/88534e2f28b65a4.png')
      .setFooter('WEEEEEWOOOOO')
      .setTimestamp();
      client.channels.cache.get("700296799482675230").send(Buffer).then(msg => {
        msg.react('✅');
        msg.react('????');
        msg.delete({timeout: 4000})
      });
    }, 5000)
  }
}); 

client.login(token);

【问题讨论】:

  • “弹出多个错误”听起来很有趣。你能分享其中一个错误吗?
  • 现在不能告诉你,我现在设法解决了这个问题

标签: javascript discord discord.js


【解决方案1】:

您将不得不通过 createReactionCollector() 方法使用 ReactionCollector。

你可以关注this guide到ReactionCollectors下面更好

【讨论】:

  • 请在您的答案中添加一些解释,以便其他人可以从中学习。此外,请避免将外部链接作为解决方案的唯一来源
【解决方案2】:

您需要使用reaction collector。

client.channels.cache.get("700296799482675230").send(Buffer).then(async msg => {
  // I'm using await here so the emojis react in the right order
  await msg.react('✅');
  await msg.react('?');
  msg.awaitReactions(
    // Discord.js v12:
    /* ({emoji}, user) => ['✅', '?'].includes(emoji.name) && user.id === message.author.id,
    {max: 1, time: 4000, errors: ['time']} */
    // Discord.js v13:
    {
      // only collect the emojis from the message author
      filter: ({emoji}, user) => ['✅', '?'].includes(emoji.name) && user.id === message.author.id,
      // stop collecting when 1 reaction has been collected or throw an error after 4 seconds
      max: 1,
      time: 4000,
      errors: ['time']
    }
  )
    .then(collected => {
      const reaction = collected.first()
      // do something
    })
    .catch(() => {
      // I'm assuming you want to delete the message if the user didn't react in time
      msg.delete()
    })

这段代码的作用:

  • 将嵌入 (Buffer) 发送到 ID 为 7002967999482675230 的频道
  • 使用嵌入的消息与 ✅ 和 ? 表情符号反应
  • 等待原始消息作者的 ✅ 或 ? 反应
    • 如果用户在 4 秒内做出反应,则运行 // do something 部分
    • 如果用户在 4 秒内没有反​​应,则删除带有嵌入的消息

【讨论】:

  • 请在您的答案中添加一些解释,以便其他人可以从中学习
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-11
  • 1970-01-01
  • 2021-12-04
  • 2021-07-24
  • 2020-11-14
  • 2021-08-11
  • 1970-01-01
相关资源
最近更新 更多