---
title: Creating a Simple Discord Bot with Python
date: 2023-10-27
draft: false
description: A step-by-step guide to creating your first Discord bot using Python and the discord.py library.
categories: ["Discord", "Bots", "Python"]
tags: ["Discord", "Bot", "Python", "discord.py"]
---
# Creating a Simple Discord Bot with Python
In this guide, we will build a basic Discord bot using Python and the powerful `discord.py` library. This bot will respond to simple commands, and we'll learn the fundamentals to expand it in the future.
## Prerequisites
* **Python:** Make sure you have Python installed (version 3.7 or higher).
* **discord.py:** Install the library using `pip install discord.py`.
* **A Discord account:** You'll need a Discord account and a server where the bot can operate.
## Creating a Bot on Discord (Step-by-Step)
1. **Create an Application on the Discord Developer Portal:**
* Go to the [Discord Developer Portal](https://discord.com/developers/applications).
* Click "New Application" and give your bot a name.
2. **Turn the Application into a Bot:**
* In your application's menu, go to "Bot".
* Click "Add Bot". Confirm the creation.
3. **Get the Bot Token:**
* On the Bot page, copy the "Token". **Keep this token a secret!**
4. **Invite the Bot to your Server:**
* Go to "OAuth2" -> "URL Generator".
* Select the "bot" scope and, under "Bot Permissions", choose the permissions your bot needs. To start, "Read Messages/View Channels" and "Send Messages" are sufficient.
* Copy the generated URL and paste it into your browser. This will redirect you to add the bot to your server.
## Python Code
Create a Python file (e.g., `my_bot.py`) and paste the following code:
```python
import discord
# Replace with your token!
TOKEN = 'YOUR_TOKEN_HERE'
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(f'Logged in as {client.user}')
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!hello'):
await message.channel.send('Hello!')
elif message.content.startswith('!ping'):
await message.channel.send('Pong!')
client.run(TOKEN)
Code Explanation:
discord.Client(): Creates an instance of the Discord client.on_ready(): This event is triggered when the bot is connected and ready to use.on_message(): This event is triggered whenever a message is sent in a channel where the bot is present.message.content: Contains the content of the message.message.channel.send(): Sends a message to the channel where the original message was sent.intents: Declaration to the bot to receive messages withintents.message_content = Trueclient.run(TOKEN): Starts the bot with your token.
Running the Bot
Open the terminal, navigate to the directory where you saved the my_bot.py file, and run:
python my_bot.py
If everything is correct, you will see “Logged in as [Your Bot Name]” in the terminal. Now, go to your Discord server and type !hello or !ping in a text channel. Your bot should respond!
Next Steps
This is just a very basic bot. You can expand it in many ways, such as:
- Adding more commands.
- Integrating with external APIs.
- Creating moderation systems.
- Developing interactive games.
The discord.py library offers countless possibilities. Explore the official documentation to learn more: https://discordpy.readthedocs.io/en/stable/