'Discord.js Multiple Commands
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
Solution 1:[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');
Solution 2:[2]
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!
Solution 3:[3]
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")
}
})
Solution 4:[4]
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("...)
}
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Markiesch |
Solution 2 | Umbel_ |
Solution 3 | Maybe Lindow |
Solution 4 | FirephoenixX02 |