Adding more commands

TIP

This page is a follow-up and bases its code on the previous page and assumes that you have read the interactions section and are familiar with its usage.

A bot with nothing but a single command would be boring, and you probably have a bunch of command ideas floating around in your head already, right? Let's begin, then.

Here's what your interaction event should currently look like:

client.on('interactionCreate', async interaction => {
	if (!interaction.isCommand()) return;

	if (interacton.commandName === 'ping') {
		await interaction.reply('Pong!');
	}
});
1
2
3
4
5
6
7

Before doing anything else, make a property to store the token. Instead of const config = ..., you can destructure the config file to extract the token variable.

const { token } = require('./config.json');
// ...
client.login(token);
 

 
1
2
3

From now on, if you change the prefix or token in your config.json file, it'll change in your bot file as well. You'll be using the prefix variable a lot soon.

TIP

If you aren't familiar with some of this syntax, it may be ES6 syntax. If it does confuse you, you should check out this guide page before continuing.

Simple command structure

You already have an if statement that checks messages for a ping/pong command. Adding other command checks is just as easy; chain an else if to your existing condition.

client.on('interactionCreate', async interaction => {
	if (!interaction.isCommand()) return;

	if (interacton.commandName === 'ping') {
		await interaction.reply('Pong!');
	} else if (interacton.commandName === 'beep') {
		await interaction.reply('Boop!');
	}
});

 
 
 
 
 
 
 

1
2
3
4
5
6
7
8
9

Displaying real data

Let's start displaying some real data. For now, we'll be displaying basic member/server info.

Server info command

Make another if statement to check for commands using server as the command name. You get the interaction object and reply to the interaction just as before:

TIP

Servers are referred to as "guilds" in the Discord API and discord.js library. Whenever you see someone say "guild", they mean server.

client.on('interactionCreate', async interaction => {
	if (!interaction.isCommand()) return;

	if (interacton.commandName === 'ping') {
		await interaction.reply('Pong!');
	} else if (interacton.commandName === 'beep') {
		await interaction.reply('Boop!');
	} else if (interacton.commandName === 'server') {
		await interaction.reply(`This server's name is: ${interaction.guild.name}`);
	}
});







 
 
 

1
2
3
4
5
6
7
8
9
10
11

The code above would result in this:

User used /server
Guide Bot08/01/2021
This server's name is: Discord Bot Guide

If you want to expand upon that command and add some more info, here's an example of what you can do:

client.on('interactionCreate', async interaction => {
	if (!interaction.isCommand()) return;

	if (interacton.commandName === 'ping') {
		await interaction.reply('Pong!');
	} else if (interacton.commandName === 'beep') {
		await interaction.reply('Boop!');
	} else if (interacton.commandName === 'server') {
		await interaction.reply(`Server name: ${interaction.guild.name}\nTotal members: ${interaction.guild.memberCount}`);
	}
});







 
 
 

1
2
3
4
5
6
7
8
9
10
11

That would display both the server name and the amount of members in it.

User used /server
Guide Bot08/01/2021
Server name: Discord Bot Guide
Total members: 3

Of course, you can modify this to your liking. You may also want to display the date the server was created or the server's region. You would do those in the same manner–use interaction.guild.createdAt or interaction.guild.region, respectively.

TIP

Want a list of all the properties you can access and all the methods you can call on a server? Refer to the discord.js documentation siteopen in new window!

User info command

Set up another if statement and use the command name user-info.

client.on('interactionCreate', async interaction => {
	if (!interaction.isCommand()) return;

	if (interacton.commandName === 'ping') {
		await interaction.reply('Pong!');
	} else if (interacton.commandName === 'beep') {
		await interaction.reply('Boop!');
	} else if (interacton.commandName === 'server') {
		await interaction.reply(`This server's name is: ${interaction.guild.name}`);
	} else if (interacton.commandName === 'user-info') {
		await interaction.reply(`Your username: ${interaction.user.username}\nYour ID: ${interaction.user.id}`);
	}
});









 
 
 

1
2
3
4
5
6
7
8
9
10
11
12
13

This will display the command author's username (not nickname, if they have one set), as well as their user ID.

User used /user-info
Guide Bot08/01/2021
Your username: User
Your ID: 123456789012345678

TIP

interaction.user refers to the user who sent the command. For a full list of all the properties and methods for the user object, check out the documentation page for itopen in new window.

And there you have it! As you can see, it's quite simple to add additional commands.

The problem with if/else if

If you don't plan to make more than seven or eight commands for your bot, then using an if/else if chain is sufficient; it's presumably a small project at that point, so you shouldn't need to spend too much time on it. However, this isn't the case for most of us.

You probably want your bot to be feature-rich and easy to configure and develop, right? Using a giant if/else if chain won't let you achieve that; it will only hinder your development process. After you read up on creating arguments, we'll be diving right into something called a "command handler" - code that makes handling commands easier and much more efficient.

Before continuing, here's a small list of reasons why you shouldn't use if/else if chains for anything that's not a small project:

  • Takes longer to find a piece of code you want.
  • Easier to fall victim to spaghetti codeopen in new window.
  • Difficult to maintain as it grows.
  • Difficult to debug.
  • Difficult to organize.
  • General bad practice.

In short, it's just not a good idea. But that's why this guide exists! Go ahead and read the next few pages to prevent these issues before they happen, learning new things along the way.

Resulting code

If you want to compare your code to the code we've constructed so far, you can review it over on the GitHub repository here open in new window.