# コマンドをもっと作成する

TIP

This page is a follow-up and bases its code on the previous page.

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? それでは始めましょう。

メッセージイベントは次のようになります。

client.on('message', message => {
	if (message.content === '!ping') {
		message.channel.send('Pong.');
	}
});

まず最初に、設定ファイルにプレフィックスを保存するためのプロパティを作成します。 Instead of const config = ..., you can destructure the config file to extract the prefix and token variables.

 

 

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

From now on, if you change the prefix or token in your config.json file, it'll change in your bot file as well. なのでプレフィックス変数を使うことが多くなるでしょう。

TIP

If you aren't familiar with some of this syntax, it may be ES6 syntax. 混乱するようであれば、先にこのガイドページを確認してください。

# 簡易的なコマンド構造

ping/pongコマンドのメッセージをチェックするif文はすでにあると思います。 Adding other command checks is just as easy; chain an else if to your existing condition.


 
 
 
 
 


client.on('message', message => {
	if (message.content === `${prefix}ping`) {
		message.channel.send('Pong.');
	} else if (message.content === `${prefix}beep`) {
		message.channel.send('Boop.');
	}
});

これにはいくつかの潜在的な問題があります。 例えば、!ping testを送るとpingコマンドは動作しません。 !pingのみにしか一致しません。 他のコマンドも同様です。 コマンドをより柔軟にしたい場合は、以下のようにします。


 
 
 
 
 


client.on('message', message => {
	if (message.content.startsWith(`${prefix}ping`)) {
		message.channel.send('Pong.');
	} else if (message.content.startsWith(`${prefix}beep`)) {
		message.channel.send('Boop.');
	}
});

これでメッセージが!pingで_始まるとき_にpingコマンドが実行されます! Sometimes this is what you want, but other times, you may want to match only exactly !ping - it varies from case to case, so be mindful of what you need when creating commands.

WARNING

Be aware that this will also match !pingpong, !pinguin, and the like. This behavior may or may not be a problem for you, but don't worry; you'll see better ways to check for commands later.

# 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've already interacted with the Message object via message.channel.send(). You get the message object, access the channel it's from, and send a message to it. Just like how message.channel gives you the message's channel, message.guild gives you the message's server.

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('message', message => {
	if (message.content === `${prefix}ping`) {
		message.channel.send('Pong.');
	} else if (message.content === `${prefix}beep`) {
		message.channel.send('Boop.');
	} else if (message.content === `${prefix}server`) {
		message.channel.send(`This server's name is: ${message.guild.name}`);
	}
});

The code above would result in this:

User
User 06/07/2021
!server
Guide Bot
Guide Bot Bot 06/07/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('message', message => {
	if (message.content === `${prefix}ping`) {
		message.channel.send('Pong.');
	} else if (message.content === `${prefix}beep`) {
		message.channel.send('Boop.');
	} else if (message.content === `${prefix}server`) {
		message.channel.send(`Server name: ${message.guild.name}\nTotal members: ${message.guild.memberCount}`);
	}
});

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

User
User 06/07/2021
!server
Guide Bot
Guide Bot Bot 06/07/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 message.guild.createdAt or message.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 site (opens new window)!

# User info command

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








 
 
 


client.on('message', message => {
	if (message.content === `${prefix}ping`) {
		message.channel.send('Pong.');
	} else if (message.content === `${prefix}beep`) {
		message.channel.send('Boop.');
	} else if (message.content === `${prefix}server`) {
		message.channel.send(`This server's name is: ${message.guild.name}`);
	} else if (message.content === `${prefix}user-info`) {
		message.channel.send(`Your username: ${message.author.username}\nYour ID: ${message.author.id}`);
	}
});

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

User
User 06/07/2021
!user-info
Guide Bot
Guide Bot Bot 06/07/2021
Your username: User
Your ID: 20833034795932416

TIP

message.author refers to the user who sent the message. For a full list of all the properties and methods for the author object (a member of the User class), check out the documentation page for it (opens 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 code (opens 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 (opens new window).