How to create a Discord bot from scratch (2026)
What a bot is and how it works
A Discord bot is an application that connects to the Discord API and acts as an automated user: it responds to commands, moderates the server, plays music, sends alerts, and much more. To create one, you need two things: register the application in the Discord Developer Portal and write the code that brings it to life.
1. Create the application in the Discord Developer Portal
- Go to the Discord Developer Portal and log in.
- Click New Application, give it a name, and confirm.
- In the side menu, go to Bot.
- Click Reset Token to generate your bot's token.
2. Get and protect your token
The token is your bot's password — whoever has the token controls the bot. So:
- Never share it or push the token to GitHub.
- Store it in an environment variable (e.g.,
DISCORD_TOKEN), never directly in the code. - If it leaks, generate a new one immediately under Reset Token.
3. Configure the Intents
Still under the Bot tab, enable the Privileged Gateway Intents your bot needs — especially the Message Content Intent if it will read messages. Without it, the bot won't receive message content (see Why isn't my bot reading messages? Understanding Privileged Intents).
4. Invite the bot to your server
- Go to OAuth2 > URL Generator.
- Under Scopes, check
bot(andapplications.commandsif you'll use slash commands). - Under Bot Permissions, check the permissions you need.
- Copy the generated URL, open it in your browser, and choose the server.
5. Write your first "Hello World"
Pick a library (discord.js for JavaScript or discord.py for Python) and make the bot respond to a simple command. Not sure which to use? See discord.js or discord.py: which one to choose to get started.
Minimal example in discord.js:
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once('ready', () => console.log('Bot online!'));
client.login(process.env.DISCORD_TOKEN);
Next step: keep the bot online 24/7
On your computer, the bot is only online while the code is running. For it to work all the time, you need to host it. See the guide How to host a Discord bot and put your bot online 24/7 on Square Cloud.
Updated on: 06/13/2026
Thank you!
