1

how can i make multiple commands with the discord.js module?

code:

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('message', msg => {
  if (msg.content === '!hello') {
    msg.reply('Hello!!');
  }
});

client.login('token');

So, how do i make it so I can use multiple commands? Like !hi and !whatsup. I hope someone can help. Thanks

1
  • maybe .indexOf() ? Commented Mar 18, 2021 at 12:42

4 Answers 4

1

You can resume your if statement with else if()

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('message', msg => {
  if (msg.content === '!hello') {
    msg.reply('Hello!!');
  } else if(msg.content === "!hi") {
    msg.reply("Hi there!!");
  }
});

client.login('token');

Sign up to request clarification or add additional context in comments.

Comments

1

as a temporary solution, you can use "else if()" like so:

const Discord = require('discord.js');
const client = new Discord.Client(); 

client.on('message', message => {
if(message.content === '!command1') {
    message.channel.send('working!'); 
    } else if(message.content === '!command2') {
    message.channel.send('working!'); 
    } else if(message.content === '!command3') {
    message.channel.send('working');
}
}); 

client.login('Your Token goes here'); 

a better solution is to set up a command handler. there are a lot on youtube, one is: https://youtu.be/AUOb9_aAk7U hope this helped!

Comments

0

its really easy

const Discord = require("discord.js");
const client = new Discord.Client();

client.on('message', async message => {
if(message.content.toLowerCase().startsWith("hello")) {
message.channel.send("hola")
}
if(message.content.startsWith("hi there")) {
message.channel.send("hello")
}
})

Comments

0

You can use if() as a temporary solution but it would be better to setup a command handler.

    const Discord = require("discord.js");
    const client = new Discord.Client();
    
    client.on('message', async message => {
    if(message.content.toLowerCase().startsWith("command1")) {
    message.channel.send("...")
    }
    if(message.content.toLowerCase().startsWith("command2")) {
    message.channel.send("...)
    }
    })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.