Make a Discord Bot Using Python

vansh4k
5 min readJan 23, 2022

--

Have you ever wanted to create your own discord bot? Do you have an idea in mind but the discord API seems too cumbersome to setup? Well, I got you! This tutorial will walk you through the steps involved in creating a discord bot(which you’ll soon realize aren’t difficult at all!).

Step 1: Create a Discord Bot Account

  1. Make sure that you have a discord account and are logged in.
  2. Head to the application page.
  3. Click on the ‘New Application’ button.

4. Give the application a name and click ‘Create’.

5. Go to the ‘Bot’ tab and then click ‘Add Bot’.

Keep the default settings and customize your bot’s username and picture. Your discord bot has been created! Your next step is to copy the ‘Token’.

Make sure that you do not share the bot’s token with anybody. You can ‘Regenerate’ the token if it accidentally gets shared.

Step 2: Add Your Bot to a Server

In order to add your bot to a server, you need to create an invite URL for it.

To do this, head to the ‘URL Generator’ inside the‘OAuth2’ tab and select ‘Bot’ under the ‘Scopes’ section.

Now, choose the relevant permissions for your bot. Our bot will mainly use text messages so it doesn’t require a lot permissions.

Be careful with the ‘Administrator’ permission

Copy the generated URL and paste it in your browser to add it to a server.

Step 3: Code the Bot Using Python

We’ll be using the discord.py Python library to code the bot.

  1. Creating a Repl

You can develop the bot using a code editor on your computer but we’ll be using Replit to make it easier for people to follow along.

Start by creating a free Replit account and setting up a Python Repl.

2. Setting up Discord.py

Add this code to your console (it imports the discord.py library and connects the discord client):

Running the code will automatically install the library on Replit. You can type ‘pip install discord.py’ into your terminal if you’re coding locally.

3. Getting the token

When you created your bot, you copied a token. This token is essentially a password required by your bot to run. We’ll now create a .env to store the token. You should always use .env files to store private information.

In order to create a .env file in Replit, go to the ‘Secrets’ tab and add a key-value pair. You’ll need to use os.environ so as to add the encrypted token to your code.

‘value’ should contain your token

4. Creating events

Now that you’re all set up, it’s time to do some actual coding!

Discord.py uses ‘Events’ to make your bot function. You can define an event as something you see and then respond to. We’ll be making a simple bot that responds ‘nice’ to every text that contains the number 69 in it.

Add this code to your console. I’ll explain what it does shortly.

Discord.py has various methods (which can be found on the documentation) to make your bot interact in a server. As you can see, I used @clients.event to register an event.

In our code, the on_ready() event is called when the bot is ready to use and on_message() event is called when it receives a message.

The bot prints “{bot name} is online” once it’s active. This is followed by the on_message() event being triggered every time a message is received. Our code checks if the message.content contains ‘69’ in it. If it does, then the bot uses message.channel.send to send ‘nice’.

5. Run the bot by calling the login token

Enter the following code to use the login token and run your bot:

client.run(os.getenv(‘TOKEN’)) logs in using the token and runs your bot.

Step 4: Make the Bot Run Continuously

At this point, your bot is pretty much ready. But you might’ve noticed that your bot goes offline after you close your script. What if I tell you that there’s a way to make your bot run continuously?

Replit continues running a web server even after a script is closed. Hence, we’ll create a web server to keep our script running. Since a web server only stays active for one hour after its last request, we’ll also have to use a 3rd party service to ping our server.

  1. Code a web server using flask

Start by creating a new file titled ‘server.py’ .

Add this code to the file:

Head to your main file, import the server into it and call the keep running() function just before your run command.

Your final code should look somewhat like this:

2. Setup UpTime Robot

Create a free account on https://uptimerobot.com/

Once you’re logged in, click ‘Add New Monitor’.

Copy the URL of your server.py file and setup your monitor.

The monitor will ping your server every 5 minutes.

You’re Done!

Congratulations! You now know how to make a discord bot and run it continuously using python.

Your bot should function like this lol:

Source Code | Documentation

--

--

vansh4k
vansh4k

Written by vansh4k

I am a typical high schooler grinding to make it big in tech.

No responses yet