Bot starter: your first Discord bot
Three files, five steps, one running bot with a slash command. Pick your language, copy the files and follow the guide below.
index.js// A minimal Discord bot with one slash command.
// Requires Node.js 18 or newer and discord.js v14.
require('dotenv').config();
const {
Client,
GatewayIntentBits,
REST,
Routes,
SlashCommandBuilder,
} = require('discord.js');
const commands = [
new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with the bot latency.'),
].map((command) => command.toJSON());
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once('clientReady', async () => {
// Register the slash commands on every start. Newly registered
// global commands can take a few minutes to appear in the client.
const rest = new REST().setToken(process.env.DISCORD_TOKEN);
await rest.put(Routes.applicationCommands(process.env.APPLICATION_ID), {
body: commands,
});
console.log(`Logged in as ${client.user.tag}`);
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === 'ping') {
await interaction.reply(`Pong! ${Math.max(0, client.ws.ping)} ms`);
}
});
client.login(process.env.DISCORD_TOKEN);
package.json{
"name": "my-discord-bot",
"private": true,
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"discord.js": "^14.16.3",
"dotenv": "^16.4.5"
}
}
.envDISCORD_TOKEN=your-bot-token
APPLICATION_ID=your-application-id
Getting it running
- Create an application in the Discord Developer Portal and copy the bot token and the application ID.
- Save the three files into a folder and put token and ID into .env. Add .env to your .gitignore.
- Install the dependencies:
npm install - Start the bot:
npm start - Invite it to your server with the invite link below and type /ping.
Generate an invite link
Listed in the Developer Portal under General Information.
The starter requests no permissions; slash commands work without them. What individual permissions mean is explained by the invite checker
What you get
A complete bot that runs, in a single file: the gateway login, one registered slash command (/ping) to copy your own commands from, error handling that does not crash the process, and the token in a `.env` file instead of the source. Plus the package file and step-by-step run instructions. No framework, no twelve-file scaffold to understand before anything happens.
Both templates are functionally identical, one in discord.js on Node.js and one in discord.py on Python. Pick whichever language you already write; the bot does not care, and switching later is cheap at this size.
Treat the token like a password
The bot token from the Developer Portal is full control over your bot. It belongs in `.env` and nowhere else: not in the code, not in screenshots, not in a public repository. The template reads it from the environment, and the included `.gitignore` line keeps the file out of Git.
If a token does leak, reset it in the Developer Portal immediately. Discord scans public code for tokens and sometimes invalidates them on its own, but that is a safety net, not a plan. The reset button takes three seconds.
From first run to a bot that stays online
Your own machine is fine for trying things out: install the dependencies, paste the token, start the process, invite the bot with the generated link. The slash command shows up in your server shortly after, and from there you add commands by copying the pattern.
For a bot people rely on, your laptop is the wrong home, because the bot dies when the lid closes. That is what the bot hosting on Topbot.gg is for: connect a repository, set the token as an environment variable, press start. The free tier covers a starter bot like this one, and the directory listing is created along the way.
Frequently asked
- Should I pick discord.js or discord.py?
- Whichever language you already like. discord.js runs on Node.js and suits you if JavaScript is your home; discord.py runs on Python and tends to read easier for beginners. Both templates here do exactly the same things in the same order, so the choice costs you nothing.
- Where do the token and application ID come from?
- From the Developer Portal at discord.com/developers. Create a new application, generate and copy the token on the Bot tab, and find the application ID under General Information. Both go into the template’s .env file, and the token never goes anywhere public after that.
- My slash command is not showing up. What now?
- Check three things in order: the invite link must include the applications.commands scope, the registration call must have run (the template registers on every start), and the client sometimes lags behind. Give it a few minutes and reload Discord with Ctrl+R before digging deeper.
- Does the bot go offline when I close my laptop?
- Yes. A bot is online exactly as long as its process runs somewhere. Your own machine is fine while developing; for anything people depend on, move it to a place that stays up, such as a server, a Raspberry Pi, or the bot hosting on Topbot.gg with its free tier.
- What does running a Discord bot cost?
- Discord charges nothing for the application or the API. The only cost is wherever the process runs. A starter bot needs very little memory and fits the free tier of the Topbot.gg hosting; the bill only grows once the bot serves many servers and heavier features.
And where does it run for good?
On your machine, the bot goes offline the moment you close the lid. The hosting here has a free tier, and your directory listing is created along the way.