我对机器人的信息做出反应后,如何使该机器人执行某些操作?

时间:2020-08-25 07:09:55

标签: javascript node.js discord.js

我试图让机器人等待用户的反应,但是我不知道如何设置它。我已经尝试过了,我也将GoogleCredentials credentials = GoogleCredentials .fromStream(Helper.class.getClassLoader().getResourceAsStream("smart-home-key.json")) .createScoped("https://www.googleapis.com/auth/homegraph"); credentials.refreshIfExpired(); AccessToken token = credentials.getAccessToken(); return token.getTokenValue(); 放在了第一部分,但是当它对1️⃣的消息做出反应时,机器人会说出答复。

const filter

2 个答案:

答案 0 :(得分:1)

Discord.js客户端对象具有messageReactionAdd(以及Remove,RemoveAll和当机器人移除表情符号反应时)事件触发器,从而为您提供了一个非常简单的解决方案。

let targetUser = "some-long-number"; 
// replace this with the author of the person who fires your 
// reaction related command in your client.on('message') def.
client.on('messageReactionAdd', (reaction, user)) {
  console.log('sample message to verify base functionality');
  if (user != targetUser) return null;
  switch (reaction.emoji.name) {
    case "1️⃣":
      message.reply("you reacted with 1️⃣");
      break;
    case "2️⃣":
      message.reply("you reacted with 2️⃣");
      break;
    case "3️⃣":
      message.reply("you reacted with 3️⃣");
      break;
    default:
      // in case you want to log when they don't reply with the correct emoji.
      break;
  }
}

我可能在某个地方输入了错误的内容,请随时纠正我。 如果message.reply()有效,我的第一个建议是在全局变量(例如channel-id)中记录用户命令的通道ID,并使用channel-id.send(content);

答案 1 :(得分:-1)

我看不到Emojis,但我会告诉你我该怎么做(注意:我使用的是异步函数,因此我不必使用大量的回调,我个人更喜欢它而不是{{ 1}}链接):

.then