简体   繁体   中英

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; or maybe 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. what code that can stop it replying to itself, and where should i put it

Just add to your messageCreate event:

  if (message.author.bot) return;

Its simple. On your messageCreate event, just add this as the first line in the event:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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