Discord · Guide

How to Make a Discord Bot

Updated October 10, 2026 4 min read

On this page
  1. Step 1: Create the application and the bot token
  2. Step 2: Invite the bot to your server
  3. Step 3a: Write the bot in Python (discord.py)
  4. Step 3b: Write the bot in JavaScript (discord.js)
  5. Keep the token out of your code
  6. Step 4: Keep your bot online
  7. FAQ

A Discord bot is a program that logs into Discord with its own account and answers commands, moderates, plays games or posts updates. Making one takes three things: an application in Discord's Developer Portal, a few lines of code, and somewhere for that code to run.

This guide builds a working bot with a /ping slash command, in Python (discord.py) or JavaScript (discord.js) — pick the language you know.

Step 1: Create the application and the bot token

  1. Open the Developer Portal. Go to discord.com/developers/applications, create a new application and give it a name.
  2. Get the token. On the Bot page, click Reset Token and copy it. Discord shows it only once; if you lose it, reset it again.
  3. Keep the token secret. It is the bot's password: anyone with it controls your bot. Never paste it into code you share or commit it to Git.
Do you need intents? A bot that only uses slash commands needs no privileged intent. Turn on Message Content on the Bot page only if the bot must read what people type in normal messages. Bots in more than 10,000 users' servers need Discord's approval for privileged intents.

Step 2: Invite the bot to your server

  1. Open the Installation page of your application.
  2. Under the default install settings for guilds, add the scopes bot and applications.commands, and the permissions the bot needs — Send Messages is enough for this guide.
  3. Open the install link it gives you, choose your server and authorise. The bot appears in the member list, offline until your code runs.

Older tutorials use OAuth2 > URL Generator with the same two scopes; that still works.

Step 3a: Write the bot in Python (discord.py)

discord.py 2.7 needs Python 3.8 or newer. Install it and create bot.py:

pip install discord.py
import os
import discord
from discord import app_commands

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()

bot = Bot()

@bot.tree.command(description="Replies with Pong!")
async def ping(interaction: discord.Interaction):
    await interaction.response.send_message("Pong!")

bot.run(os.environ["DISCORD_TOKEN"])

Run it with the token in an environment variable, for example DISCORD_TOKEN=your-token python bot.py on Linux or macOS.

Step 3b: Write the bot in JavaScript (discord.js)

discord.js 14 needs Node.js 18 or newer. In a new folder, run npm install discord.js and create index.js:

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

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

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

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

client.login(process.env.DISCORD_TOKEN);

Start it with DISCORD_TOKEN=your-token node index.js. When the console shows Logged in, type /ping in your server.

Keep the token out of your code

  • Use an environment variable or a .env file, and add .env to .gitignore before your first commit.
  • If the token leaks, reset it on the Bot page right away. The old token stops working, and you update it where the bot runs.
  • GitHub scans public repositories for Discord bot tokens and reports them to Discord, which can revoke them — so a token pushed by mistake often stops working before you notice.

Step 4: Keep your bot online

Your bot is online only while its code runs. Close the terminal or shut down the PC, and it goes offline. You have three options:

  • Your own PC — free, fine for building and testing, but the bot sleeps when the computer does.
  • A free tier — most idle or cap bots that run all month; the trade-offs are compared in free Discord bot hosting.
  • Bot hosting — a small always-on server. On HytHost Discord bot hosting you upload your project, choose Node.js, Python or another runtime, and the panel starts the bot and restarts it if it crashes.

The full walkthrough, from upload to auto-restart, is in how to host a Discord bot 24/7.

Run your bot without leaving your PC on: HytHost Discord bot hosting supports Node.js, Python, Java, Go, C#, Bun and Lavalink — from €0.89/mo.

See bot hosting plans
FAQ

Frequently asked questions

Yes. Creating the application, the token and the code costs nothing, and discord.py and discord.js are free. You only pay if you want it hosted somewhere that stays online.

Whichever you already know. discord.py and discord.js are both mature and cover the full Discord API. If you know neither, Python reads more simply; JavaScript has the larger library ecosystem.

Check that you invited it with the applications.commands scope and that the code synced the commands. Global commands can take a moment to appear; restart Discord if they do not.

Reading normal message text needs the Message Content intent, turned on in the Developer Portal and requested in your code. Without it the bot still receives slash commands, mentions and DMs.

Reset it on the Bot page of the Developer Portal straight away. The old token stops working; put the new one wherever the bot runs, and remove the old one from your code or history.

Your bot, online without your PC

HytHost Discord bot hosting runs Node.js, Python, Java, Go, C#, Bun or Lavalink with a MySQL database and backups — from €0.89/mo.

Live chat 8 AM – 11 PM (Chișinău time) · tickets answered during business hours

Was this guide helpful?