Learn to make a simple Discord Bot using Python.

Learn to make a simple Discord Bot using Python.

Create your first discord bot using python!

ยท

3 min read

Setting things up

1. Create a Discord Application

To continue making our Discord Bot, we need to create a Discord Application, Create Bot and save the token. If you don't know how to do this, learn here. Invite the bot made to your discord guild/server.

2. Install the python packages

To create a discord bot that will respond to our commands, we need to install an API wrapper that interacts with the Discord API and makes our work easy. The most preferred discord API wrappers are:

For this tutorial, I will be using Py-Cord. We install this package using the following command:

pip install py-cord

Let's fire up our editors and code!

Coding the bot

1. Creating a discord connection making an event and making a command.

The first step is to create a connection with Discord. With the help of our package Py-Cord, we can do so by creating an instance of Bot:

# main.py
import discord


bot = discord.Bot()

Over here, using the Bot instance, we have created an object that represents our connection with Discord.

2. Making an event

We need a helper event to let us know that the bot has established its connection with Discord and is online. We can do so like this:

# main.py
import discord


bot = discord.Bot()

@bot.event
async def on_ready():
    print(f"{bot.user} is online!")
    print(f"Discord Version: {discord.__version__}")

on_ready is an event handler that when used under the .event decorator gets called when the bot has established its connection with Discord. Our on_ready event prints a simple message when it is online.

3. Making a slash command

We need to make a slash command that will respond to the user. Let's make a basic ping-pong command.

# main.py

import discord


bot = discord.Bot()

@bot.event
async def on_ready():
    print(f"{bot.user} is online!")
    print(f"Discord Version: {discord.__version__}")

@bot.slash_command()
async def ping(ctx):
    await ctx.respond("Pong!")

We can create commands using the .slash_command() decorator. Over here, I have made a "ping" function that responds to the user with a "Pong!" via the help of the .respond() function. Note that ctx is a parameter for discord.ApplicationContext, it can be named whatever you like.

4. Running the bot

Last but not least, we need to run the bot. We can do so by simply calling the .run() function with our token, and then finally running the file.

# main.py
import discord
import os


bot = discord.Bot()

@bot.event
async def on_ready():
    print(f"{bot.user} is online!")
    print(f"Discord Version: {discord.__version__}")

@bot.slash_command()
async def ping(ctx):
    await ctx.respond("Pong!")

bot.run(os.getenv('TOKEN'))

Running the file for Windows:

py main.py

Running the file for Mac & Linux:

python3 main.py

After doing this, we see a nice message on our terminal!

Bot is online!
Discord Version: 2.0.0

Conclusion

Congratulations! You have now made a basic Discord bot using Python.

You now know:

  • About Py-Cord
  • How to establish a connection with Discord
  • How to handle events
  • How to create and handle slash commands
  • How to run your Discord application

I hope you found this blog helpful ๐Ÿ˜„โœŒ๏ธ

ย