简体   繁体   English

如何使机器人不响应自身 discord.js

[英]How to make a bot not respond to itself discord.js

so i know that the way to make bot not respond to itself is put if (message.author.id === client.user.id) return;所以我知道if (message.author.id === client.user.id) return; or maybe if(message.author.bot) return;或者if(message.author.bot) return; but i dont know where should i put it.但我不知道我应该把它放在哪里。 my code is something like:我的代码是这样的:

const {Client, Intents} = require('discord.js'); 
const dotenv = require('dotenv')

dotenv.config();

const client = new Client(
    {
        intents:[
             Intents.FLAGS.GUILDS,
             Intents.FLAGS.GUILD_MESSAGES,
        ]
    }
);

client.on('ready', ()=>{
    console.log('bot working ngl')
})

//greetings
const random_greeting = () =>{
    return Math.floor(Math.random() * 6);
  }
  
  client.on('messageCreate', msg=>{
    let greeting = ['Hi', 'Yo', 'Ohayo', 'Hello', '👋👋', 'Ok...',]
    if (msg.content === 'hi', 'yo'){
      msg.reply(greeting[random_greeting()])
    }
  })

  client.on('message', message => {
    if (message.author.id === client.user.id) return;
  })
client.login(process.env.TOKEN)

as you see when someone say hi, the bot will greeting back by saying 1 of the 6 word in list, but the problem is whenever the bot replying someone with that list word is will keep going spam those word.正如您所看到的,当有人打招呼时,机器人会通过说出列表中 6 个单词中的 1 个来打招呼,但问题是,每当机器人用该列表词回复某人时,就会继续发送这些单词的垃圾邮件。 what code that can stop it replying to itself, and where should i put it什么代码可以阻止它自己回复,我应该把它放在哪里

Just add to your messageCreate event:只需添加到您的 messageCreate 事件:

  if (message.author.bot) return;

Its simple.这很简单。 On your messageCreate event, just add this as the first line in the event:在您的 messageCreate 事件中,只需将其添加为事件的第一行:

if(msg.author == client.user) return;

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

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