Creating a Discord Bot with Python — Step-by-Step Guide (Part I of II)

Want to create a Discord bot? Python is a great choice for this task. With its simple syntax and powerful libraries, Python makes it easy to build and manage bots for your Discord server. Here’s a step-by-step guide on how to get started with Python and create a functional Discord bot.

1. Setting Up Your Environment

First, make sure you have Python installed on your computer. You can download the latest version from the official Python website. After installing Python, you’ll need to install a few additional packages. The primary library for interacting with Discord in Python is discord.py. To install it, open your command line or terminal and run:

pip install discord.py

2. Creating Your Discord Bot

To begin, you’ll need to set up a bot on the Discord Developer Portal. Go to the Discord Developer Portal and log in with your Discord account. Click on “New Application” to create a new app. Give it a name, then navigate to the “Bot” tab on the left sidebar and click “Add Bot.” This action will generate a token for your bot, which you’ll use to connect your bot to Discord. Copy this token; you’ll need it later.

3. Writing Your Bot’s Code

Create a new directory for your project. Let’s name it discord_bot_project. Inside this directory, create a file named bot.py. This file will contain the code for your bot. Open bot.py with your preferred text editor and add the following code:

import discord
from discord.ext import commands

# Replace 'your_token_here' with the token you copied earlier
TOKEN = 'your_token_here'

# Define the bot prefix
bot = commands.Bot(command_prefix='!')

# Event handler for when the bot is ready
@bot.event
async def on_ready():
    print(f'We have logged in as {bot.user}')

# Command handler for the 'ping' command
@bot.command()
async def ping(ctx):
    await ctx.send('Pong!')

# Run the bot
bot.run(TOKEN)

In this code:

  • We import the discord and commands modules from discord.ext.
  • We define a bot prefix !, which means users will need to start their commands with this character.
  • The on_ready event is triggered when the bot successfully connects to Discord.
  • The ping command responds with “Pong!” when users type !ping.

4. Running Your Bot

To run your bot, navigate to your discord_bot_project directory in your command line or terminal and execute:

python bot.py

If everything is set up correctly, you should see the message “We have logged in as [bot’s username]” in your terminal. This means your bot is now online and connected to Discord!

5. Inviting Your Bot to a Server

To invite your bot to a server, return to the Discord Developer Portal. In your application settings, go to the “OAuth2” tab and then “URL Generator.” Under “OAuth2 URL Generator,” select “bot” in the scopes section and choose the permissions you want to grant your bot. Copy the generated URL and paste it into your browser. Follow the prompts to invite your bot to one of your servers.

6. Adding More Features

Now that you have a basic bot running, you can start adding more features. For example, let’s add a command that sends a random joke. Create a new file in the discord_bot_project directory named jokes.py and add the following code:

import random

def get_joke():
    jokes = [
        "Why did the scarecrow win an award? Because he was outstanding in his field!",
        "Why don’t skeletons fight each other? They don’t have the guts.",
        "What do you call cheese that isn't yours? Nacho cheese!"
    ]
    return random.choice(jokes)

Update your bot.py file to use this new joke function. Add the following code to bot.py:

from jokes import get_joke

@bot.command()
async def joke(ctx):
    joke = get_joke()
    await ctx.send(joke)

7. Testing and Troubleshooting

Run your bot again with python bot.py and test the new !joke command in your Discord server. If you encounter any issues, check your code for typos or missing imports. Consult the discord.py documentation for additional help and features.

PART II can be found here.


Thank you for following along with this tutorial. We hope you found it helpful and informative. If you have any questions, or if you would like to suggest new Python code examples or topics for future tutorials/articles, please feel free to join and comment. Your feedback and suggestions are always welcome!

You can find the same tutorial on Medium.com.

Leave a Reply