Skip to main content

How to write a Discord bot in Python using discord.py

Discord is the biggest chatting platform for all gamers and people in general out there, and there's no doubt that if you have Discord, you've seen a Discord bot. Everyone's seen Dank Memer, Dyno, Carl-bot, Ticket Tool, and many other famous bots, as well as servers' own custom bots, but how do you make one yourself?

This article will guide you through making a Discord bot in Python using discord.py, a popular library/wrapper for the Discord API. It's also my first blog post :)

Note: this tutorial was written for discord.py 1.7.x. That version is now discontinued, and while it still works as I write, Discord may change things in their API that stop it from working and you'll have to upgrade to v2.0.x (which you should anyway). TLDR; until I update this tutorial, you might want to look somewhere else.

Creating the bot

First of all, open up https://discord.com/developers/applications. Click New Application, and enter the name for your new bot. You can change the name and about me here, and if you want upload an icon. 

Then head to the Bot section on the side, and hit Add Bot, and then Yes, do it!. You've created the bot (upload the picture again if you already did previously)!

Head to the OAuth2 tab and under the OAuth2 URL Generator select Bot under scopes, and then in the selection area that appears below select the permissions you want the bot to have (if it's just for your own server, then Administrator will save you time but if it's a public bot, review this: you can change it later but the bot will have to be reinvited to all its servers or the admins will have to change permissions manually). 

Important: if your bot needs to use slash commands through a discord.py extension, make sure applications.commands is selected alongside Bot.

Under the first section where you selected Bot, there'll be a link. Click Copy and open it in your browser! This prompt will guide you through adding the bot to your server.

Making it do something

First we need to install discord.py. Open up your command prompt, and use pip to install discord.py.

macOS/Linux: python3 -m pip install -U discord.py
Windows: py -3 -m pip install -U discord.py

Open up your favourite Python IDE (this tutorial assumes you know Python syntax and are familiar with the language), and create a new file.

We'll need to import the library, using import discord at the top of our file. And to create our Client instance, a class that handles everything our bot does, put client = discord.Client() below it.

Then, to log in as the bot and start running, we'll need to add client.run(). Back on the Discord Developer Portal, click Copy under Token on the Bot page. Then add client.run("YOUR TOKEN HERE") as the last line of your program, with "YOUR TOKEN HERE" being the token you just copied, wrapped in quotes to make it a string.

If you run this, the bot will show up as Online in Discord, but it doesn't do anything!

Let's do something simple. When someone's message contains "hello", we'll reply. To do this, we need an event handler. An event handler is a function that gets called by the library every time the event happens. We're going to be using the on_message event handler, which is fired every time someone sends a message in a channel the bot can see the messages in.

The discord.py library is asynchronous, so we need to prefix our method names correctly. Before client.run(), create a function called on_message, with a message argument, and the @client.event decorator.

Now, we need to check if it contains the word hello, so add an if statement inside the method checking if message.content.lower() (the lowercase version of the message string) contains hello.

Done? Finally, use message.reply() to reply to the message if it does contain hello. Make sure that this entire method is before  client.run(). You can also choose the message to reply with, it could have a smiley face or anything else, as long as it's a string. Remember, discord.py is asynchronous, so you need to use await in front of message.reply()!

Now run your bot, and type hello in chat! Uh-oh! Stop the Python program. The bot is stuck in an infinite loop (if you used the word hello in your reply) because it's replying to its own message that contains hello! We need to check if the message isn't from the bot. 

Put the first if statement in another, if not message.author == client.user:


Now try it. 


Yay! 

A challenge for you:

Use the on_ready event handler to print a message to the Python shell (using normal print() syntax) when the bot has successfully logged in. This is because it takes a few seconds for the bot to log in every time you run it, and this will help you know when your bot is in action.

Conclusion 

I hope this article helped you make your first Discord bot in Python, or maybe you're an experienced bot developer and this helped you remember something about setting up a new bot.

Please comment down below with your feedback, and I'll be sure to edit the post and change anything if you think something needs improvement. Also feel free to ask for help, especially with the extension challenge.

I also wrote this late at night so apologies if anything's incorrect.

Have fun!

Comments