Make a discord-bot using the discord.js Commando framework
Discord.js Commando Framework tutorial
Next, let's get started
Step 1: Setting up the project
Create a new folder (Make sure there are no spaces in your folder name) in your system and open command prompt or power shell in the folder.
Run the command
npm initand fill out the form, If you don't know what you're doing, then leave everything to default and type in you name when it asks for
After this, you will see a new file named
package.jsonin your folder.
Now use the following command to install the required libraries
Now you'll see a new folder named
node-modulesin your bot's folder and you're ready to start coding the bot.
Step 2 : Writing the index.js file
Create a new file in your bot's folder called
index.jsand open it with your favourite text editor.
Now write the following code in the beginning of your index.js file.
These lines will import the necessary libraries to use.
Next, write the following code
This will create a
CommandoClientObject which is an extension of
discord.js.Clientclass and set the owner of the bot as yourself to allow you to use the owner only commands that come built-in with Commando.
Now we will import the default commands for our bot.
Now let's make a handler that displays a message in the console when the bot is logged in and ready to use.
Last of all, let's prepare the bot's login
Testing the defaults
Run the bot by using the command
node indexin your console.
After a while you will see a message similar to this: -
You will also observe that your bot comes online so it's ready to be checked out.
Use the help command which in my case is
It shows something like this in your DMs: -
groups: The commands in a commando bot are grouped together under categories. the
groupcommand can be used the list these categories.
enable/disable: Commando framework comes with an in-built feature of enabling/disabling commands in different guilds, these commands do the job.
load/unload: These commands can be used to register commands from outside the code.
reload: This command can be used to reload a command after you've made changes to the code to refresh it.
help: The in-built help function ( very detailed ).
prefix: Commando also comes with a feature to change the default prefix in different servers, the
prefixcommand helps the users do it.
ping: Does exactly what help says.
eval: runs the code passed as an extension of the
index.jsfile and returns the output ( recommended not to use ).
These are the commands that come built-in with commando framework.
Creating your own commands
First of all, create a folder named
commandsin your bot's root directory.
Next make the following change in your code
This will create a new group named Starter Commands for your bot which can be referred to in the code by the name test and also read the files inside the commands folder and treat them as commands for the bot.
Inside the commands folder, create another folder named
testand inside the test folder, create a file named
foo.jsas the first command will be a simple foo-bar command.
foo.jsfile and change it's contents to
The first line imports the predefined
Commandclass from the Commando Framework.
Next, we created the command as a extension of the
The constructor is run as soon as our index.js reads this file. Inside our constructor, we set the basic details about our command.
name, memberName: set the name of the command.
aliases: sets what other commands can be used to do the same thing.
description, details: describe what the command does.
examples: sets an example for the user on how to use the command.
group: sets our command will be part of which command group.
The next part is the run method, it is called whenever the user uses the command, the parameter msg is the message sent by the user, it's of the type
We make our bot respond with
barwhenever the user uses the
module.exports = fooCommandexports the command making it readable by our index.js file.
Now, let's test out our foo command.
Run the bot using
node indexand use the help command.
You will see something new at the end of the help menu
Let's use help about our command, use the help foo command. You should see something like: -
Let's use our command now, there are 4 ways to do it now.
==fin a server
You will see that the bot responds with
barwherever you use the command.
Now that we are done with one command, let's make another one that takes in input (arguments) from the user.
Create a new file named
say.jsin your test folder.
Write the following code in the say.js file.
Let's see what's new!
args: Defines what arguments the user has to enter.
- key: defines the name of the argument.
- prompt: defines what the bot will say if the user has not provided the argument
- type: defines the type of the argument.
The run method now has 2 parameters, the second one is the arguments, we are asking the user to pass.
msg.delete()deletes the message sent by the user.
Let's test out our say command
If you use the help command now, you will see a new entry at the bottom for the say command.
Use help say before using the command.
You should see something similar to: -
Notice how Commando prepared the format for how to use the command, trust me it's a handy feature.
saycommand now without passing any arguments.
You will see that the bot asks you for the text to say, also with a message like
This is another feature of Commando framework - command cancellation.
You can either say something for the bot to repeat it or wait for 30 seconds to cancel the command or say cancel yourself.
You can also pass on the argument while using the command as in the examples.
Messing with the defaults
You can also modify the default commands: -
You can find them at
In there, you will see 2 folders containing the command files.
help.js- shows help for the various commands.
ping.js- displays the bot's ping to the discord server.
prefix.js- changes the prefix for the server ( guild Only command ).
enable.js- enables a command in a server.
disable.js- disables a command in a server.
groups.js- lists all command groups.
load.js- loads a new command.
reload.js- reload a command.
unload.js- unloads a command.
That's all you need to know to get started, put your imagination to the test and make amazing bots.
Sqlite does not work with repl.it and you do not go any further than saying that sqlite is required, what do you do with sqlite?
For the custom commands can you add how to use the validate option?
Also might want to link some documentation: https://discord.js.org/#/docs/commando/master/general/welcome