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:
- Open a terminal or command prompt.
- Run python --version. The output should display the installed version.
- 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:
- Run pip install -U discord.py in your terminal.
- 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:
- Visit the Discord Developer Portal at https://discord.com/developers/applications.
- Click “New Application,” give it a name, and confirm.
- Navigate to the “Bot” tab and click “Add Bot.” Confirm the creation.
- Under “TOKEN,” click “Copy” to store the secret token securely. Never share this token publicly.
- 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:
- Go to the “OAuth2” tab, then “URL Generator.”
- Select the “bot” scope.
- Choose required permissions (e.g., “Send Messages,” “Manage Channels”).
- 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