Making your first command
How do command works?
Here is a basic system that how command work:
1. Player type in a command
2. The bot fetch the command
3. the bot finds the appropriate code
4. run the code
Getting stuff ready
To start, we will be using the message event. Message event emits whenever a message is sent in a server. So go ahead and type in the code below:
client.on("message", msg => {
})
We will be writing in line 2 of the code from now on.
Next, we will be getting message content that is sent in the server and validate it.
It is suggeted to put a prefix to the command. A prefix is what comes before a command, in the following example, L! is the prefix
if(msg.content === "L!ping"){
msg.channel.send("Pong")//using msg.channel.send will send a message to the current channel
}
Adding more commands
To add more commands, just simply copy paste the if statement. Here is an example:
client.on("message", msg => {
if(msg.content === "L!ping"){
msg.channel.send("Pong")//using msg.channel.send will send a message to the current channel
}
if(msg.content === "L!help"){
msg.channel.send("Help command")//using msg.channel.send will send a message to the current channel
}
})
Last updated
Was this helpful?