Tutorial: Creating a Discord Bot (Part 1)
NicolasBrondinBernard
Want to experiment with adding features to Discord with a bot? This way!

Article published on 10/11/2021, last updated on 10/08/2026
Let's start by understanding what a Discord bot actually is, and how it works so we can then code our own.
Note that we will be using Javascript and NodeJS to develop our bot throughout this tutorial.
The principle
A Discord bot is made up of two distinct parts: A virtual Discord user and the code hosted on a server.
The virtual user lets us manage the name, avatar, permissions, credentials, and other settings, directly on Discord's admin pages.
The code, on the other hand, will subscribe to this virtual user so it can read on its behalf and perform actions on the server where it has been invited (a bot can be invited to as many servers as we want).
Creating the virtual user
The first step is to go to Discord's developer portal, right here: https://discord.com/developers/applications
Next, you'll need to create a new application by filling in your bot's name and description.
Careful, here you're creating an application, but not yet your virtual user (bot). That's the next step.


Once this step is done, set your "application id" aside, we'll need it at the end to invite your bot to a server.
Next, click on the "Bot" tab in the left-hand menu, then on "Add Bot" to create your virtual user.

It's in this section that you'll be able to choose your bot's final name, its avatar, and above all retrieve the login token!

Careful, the token must not be disclosed to anyone, otherwise anyone could take control of your bot! Set it aside so you can add it into your code.
Inviting the bot to a server
Congratulations, your virtual user (bot) has been created, and all that's left is to bring it to life with code, but before that, the first step is to invite it to a server, since it's only within a server that we'll be able to use our bot.
To do this, you just need to click on the bot's invitation link. The only problem is that it doesn't exist yet, and it's up to you to create it.
A bot invitation link looks like this:
https://discord.com/api/oauth2/authorize?client_id=CLIENT_ID&permissions=PERMISSION_NUMBER&scope=bot%20applications.commands
As you've probably guessed, the CLIENT_ID must be replaced by the one retrieved earlier on the application page, but for the PERMISSION_NUMBER, you'll need to generate it based on the permissions you want!
By scrolling all the way down the "Bot" tab, you'll find a utility that lets you select all the desired permissions, and copy the final number to insert into the invitation link.

All you have to do now is click on this link, to be redirected to a page asking you to choose the server to add the bot to, and that's it!
How the bot works
Once the virtual user has been created on Discord, you have access to a login token, which your code will use to identify itself to Discord as being that virtual user.
Then, for as long as your code is running, Discord will send all events related to the virtual user (being added to a server, mentions, messages, commands, etc.) directly to your NodeJS client (identified with the token).
Finally, all your bot will have left to do is respond to events however you decide to (sending messages, images, admin actions, etc.)
The events sent by Discord and your bot's responses will always be governed by the permissions passed in the invitation link.
The minimal code
Here is the minimal code (NodeJS) to generate the bot's client and identify it to Discord (with the token) in order to receive events related to the virtual user:
//index.js
require('dotenv').config();
const { Client, Intents } = require('discord.js');
const token = process.env.DISCORD_TOKEN
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
// When the client is ready, the virtual user is shown as "Logged in" in the servers.
client.once("ready", function(){
console.log(`I am ready! Logged in as ${client.user.tag}!`);
});
// Login to Discord with your client's token
client.login(token);
/*
//Listen to messages, server invite, etc...
client.on("...", (...)=>{});
*/
Careful, the discord.js SDK requires a minimum NodeJS version of 16.6.0, I recommend using NVM to make your life easier!
For convenience, you can find this code, along with the environment and dependency files, directly on my GitHub:
All you need to do is run "node index.js" to see your bot come to life and connect to Discord!
The second part of this tutorial is available here: https://code-garage.fr/blog/tutorial-create-a-discord-bot-part-2/
No spam. Only free content, news, and ever more resources to level up your skills!
Join +1500 developers
No comments yet