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

156
Views
Can msg.content be equal to a variable?

I'm new to writing discord.js and first I don't know if this is a stupid question, but the question is I want to make a bot so that the first section is greeting:

//greetings
const random_greeting = () =>{
    return Math.floor(Math.random() * 5);
  }
  
  client.on('messageCreate', msg=>{
    if(msg.author == client.user) return;
    let greeting = ['Hi', 'Yo', 'Hello', '๐Ÿ‘‹๐Ÿ‘‹', 'Ok...',]
    if (msg.content === 'hi'){
      msg.reply(greeting[random_greeting()])}
    if (msg.content === 'hello'){
      msg.reply(greeting[random_greeting()])}
    if (msg.content === 'yo'){
      msg.reply(greeting[random_greeting()])}
    if (msg.content === 'sup'){
      msg.reply(greeting[random_greeting()])}
    if (msg.content === 'wassup'){
      msg.reply(greeting[random_greeting()])}
    if (msg.content === 'yo'){
      msg.reply(greeting[random_greeting()])}
  })

As you can see i need to write:

if (msg.content === 'hello'){
      msg.reply(greeting[random_greeting()])}

and keep spamming it like 6 times, so my question is there any way to do this short or faster?

I have tried something like adding greetinganswer variable:

let greetingAns = ['hi', 'hello']

but when i used

const random_greeting = () =>{
    return Math.floor(Math.random() * 5);
  }

  client.on('messageCreate', msg=>{
    if(msg.author == client.user) return;
    let greeting = ['Hi', 'Yo', 'Hello', '๐Ÿ‘‹๐Ÿ‘‹', 'Ok...',]
    let greetingAns = ['hi', 'hello']
    if (msg.content === greetingAns()){
      msg.reply(greeting[random_greeting()])}

I think it should work but it doesn't. So is msg.content === a variable?

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

0

Yes, you can do that and you will need to use an array of strings as you already did. So to check if the message content is found in the array it will be needed the includes() javascript method.

Like this:

if (greetingAns.includes(msg.content)) {
    msg.reply(greeting[random_greeting()]);
}
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!