Creating and launching a discord bot with Python

Creating a discord bot is actually a quite easy task that can be completed with a rudimental knowledge of python (all depending on how complex you wish your bot to be). However, you cannot simply code a bot on your python discord and launch it from there: you need to follow a quick, yet necessary procedure that includes logging into the Discord Developer Portal.

Where to start?

To create a Discord bot, you need to have:

  • A discord account
  • A python workspace

The procedure will be the following: first, we are going to set up an empty bot on the Discord Developer Portal with specific authorizations, accessible through a generated token. Then, we are going to add that bot to our discord server. As the last step, we are going to code the behavior of our discord bot, in this case, to print a randomly generated number (could not be simpler).

Discord Developer Portal

The Discord Developer Portal is an interface that lets you create and manage your own applications on discord servers. Some of these applications are actually bots. First thing, login into The Discord Developer Portal with your discord account.

Then, you are going to create a New Application.

Click on the New Application Button

Make sure to fill in all the general information details, including the image of the bot, I believe this will have a part in generating the Token.

Now we have to specify what are the details of your application. To do so, click on OAuth2 > URL Generator.

URL Generator

The best definition (the use case) of your application is a bot:

Note that once a bot has been created, it does not have unlimited permissions to any server. This could be dangerous, especially if you let in bots with malicious code. Therefore, you will need to select only the actions you wish your bot to have access to when running. To do this, you are first going to click on URL generator:

I only need the send messages authorization

Now that you have completed your bot settings, the portal will generate a new URL for you: copy it!

To activate the bot, now go back to OAuth2 > General, and copy the URL in the Default Authorization Link at the bottom:

Retrieve Token

To get your Token, simply click on the third window of the menu: Bot, and copy (or reveal) your Token. This will be used when coding the bot in your python workspace.

Inviting the Bot into your server

To invite the bot into your server simply copy and paste on your Browser the URL you have been using during authentication:

Authorizing the bot into your server

Once added, the bot will appear within your members, still inactive though because you will first need to activate it using your python code:

Active Bot

Coding the bot behavior in Python

After inviting the bot into your server, you can use the following code on a .py file to run it:

#stops running with our code
import discord
import random

client = discord.Client()

@client.event
async def on_ready():
    #runs when the code is activated on local
    print('now running', client)

@client.event
async def on_message(message):
    #everytime a new message is written on the server
    username = str(message.author).split('#')[0]
    user_message = str(message.content)
    channel = str(message.channel.name)
    print(message.author, message.content)

    if user_message.lower() == '!random':
        response = random.random()
        await message.channel.send(response)
        return

client.run(<YOUR TOKEN>)

What this code does is simply scan every new message on the server, and when this message is “!random” it will generate a random number as a response to that message. Note that to make this code run, you will need your Token.

Test the bot!

Looks great! Why don’t you login into Python Kai and try it yourself?

Our bot works!!!

How to make this bot run forever

Once your local python code stops running (for example you wish to shut down your PC), your bot will no longer work. You need to run your bot on a service that is online 24/7, in a further article I will give you proper instructions on how to create an EC2 instance that will host your code 24/7.

Join our free programming community on discord, learn how to code, and meet other experts

One thought on “Creating and launching a discord bot with Python

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: