机器人对表情符号做出反应

时间:2020-04-16 22:12:57

标签: javascript discord discord.js

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

我尝试了许多类似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);

2 个答案:

答案 0 :(得分:1)

您将不得不使用ReactionCollector方法来使用createReactionCollector()

您可以更好地关注this guide,并在ReactionCollector以下

答案 1 :(得分:1)

您需要使用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(
    // only collect the emojis from the message author
    ({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为700296799482675230的频道
  • 与嵌入的消息上的✅表情符号和?表情符号反应
  • 等待原始消息作者的✅或?反应
    • 如果用户在4秒钟内做出反应,请运行// do something部分
    • 如果用户在4秒钟内没有反应,请删除带有嵌入内容的邮件