Creating a Discord Bot in Python (2025) | Episode 1: Setup & Basics
Table of Contents
Introduction
This tutorial will guide you through the process of creating a Discord bot using Python, specifically tailored for beginners. By following these steps, you'll learn how to register your bot, set up the necessary libraries, and write your first lines of code. This foundational knowledge will prepare you for more advanced bot development.
Step 1: Register Your Bot with Discord
To start, you need to create a new application on the Discord Developer Portal.
- Go to the Discord Developer Portal.
- Click on "New Application" in the top right corner.
- Enter a name for your application (this will be your bot's name) and click "Create."
- Navigate to the "Bot" tab on the left sidebar.
- Click on "Add Bot" and confirm by clicking "Yes, do it!"
- Note down the Token provided, as you will need it later to connect your bot to your server. Keep this token secret.
Step 2: Set Up Your Development Environment
You will need to install Python and a code editor to write your bot's code.
-
Install Python
-
Download Visual Studio Code
- Go to Visual Studio Code and download the version suitable for your operating system.
-
Install Required Libraries
- Open your terminal or command prompt.
- Install the Discord library using pip:
pip install discord.py
Step 3: Write Your First Lines of Code
Now that your environment is set up, it's time to write the code for your bot.
- Open Visual Studio Code and create a new file named
bot.py
. - Write the following code to create a simple bot that responds to a command:
import discord from discord.ext import commands # Create a bot instance bot = commands.Bot(command_prefix='!') # Define an event for when the bot is ready @bot.event async def on_ready(): print(f'Logged in as {bot.user}') # Define a command @bot.command() async def hello(ctx): await ctx.send('Hello! I am your bot.') # Run the bot with your token bot.run('YOUR_BOT_TOKEN')
- Replace
'YOUR_BOT_TOKEN'
with the token you copied earlier.
- Replace
Step 4: Run Your Bot
To see your bot in action, you need to run the script.
- Ensure you are in the directory where
bot.py
is saved. - In your terminal or command prompt, run the following command:
python bot.py
- If everything is set up correctly, you should see a message indicating that the bot is logged in.
Conclusion
Congratulations! You have successfully created your first Discord bot using Python. You learned how to register your bot, set up your development environment, and write basic code.
Next steps could include adding more commands, learning about events, or exploring Discord's API for more advanced features. Keep experimenting and expanding your bot's capabilities!