Getting Started
Creating your Discord Bot
To get started, we need to create a Discord Bot.
- Go to the Discord Developer Portal.
- Choose a cool name for your new Discord Bot.
- Accept Discord’s ToS.
- Click on the “Create” button.
Discord Developer Portal showing the “New Application” button |
Now you have created your Discord Bot. Next, we’re going to allow some intents for the bot so it can read our messages.
- Head to the “Bot” tab on the left sidebar.
Discord Developer Portal showing the “Bot” tab |
- Scroll down until you see “Privileged Gateway Intents”.
- Check all three Intents.
Discord Developer Portal showing the “Privileged Gateway Intents” section |
Inviting your Bot
Now we need to invite our newly created bot to our server!
- Go to the “OAuth2” tab on the left sidebar.
Discord Developer Portal showing the “OAuth2” tab |
- Scroll down until you see “OAuth2 URL Generator”.
Check the following scopes:
bot
application.commands
Preferably, you should also check the “Administrator
” permission to avoid any permission issues.
Discord Developer Portal showing the “OAuth2 URL Generator” section |
- Copy the generated URL and paste it into your browser and select the server you want to invite the bot to.
Showing how to Invite the Bot |
You should now see your bot in your invited server. Next, we will set up a project to start coding our bot.
Showing the bot in the server |
Setting up a Project
The last thing to do is getting our new bot up and running. But first, let’s get our Discord Bot Token.
- Go back to the Developer Portal.
- Go to the “Bot” tab on the left sidebar.
- Reset the token.
Discord Developer Portal showing the “Bot” tab |
- Copy the Token, and keep it safe.
Bringing your Bot Online
Now that we have our bot token, we can start coding our bot.
We are going to use Visual Studio Code for this, you can however use any code editor you prefer.
- Open Visual Studio Code.
Choose what option you want to use.
Using the Command Line
-
Open a new terminal.
Opening a new terminal -
Run the following command to create a new project:
This might take a while, depending on your internet connection.
Running CLI |
- You will be asked to enter your bot token. Paste the token you copied earlier.
- Head to the created index.js file, you should see a couple of placeholders.
You need to replace the DISCORD BOT TOKEN
with the token you copied earlier, and the DISCORD BOT PREFIX
with the prefix you want to use for your bot.
Adding the base of your bot |
To continue with this guide, follow this link.
Starting from Scratch
To start from scratch we need to manually create our files and folders.
- Create a new folder for your project.
- Open the folder in Visual Studio Code.
- Create a
commands
folder. - Create a
index.js
file.
Creating a new folder |
- Run
npm init -y
in the terminal to create apackage.json
file.
package.json
is a file that contains metadata about the project. It includes the project’s name, version, description, and dependencies.
Make sure you’re in the project folder before running the command.
Running cd |
-
Install the
aoi.js
package by running the following command:You can use either
npm
,pnpm
,yarn
, orbun
to install the package. If you don’t know what to use, you can usenpm
.
If you get an error saying npm is not recognized as an internal or external command
, you need to install Node.js. You can download it from here.
npm
is a package manager for JavaScript programming language. It is the default package manager for the JavaScript runtime environment Node.js.
npx
is a package runner tool that comes with npm 5.2+ and higher. It is used to execute packages without installing them globally.
Running npm install |
Your file structure should look like this:
File Structure |
- We now need to add the base of your bot.
- This is the code that will be in your
index.js
file. - You require the previously copied token!
- You can ignore everything else for now besides the token.
Adding the base of your bot |
To continue with this guide, follow this link.
Creating your first Command
Now that we have the base of our bot, let’s create our first command.
- Create a new file in the
commands
folder. - Name the file
avatar.js
.
We will now use several functions to return the avatar of the user who used the command.
Creating the avatar command |
Running your Bot
Now that we have our bot set up, we can run it.
Head back to your terminal and run the following command:
This will run our bot, and you should see it come online in your server.
When running !avatar
or !ping
in your server, you should see the bot respond with the avatar of the user who used the command or with “Pong!“.