简体   繁体   English

机器人为什么会自我反应? (discord.js)

[英]Why does the bot react itself? (discord.js)

I'm setting up a bot, who will await for a user's reaction to write a short message. 我正在设置一个机器人,该机器人将等待用户的反应以编写短消息。 Currently, the bot responds itself to his reaction! 目前,该机器人对自己的反应做出了回应! Why? 为什么?

const { Client, RichEmbed, Discord } = require('discord.js');
const config = require('./config.json');
const client= new Client();


client.on('ready', () => {
    console.log('je suis pret!');
    client.user.setActivity("/help pour appeler de l'aide (avec le code NOMAD bien sur)", { type: 'WATCHING' });

})
client.on('messageReactionAdd', (reaction, user) => {
    console.log(`${user.username} reacted with "${reaction.emoji.name}".`);
});

client.on('messageReactionRemove', (reaction, user) => {
    console.log(`${user.username} removed their "${reaction.emoji.name}" reaction.`);
});
client.on('message', async message => {
    if (message.author.bot) return; 

    if (message.content.toLowerCase().startsWith('!rea')) {
      try {
        const sentMessage = await message.channel.send('Select a number.');

        for (let n = 1; n <= 5; n++) await sentMessage.react(`${n}⃣`);

        const filter = (reaction, user) => ['1', '2', '3', '4', '5'].includes(reaction.emoji.name.slice(0, 1)) && user.id === message.author.id;
        const collected = await sentMessage.awaitReactions(filter, { maxMatches: 1, time: 60000 });

        if (collected.size === 0) {
          await sentMessage.clearReactions();
          await message.channel.send('Your time ran out.');
        } else {
          const reaction = collected.first();

          switch(reaction.emoji.name.slice(0, 1)) {
            case '1':
              await message.channel.send('You chose `one`.');
              break;
          }
        }
      } catch(err) {
        console.error(err);
      }
    }
  });
client.login(config.token);

I found the problem: the bot considers the first message: "!rea", so if I add the reaction below !rea, it answers! 我发现了问题:该漫游器考虑了第一条消息:“!rea”,因此,如果我在!rea下面添加响应,它就会回答! How can I fix this, because I want that he considered the embed reactions! 我该如何解决这个问题,因为我希望他考虑了嵌入的反应!

Thank you for your help 谢谢您的帮助

Your client's message event is emitted by any message, including those sent by itself. 客户的message事件由任何消息发出,包括自身发送的消息。

To prevent bots (and therefore your client as well) from triggering the code, add a condition checking the author's User.bot property at the beginning of the callback, and return if it's true . 为了防止机器人(以及您的客户端)触发代码,请在回调的开头添加一个条件,以检查作者的User.bot属性,如果为true返回。

if (message.author.bot) return; // Stop if the author is a bot.

// Continue with your code.

Additionally, you're collecting reactions on the wrong message, and on every one. 此外,您正在收集对错误消息以及每条消息的反应。 message outside of your first then() callback is the one the bot received . 僵尸程序收到的第一个then()回调之外message

To make things much more simple and readable, we can use the await keyword (note that it can only be used asynchronous context, like in a function declared as async). 为了使事情更简单易读,我们可以使用await关键字(请注意,它只能用于异步上下文,例如在声明为async的函数中)。

Combining these changes... 结合这些变化...

client.on('message', async message => {
  if (message.author.bot) return; 

  if (message.content.toLowerCase().startsWith('!rea')) {
    try {
      const sentMessage = await message.channel.send('Select a number.');

      for (let n = 1; n <= 5; n++) await sentMessage.react(`${n}⃣`);

      const filter = (reaction, user) => ['1', '2', '3', '4', '5'].includes(reaction.emoji.name.slice(0, 1)) && user.id === message.author.id;
      const collected = await sentMessage.awaitReactions(filter, { maxMatches: 1, time: 60000 });

      if (collected.size === 0) {
        await sentMessage.clearReactions();
        await message.channel.send('Your time ran out.');
      } else {
        const reaction = collected.first();

        switch(reaction.emoji.name.slice(0, 1)) {
          case '1':
            await message.channel.send('You chose `one`.');
            break;
        }
      }
    } catch(err) {
      console.error(err);
    }
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM