← Blog

How to make a Discord bot in 6 steps with discord.js or Python

Par Benjamin Dayan · PDG

· Mis à jour le November 20, 2026 · Lecture 6 min

Contents
Developer writing discord.js code for a Discord bot on a laptop beside a Discord server window

Learning how to make a Discord bot comes down to four things. Create an application in the Discord Developer Portal, add a bot to it and copy its token. Then invite the bot with an OAuth2 link (scopes bot and applications.commands), write a few lines in discord.js or Python, and host it so it runs round the clock. On Discord's side, all of it is free.

You need a Discord account, Node.js or Python installed and a code editor. The goal: a /ping command that replies on your server, then a bot that stays online while your PC is switched off. A custom bot can, for example, show the status of your rented game servers or announce a Rust wipe.

In short

  • The 6 steps: application, token, invite, choice of language, code, hosting.
  • Two scopes to tick when you invite the bot: bot and applications.commands.
  • The token lives in a .env file, never in your code and never on Discord.
  • A bot that is "online but silent" almost always comes down to slash commands, permissions or the invite.
  • To keep it running without your PC, you need Discord bot hosting or a machine that stays on permanently.

Step 1: how to make a Discord bot starts with an application in the Developer Portal

Head to the Developer Portal (discord.com/developers/applications), click New Application, give it a name, then open the Bot tab. Add an avatar and a display name.

The application is the container: credentials, settings, invite links. The bot is the account that actually joins your servers and acts on their behalf.

If the bot is only for your own community, untick Public Bot. Nobody else will then be able to invite it.

Step 2: get the token and protect it

In the Bot tab, click Reset Token. It is shown only once, so copy it straight away. If you lose it, you simply generate a new one.

The token is the bot's password. Anyone who holds it can control the bot: post in your channels, ban members, read whatever the bot can see.

Create a .env file at the root of the project:

DISCORD_TOKEN=paste_your_token_here
GUILD_ID=your_test_server_id

Then add a .gitignore file so it never ends up on GitHub:

.env
node_modules/

Keep the token out of screenshots, Discord messages and public repositories. If it leaks, go back to the Bot tab and click Reset Token: the old one stops working immediately. Don't count on automatic detection to rescue you.

The same page lists three Privileged Gateway Intents: Presence, Server Members and Message Content. Leave them switched off until you have a specific need. According to the Discord documentation, you toggle these intents in the portal, but once a bot reaches 100 servers, app verification becomes mandatory and each privileged intent must also be approved after that verification.

Step 3: invite the bot to your server

Go to OAuth2 > URL Generator. Tick the bot and applications.commands scopes, then the permissions your bot needs. Send Messages and Embed Links are often enough for a first bot. Avoid Administrator: a bot with only the bare minimum limits the damage if its token leaks.

Copy the generated URL, open it and choose your server. To add a bot, you need the Manage Server permission there.

Once invited, the bot shows as offline in the member list. That's normal. It only connects while your code is running.

Step 4: choose discord.js or Python

Criterion discord.js discord.py
Language JavaScript, Node.js Python
Version Current release, see npm 2.x
Minimum runtime A supported Node.js release; check the discord.js documentation A recent Python 3 release; check the discord.py documentation
Learning curve Gentle if you already know JS Very gentle for a beginner
Typical use Bot tied to a website or web panel Scripts, data, automation

If you already know JavaScript, or your community runs a web panel, pick discord.js. If you're starting from scratch, or mostly write scripts, go for Python. For a first bot, both work very well. Check the current versions on the official sites before installing, as they move quickly.

Step 5: write your first bot (a /ping command)

The principle is the same in both cases. The token comes from the .env file, the command is registered on your test server (so it appears immediately) and the bot replies "Pong" with its latency.

discord.js version

In an empty folder, run npm init -y, then npm install discord.js dotenv. Create index.js:

require('dotenv').config();
const { Client, Events, GatewayIntentBits, REST, Routes, SlashCommandBuilder } = require('discord.js');

const client = new Client({ intents: [GatewayIntentBits.Guilds] });
const commands = [new SlashCommandBuilder().setName('ping').setDescription('Tests latency').toJSON()];

client.once(Events.ClientReady, async () => {
  const rest = new REST().setToken(process.env.DISCORD_TOKEN);
  await rest.put(Routes.applicationGuildCommands(client.user.id, process.env.GUILD_ID), { body: commands });
  console.log(`Logged in: ${client.user.tag}`);
});

client.on(Events.InteractionCreate, async (interaction) => {
  if (interaction.isChatInputCommand() && interaction.commandName === 'ping') {
    await interaction.reply(`Pong: ${client.ws.ping} ms`);
  }
});

client.login(process.env.DISCORD_TOKEN);

Start it with node index.js. The discord.js guide itself recommends server-specific commands for development and testing.

Python version (discord.py)

Install the dependencies with pip install discord.py python-dotenv, then create bot.py:

import os
import discord
from discord import app_commands
from dotenv import load_dotenv

load_dotenv()
GUILD = discord.Object(id=int(os.getenv("GUILD_ID")))

class Bot(discord.Client):
    def __init__(self):
        super().__init__(intents=discord.Intents.default())
        self.tree = app_commands.CommandTree(self)

    async def setup_hook(self):
        await self.tree.sync(guild=GUILD)

bot = Bot()

@bot.tree.command(name="ping", description="Tests latency", guild=GUILD)
async def ping(interaction: discord.Interaction):
    await interaction.response.send_message(f"Pong: {round(bot.latency * 1000)} ms")

bot.run(os.getenv("DISCORD_TOKEN"))

Start it with python bot.py. If a major release changes the syntax, the official documentation is the final word.

My bot is online but doesn't respond: 4 common causes

  1. Slash commands not synced. Restart the bot after adding the command, and test on a specific server first. Global commands take longer to appear.
  2. Message Content intent disabled. It only matters for prefix commands (something like ?status). Enable it in the Bot tab, or switch to slash commands.
  3. applications.commands scope forgotten at invite time. Regenerate the URL with both scopes and invite the bot again.
  4. Missing channel permissions. Check that the bot can see the channel and send messages in it.

Step 6: keep your Discord bot online 24/7

While the terminal stays open on your PC, the bot answers. Switch the machine off and it vanishes. For a wipe announcement scheduled for 7 pm, or a /status command someone uses at 3 am, that won't do.

Option Advantages Limits
Your own PC Free Power cuts, running costs, your home IP address exposed
Mini-PC at home Cheap to run Maintenance, updates, home broadband
Fly-Serv Discord bot hosting Runs without your PC or your home connection A subscription

If you self-host, install a process manager so the bot restarts after a crash or a reboot. With Node.js, PM2 does the job:

npm install -g pm2
pm2 start index.js --name community-bot
pm2 save
pm2 startup

On Linux, a systemd service with Restart=always plays the same role for a Python script. Our guide on self-hosting or a game server host walks through that trade-off.

For a turnkey option, see Discord bot hosting: keep your bot online 24/7, where you'll find the details of the offer, its resources and its pricing. If you run several bots or want a panel, look at the Pterodactyl VPS.

What to avoid: self-bots and bots without the BOT tag

Automating your own user account with a personal token is called a self-bot. Discord prohibits it in its terms of service, and you risk having your account suspended. Only a bot account created through the portal and the official API is allowed.

Ready-made bots exist, but they can't query your Rust server or announce your wipe the way you want. For features specific to your community, a custom bot is still the right answer.

Frequently asked questions

Is it free to make a Discord bot?

Yes. The developer portal, the API and the discord.js and discord.py libraries cost nothing. The only possible expense is hosting, if you'd rather not leave your own machine running.

Do I need to know how to code to make a Discord bot?

A few basics in JavaScript or Python are enough for a first bot. The examples in this article are under 30 lines: copy them, get /ping working, then change the reply text.

What should I do if my Discord bot token has leaked?

Reset it immediately with Reset Token in the Bot tab, and the old one stops working. Put the new one in your .env file, delete any screenshot or commit that contained the old one, and restart the bot.

Why is my Discord bot online but not responding to commands?

The usual causes: slash commands not synced, the applications.commands scope missing from the invite, missing permissions in the channel, or the Message Content intent disabled for a prefix command.

How do I keep my Discord bot online without leaving my PC on?

It has to run on a machine that stays on permanently, with a process manager if you self-host, or on Fly-Serv Discord bot hosting. The second spares you from keeping a machine running at home and from the occasional broadband drop.

Is your bot working? Keep it running 24/7 without leaving your PC on: we take care of the rest. Check the Discord bot hosting page for the plans, and get in touch with the Fly-Serv team on Discord if you're unsure about your setup.

Read next