How to Create a Discord Bot from Scratch: A Beginner’s Guide 🚀
Learn how to create Discord bot from scratch with this simple, step-by-step guide for beginners. Set up your bot, write code, and bring your Discord server to life today!
Step 1: Set Up Your Environment 🛠️
Before we write any code, let’s get a few things ready:
- Install Node.js🕸️
We’ll use Node.js to run the bot.
👉 Download Node.js here and install it. (Get the LTS (long term support) version, currently the version is in v22.15.0) while installing tick in add to path checkbox to make sure it is added it in env variable. Install on MacOS and Linux use any package manager to install node.js
To make sure it’s installed, open your terminal or command prompt and type:
node -v
npm -v
You should see the versions of Node and npm printed.
Step 2: Create Your Discord Application 🧩
- Go to the Discord Developer Portal
Visit Discord Developer Portal.
- Create a New Application
- Click “New Application”.
- Give it a name (for example, Merofirstbot).
- Click “Create”.
- Add a Bot
- Go to the Bot section (left sidebar).
- Click “Add Bot” → “Yes, do it!”.
Congratulations, you now have a bot! 🎉
Step 3: Get Your Bot Token 🔑
This token is like the bot’s password.
Important: Keep it secret!⚡
- Under Bot Settings, you’ll find TOKEN.
- Click “Reset Token” → “Copy” it somewhere safe. And do not share to anybody else .this token is like the password
Step 4: Invite Your Bot to Your Server 🎟️
You need to invite your bot to a server where you have “Manage Server” permissions.
- Go to OAuth2 → URL Generator.
- Under SCOPES, select:
- bot
- Under BOT PERMISSIONS, select what you want (for testing, choose Send Messages and Read Messages).
- Copy the generated URL, paste it into your browser, and invite the bot to your server!
- THE GENERATED URL LOOKS LIKE THIS : https://discord.com/api/oauth2/authorize?client_id=1xxxxxxxxxxxxxxxx&permissions=0&scope=bot%20applications.commands
Step 5: Build Your Bot 🤖
Once you add bot to the server the next step is to start coding and get it online! Let’s start by creating a config file for your client token and a main file for your bot application.
- Create a Project Folder
In your computer, make a folder called mero-first-bot and open it in a code editor (like VS Code)
In the vscode terminal
mkdir mero-first-bot
cd my-first-bot
- Initialize the Project
Run:
npm init -y
It will create a package.json file for you.
- Install Discord.js
This is the library we’ll use to interact with Discord. Your token is essentially your bot’s password, and you should protect it as best as possible. This can be done through a by using environment variables. So along with with discord.js package also install dotenv
npm install discord.js
npm install dotenv
Create .env file
In your project folder , create a file name .env this is where you keep your environment variables. In your .env file
DISCORD_TOKEN = PASTE_YOUR_TOKEN_FROM_STEP_3_HERE
- Create index.js
In your project folder, create a file named index.js and add this code:
- const { Client, GatewayIntentBits } = require(‘discord.js’);
- const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
- const token = process.env.DISCORD_TOKEN;
- client.on(‘ready’, () => {
- console.log(`Logged in as ${client.user.tag}!`);
- });
- client.on(‘messageCreate’, (message) => {
- if (message.content === ‘!hello’) {
- message.reply(‘Hello there! namaste 👋’);
- }
- });
- client.login(token);
Replace the Token
Make sure you add your token in dot env file as stated earlier in step 3
Step 6: Run Your Bot 🚀
Now, start your bot by typing:
node index.js
If everything is good, you should see:
Logged in as Merofirstbot! In the terminal and see the bot online on your discord server
Now, go to your Discord server and type:
!hello
Your bot should reply:
Hello there! namaste 👋
🎉 BOOM! You just created your first working Discord bot from scratch! Happy coding !!!