Python Discord Bot

Frank Hui

Get Help

Table of Contents

In this workshop, I'll show you how to make a basic Discord Bot. The goal of this is to provide a platform for you to be creative, so don't be afraid to riff on this workshop and explore!
You'll need a Discord account and Google account.

Setting up a Discord Bot

This assumes that you have a Discord account. If you don't, click the button below to sign up.

Register for Discord

Next, we want to go to the Discord Developer Portal. Clicking the button below will bring you to the Portal.

Discord Developer Portal

Sign in if you need to, and click on 'New Application' and create a name for the bot and fill it in. Then click 'Create'.

Go to the Bot tab on the left menu and click 'Add Bot' and then click 'Yes, do it!'

Now go to the OAuth2 tab on the menu on the left, copy the Client ID by clicking the 'Copy' button and replace the bold text below (you can edit it!) with the Client ID you just copied.

This is your OAuth2 Redirect Link.

Go back to the Developer Portal, now scroll up to the button that says 'Add Redirect' and paste in your OAuth2 Redirect Link and click the green 'Save Changes' button.

Go to the place that says 'Select Redirect URL' and select the OAuth2 Redirect Link you just added. This link lets you add your bot to the server. Paste the link into a new tab and follow the instructions on screen to add a bot to your own server.

You've now created a bot! Don't close the Developer Portal yet because we'll need it later.

Setting up the environment

We'll be using a pre-made developing environment called Google Colab. Click the button below to do a really quick workshop on setting up Google Colab. You'll need a Google account if you want to use Google Colab.

Google Colab

Pick a name for your Discord bot, and rename the file if you want. Keep the .ipynb file extension.

Writing the Code

"Let's start coding already!"
Alright, let's get started. Type this command into the box and hit run. Do NOT copy and paste! You will learn better if you type it out!

                  !python3 -m pip install -U[voice]

Here's what your screen should look like afterwards.

What this command did was install the Discord python package that we will use to build our Discord bot. Everytime you leave Google Colab, the environment that is used to run the code is deleted, so you'll have to run this code everytime you open up Google Colab to run the bot.

Let's create a new code box by clicking '+ CODE' on the top left corner and write our imports in:



import discord import asyncio

This imports the discord package we just installed, as well as a tool we'll need later on.

Next, we want to write in the same box:



TOKEN = 'YOUR TOKEN' client = discord.Client() loop = asyncio.new_event_loop() asyncio.set_event_loop(asyncio.new_event_loop()) asyncio.get_event_loop() @client.event async def on_message(message): @client.event async def on_ready(): print('Logged in as') print( print( print('------')

This code first creates a variable which is then set to equal a client that connects to Discord, resets an async loop (which you don't need to understand or know about), defines an action that is triggered when the client is ready to accept commands, defines an action that is triggered when the client receives a message, and finally runs the client with the token. A token is basically the password which your bot uses to login to the Discord server.

Now, go back to the Bot tab on the left menu in the Developer Portal, then go ahead and click 'Copy' right below where it says 'Token'. Now paste it into the code where it says 'YOUR TOKEN'. Don't remove the apostrophes!

NEVER give this token to anyone else, or else they can log on to your bot!

Okay, now let's focus on this part:



@client.event async def on_message(message):

This piece of code listens to Discord and waits for someone to send a message. This is called a function, or a method. You can think of a function as an input/output machine, where if you put in x, you get y out. In this case, our x is the message that is sent in the server, and out y is the response we'll give. Our next step is to write our response.

For this workshop we'll just do something very basic. If the message "!ping" is sent, we'll send back "Pong!"

We set up an if statement:



@client.event async def on_message(message): if message.content == "!ping":

This if statement basically translates to: "When a message is received, if the message's contents are !ping, then do something". We will now define the "something" that it does. You might notice that the if statement is indented one tab. This indicates that the if statement 'belongs' to the on_message() function, and will run whenever on_message() does.



@client.event async def on_message(message): if message.content == "!ping": await"Pong!")

Now this code translates to: "When a message is received, if the message's contents are !ping, then send a new message in the same channel the original message was sent that says "Pong!""

Okay, we're done! Your code should look something like this:

Now just run the code by hitting the play button on the left side of the box, and try sending !ping to a channel that the bot is in.

Back to Workshops

Learn more Python