Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
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

about 4 years ago ยท Juan Pablo Isaza
2 answers
Answer question

0

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

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

about 4 years ago ยท Juan Pablo Isaza Report

0

Just add to your messageCreate event:

  if (message.author.bot) return;
about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!