How To Make a Discord Bot: Step‑by‑Step Guide

Creating a Discord bot can automate server moderation, deliver custom commands, and enhance community interaction. This article walks you through the entire process—from setting up a development environment to deploying a functional bot—so you can start building your own Discord companion today.

1. Prepare Your Development Environment

Before writing any code, you need a reliable IDE and the required libraries. PyCharm is a popular choice for Python developers; you can download it for free and enjoy a month of the Pro edition at no cost. If you prefer a lightweight editor, VS Code or Sublime Text also work well.

Install Python (version 3.8 or higher) from the official website and verify the installation:

  1. Open a terminal or command prompt.
  2. Run python --version. The output should display the installed version.
  3. If the command fails, add Python to your system PATH or reinstall.

Next, install the discord.py library, which provides a simple interface for the Discord API:

  1. Run pip install -U discord.py in your terminal.
  2. Wait for the package and its dependencies to download.

2. Register Your Bot with Discord

Discord requires each bot to have a unique application ID and token. Follow these steps:

  1. Visit the Discord Developer Portal at https://discord.com/developers/applications.
  2. Click “New Application,” give it a name, and confirm.
  3. Navigate to the “Bot” tab and click “Add Bot.” Confirm the creation.
  4. Under “TOKEN,” click “Copy” to store the secret token securely. Never share this token publicly.
  5. Enable the “SERVER MEMBERS INTENT” if your bot will access member data.

To invite the bot to a server you manage, generate an OAuth2 URL:

  1. Go to the “OAuth2” tab, then “URL Generator.”
  2. Select the “bot” scope.
  3. Choose required permissions (e.g., “Send Messages,” “Manage Channels”).
  4. Copy the generated link and open it in a browser to add the bot to your server.

3. Write the Basic Bot Code

The following minimal script connects the bot to Discord and responds to a simple command. Save the file as bot.py.

import discord from discord.ext import commands intents = discord.Intents.default() intents.message_content = True bot = commands.Bot(command_prefix='!', intents=intents) @bot.event async def on_ready(): print(f'Logged in as {bot.user}') @bot.command