Creating a Discord bot might sound complicated at first, but a very simple bot can actually be built in just a few minutes.
In this post we'll create a small bot using Discord.js that logs in and responds to a basic command.
No frameworks. No complicated architecture. Just the basics.
First, go to the Discord Developer Portal.
Create a new application, then:
⚠️ Never share your bot token. It is essentially the password for your bot.
Create a new folder for your bot and initialize a project.
mkdir my-discord-bot
cd my-discord-bot
npm init -y
Now install Discord.js:
npm install discord.js
This installs the latest version of Discord.js.
Create a file called:
index.js
This file will contain the code for our bot.
Your folder should now look like this:
my-discord-bot/
│
├── index.js
├── package.json
└── node_modules/
Open index.js and paste the following code.
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
/* Changed from "ready" to "clientReady" in DJS v14 */
client.once('clientReady', () => {
console.log(`Logged in as ${client.user.tag}`);
});
client.on('messageCreate', message => {
if (message.author.bot) return;
if (message.content === '!ping') {
message.reply('Pong! 🏓');
}
});
client.login('YOUR_BOT_TOKEN');
Replace: YOUR_BOT_TOKEN with the bot token you copied earlier.
Go back to the Discord Developer Portal.
Navigate to:
OAuth2 → URL Generator
Select the following scopes:
- bot
- applications.commands
Then give the bot these permissions:
- Send Messages
- Read Message History
Discord will generate an invite link for you.
Open that link in your browser and invite the bot to your server.
Start the bot with:
node index.js
If everything worked correctly, your terminal should show something like:
Logged in as MyBot#1234
Now go into your Discord server and type:
!ping
Your bot should respond with:
Pong! 🏓
And just like that — your first bot is running.
Let's break down the important parts.
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
This creates the Discord client and tells Discord what events the bot should receive.
Without the correct intents, the bot won't receive message events.
client.once('clientReady', () => {
console.log(`Logged in as ${client.user.tag}`);
});
This runs when the bot successfully logs into Discord.
client.on('messageCreate', message => {})
This event runs every time a new message is sent in a server the bot can see.
if (message.author.bot) return;
This prevents bot loops where bots reply to other bots forever.
if (message.content === '!ping') {
message.reply('Pong! 🏓');
}
If someone sends !ping, the bot replies with Pong!.
Once your first bot works, you can start adding features like:
Most bots start small and slowly grow as you experiment.
Tip
The best way to learn Discord.js is to build small features one at a time. Even experienced developers started with simple bots like this.
Building your first Discord bot is a great introduction to working with APIs, events, and JavaScript applications.
The hardest part is usually just starting.
Once the bot logs in and sends its first message, everything else becomes much easier to understand.
If you're curious about bot development, try adding new commands and experimenting with Discord.js features. You might be surprised how powerful these bots can become.