How to make a bot in discord using python🤖

that’s a snek

Hello, It’s me (again) with a brand new post about how to create a 🤖 in discord with python🐍..

sorry for not posting some stories in a while…..

Hello, It’s me (again) with a brand new post about how to create a 🤖 in discord with python🐍.

You will need a;

  • A discord account
  • Python (Version 3.8 or Higher) and Pip (should come pre-installed)
  • And finally an open and free mind 🧠

ok, firstly let’s go and create a Discord Bot Account), you can get started by going to discord’s developer portal.

first thing you will see is landing page/login page where you have to login(if you have an account already), or create a new account.

Image: By author(me)

Image: By author(me)

once you have finished you will be redirected to the portal’s homepage

IMPORTANT: you will need to verify your email here

when you arrive on the homepage click the “New Application” Button

the button with the red outline

the button with the red outline

After that enter a name for the app. (I named it “TutorialBotForThisPost”)

now click the bot option in the setting sidebar and click add bot.

be careful when you create a bot you can’t delete it after you made it.

be careful when you create a bot you can’t delete it after you made it.

once you made the bot, uncheck the “Public Bot” option since this is still in dev.

After that click copy token [and paste it somewhere. we will need it]

now that the bot is set-up we will need a place to test it so go to discord, and create a server(guild).now go back to the portal and into the bot then click the OAuth2 setting and go to the URL generator then select the bot option in scopes and after that choose these permissions for the bot.

the things I’ve hidden are the id and permission number for security reasons

the things I’ve hidden are the id and permission number for security reasons

After that click copy and put the link in your browser and you will get a discord window asking which server to put your bot is after that your bot will be in a server. You may have to do a Captcha to verify your human(🤖)

That’s a cool background gif

That’s a cool background gif

Now your bot should Arrive to your server.

Now that the Setup Is finished We can go to the Coding Part

Ok, Now to code this.

Now we need to install virtualenv. First open the command-line(cmd, powershell, bash)

if you don’t know what that is it’s a package that creates isolated python enviroments now you download it with the command Below

python3 -m pip install -U virtualenv
py -3 -m pip install -U virtualenv
pip install -U virutalenv

ok now that’s finished create a new folder for the bot.

and open it on your IDE or code editor.

then in the command line type the below command for creating the virtual environment.

py -3 -m virtualenv venv
python3 -m virtualenv venv

it should create new directory(folder) for the venv

then type the command below for activating

#you might want to do this command several times if your on command prompt. since sometimes it doesn't work.
source bin/activate

after that your command line should look like this


after that now you should install the library

                # Linux/macOS
python3 -m pip install -U
# Windows
py -3 -m pip install -U
pip install -U

then it will install the library.

Now that’s settled let’s go for the actual coding bit.

First of create a file called

now enter this code below(I will explain it).

put your token in the ``

put your token in the ``

now in the first line we import the commands() class from the discord.ext this is the class that help us to create the bot

in the next line we create a bot variable and we also set the bot prefix to “!”.

the next few lines are the actual command.

the @bot.command(name = "ping") defines the command and it’s name

underneath is the ping() function this is where, the command calls to run the ping command

ctx is the commands.Context . This allows to send messages and etc..

then finally the runs the bot.

Run the bot with python <filename>.py

A game of ping pong 🏓

A game of ping pong 🏓

and when you type !ping it will return “pong”

Now, we ran the bot by putting token in the, This is a very insecure way of doing it since. if you say put the code in Github per say someone could take the token and use it for nefarious purposes. This is where we can use a package called python-dotenv .

first let’s install it in our virtual enviroment.

python -m pip install -U python-dotenv
pip install -U python-dotenv
python3 -m pip install -U python-dotenv

After it’s downloaded create a new file in the folder called .env

and put this in the file.


After that return to the Main bot file and update it like this.

Updated more secure code.

Updated more secure code.

here we add 2 more import statements.

the load_venv() function of dotenv and environ from os.

the load_venv() function makes all the variables in .env into environment variables.

And the environ finds the environment variable in the [] .

and it is set to a variable called token .

Now you have created a basic discord bot. I hope you have a grasped the basics of this. ~
Github-Repo =
Docs =

This Bot ~
Github Repo =

Bye ✋🏽.

Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies and get more readers

Join other developers and claim your FAUN account now!


Sasen Perera

Student, Thurstan College

A 14 year old boy who likes to code and write



Total Hits