How to Make A Discord Bot - Part 2 (Updated 2021)
Ok, so assuming you've already read Part 1 of my Tutorial Series, let's get straight into the good stuff!
Discord Rewrite Branch uses the async/await method, for those of you who don't know, you will understand it later.
By the way, I'm using repl.it so I won't explain how to install Discord on your computer.
Firstly, let's import the discord modules into python. We will be importing 2 modules, which is enough to last you till quite an advanced level. The 2 modules are namely:
To import, copy and paste this:
I previously used the token to string method which is unsafe.
We will be using Secrets that Replit has integrated into all repls.
Set the key name to
bot_token and the value to the copied string. (Should be a string of random characters)
Defining Bot Object
Most of the code will heavily rely on this. To define your bot, we will be using the
Bot function from the
commands module. It will take in one main parameter which is the bot prefix. A bot prefix is a character you have to type before the actual command for it to work, for example:
Do note that you can change the prefix to anything you like, as long as there are no spaces
Programming the On-Ready Event
Your bot is almost about to go online, the last step is to get the bot to print out a message when it successfully goes online and to also add an activity it's doing like this...
This is called an event, an Discord has a built-in function just for what we want to do.
The code below will explain what each step does:
Lastly, Don't Forget to at the run command to get your bot online and working
Now if you hit the run button, it should start installing the relevant lock files.
Once you see it says "Moderation Bot Online and Running" you know you have successfully made a bot.
But what is a bot without a command? Let's make 1 Simple command to show the ping of the user. The ping is the latency between the transmission of data between the server and the user. It is usually measured in milliseconds(ms) but Discord measures it in seconds(s) - weird.
Here's the code:
Oh my, it surely has been a long Tutorial hope you enjoyed it, stay tuned for the next one:)
If you encountered any problems along the way, feel free to voice it out in the comments section below.
[EDIT] This is now depricated since replit introduced a new method for creating environment variables, more info at https://docs.replit.com/repls/secrets-environment-variables
This can be very wrong way to put tokens if the bot is being hosted on repl.it, because, most of us do not pay, and we keep our projects public. So putting the token in the way you showed, it would possibly lead to a token leak. The better practice when writing a discord.py bot on repl.it is putting the token in a
.env file. Then you'll have to
import os at the top of your file, assuming you wrote this in the
Remember, there are no spaces, quotes or double-quotes in the
Now the token is stored in
.env file with the name
TOKEN. You'll just have to grab it in the main.py file using
token = os.environ.get("TOKEN")
Use it exactly as I showed, the token is now stored in the main.py file's
token variable, now even if the main.py file is public, you will never find any token in it, just a simply
.env file however, will not be visible to anyone else except the people you give editing permissions.
@FishballNooodle also, you should know you're giving away partial knowledge to people through this way of putting token on repl.it, if the projects of the people who use this method gets discovered by other people, it will most probably lead to a token leak, which can be extremely disastrous for their servers if the bot has permissions in them. And I'm fairly certain you weren't aware of that were you?
@FishballNooodle you mean bout trinket or stuff, I'm sorry I don't have any idea what's trinket, I did a google search on it few hours ago, but it just got me more confused. But I was talking bout the bots hosted here, on repl.it. People who write and host their bots on repl.it with the free plan, should be encouraged to use
.env files because leaking authorization tokens is obviously not a very good coding practice.
@DynamicSquid I am aware of the updates on
.env files, however, those updates happened about a month or 2 ago, and this post has been updated in 2021.
What I mean to say, is that previously, this post just told users to put token in the main file, which is entirely incorrect, I understand that what I commented is now outdated, but 10 months ago, that is, the time when I commented it,
.env files were used to create environment variables.
I'm sorry but I do not keep track of every single comment of mine.
Mine just opens and then closes immediately. I'm not using repl.it for the same reason as nathanwong123 but I'm also not using trinket. I've imported the libraries already.