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

149
Views
How to use client.on in a JavaScript function?

I am currently using discord.js to develop a discord bot. I wanted to create a function to send a message based on user input.

The code I have is as follows:

function myFunction(keyWord) {
   client.on("messageCreate", myMessage => {
   if (myMessage.content.bot) {
      return
   }
   myMessage.channel.send(keyWord)
   })
}

When I run this function in my code, the code seems to stop at the client.on section. It does not throw an error, it simply does not send the message or run any code beyond the client.on.

I appreciate any help!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

myMessage.content returns a string; you are reading it as an object.

If you want to check if the author is a bot, you would read message.author.bot.

Try the following:

function myFunction(keyWord) {
  client.on('messageCreate', (myMessage) => {
    if (myMessage.author.bot)
      return;

    myMessage.channel.send(keyWord);
  });
}

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!