Skip to content
Sign upLog in
How to Make A Discord Bot - Part 2 (Updated 2021)
Profile icon

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 so I won't explain how to install Discord on your computer.

Import Modules

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:
commands from discord.ext
To import, copy and paste this:

import discord from discord.ext import commands


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:

In Discord:

Tutorial 2 Picture 1
As shown in the picture, the bot only responded when I had the | symbol.
To code this, copy and paste this:
client = commands.Bot(command_prefix = "|")

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...
Tutorial 2 Pic 2
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:

#This is a Decorator, something used to give more information about a function @client.event #This is normally how you define a function in python, just that we added an async to it #on_ready is the built-in function that will tell us when the bot is successfully online async def on_ready(): #Remember client is the bot, presence is the activity shown in Discord #Note how we used await to complete the async/await method await client.change_presence(activity=discord.Game(name="|help")) #We will then log it to our python console when it's online print("Moderation Bot Online and Running")

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:

#Note how we used a command decorator instead of an event since this is a command not an event @client.command() #Note that the name of the function will be the name of the command you will have to type in discord #We are also passing in ctx as a parameter in this function #You can think of ctx (read as context) as the overview story of the command, the story starts where the user enters the command #ctx stores millions of useful information inside it, for example, username, channel the message was sent it, userid async def ping(ctx): #ctx is also used to send back stuff to Discord await ctx.send("Pong! {}ms".format(round(client.latency * 1000)) #As you can see we rounded the latency *1000 since it was originally in seconds

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.

You are viewing a single comment. View All
Profile icon

and no, i didnt mean .Env, I meant .env
there is a difference.
once again im sorry, i didnt want you to feel offended in any way.