Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading