Skip to content
Sign UpLog In
This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs
36

How to Make A Discord Bot - Part 2 (Updated 2021)

FishballNooodle
FishballNooodle

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.

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

import discord from discord.ext import commands

Token

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)

image

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
client.run(token)
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.

2 years ago

Voters

Comments

TopNew
6
HarshVardhan19
HarshVardhan19

[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 .env file:
TOKEN=your_token
Remember, there are no spaces, quotes or double-quotes in the .env file
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 os.environ.get("TOKEN"). The .env file however, will not be visible to anyone else except the people you give editing permissions.

2 years ago
5
FishballNooodle
FishballNooodle

@HarshVardhan19

can we report this guy, he just literally used me as an advertising space.
You only have 0 upvotes you have no rights to just steal my viewers through my posts comments.

2 years ago
1
HarshVardhan19
HarshVardhan19

@FishballNooodle
alright im sorry for that, im extremely sorry if you felt offended by that, but, please try to show me if you can see my bot's tokens in my pinned repls

2 years ago
1
HarshVardhan19
HarshVardhan19

@FishballNooodle
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.

2 years ago
1
HarshVardhan19
HarshVardhan19

@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?

2 years ago
2
FishballNooodle
FishballNooodle

@HarshVardhan19

if you didn't see the comments below, I already discussed that withe the other replers

2 years ago
2
HarshVardhan19
HarshVardhan19

@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.

2 years ago
1
DynamicSquid
DynamicSquid

@HarshVardhan19
This was 10 months ago, but now .env files are deprecated: https://docs.replit.com/repls/secrets-environment-variables

The way shown in the tutorial is correct

2 years ago
1
HarshVardhan19
HarshVardhan19

@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.

2 years ago
1
DynamicSquid
DynamicSquid

@HarshVardhan19
Oh, no worries! I actually didn't know the time this was posted, and thought it was a recent. But then I checked that this was 10 months ago, so I just updated by comment instead of deleting it

2 years ago
1
HarshVardhan19
HarshVardhan19

@DynamicSquid
Alrighty! I do think a better way to publish this tutorial would've been a new post, but anyway, it is what it is.

2 years ago
2
minecraftMaaz
minecraftMaaz

image

can someone help me here?
its working if i remove the ping

@fishballnoodles

7 months ago
1
DomonkosTaschne
DomonkosTaschne

replit


what is the problem?

5 months ago
1
films5984
films5984

where is discord.ext and import commands thing

7 months ago
1
asherpalmer5
asherpalmer5

pls help there is an error
Screenshot 2021-10-12 10.55.49 AM

1 year ago
2
aShortKid
aShortKid

@asherpalmer5
your client.run have to be like this: client.run("your_token"). You forgot the ".

9 months ago
1
quasilius
quasilius

@asherpalmer5
I wouldn't show that Client ID if I were you.

6 months ago
1
RixTheTyrunt
RixTheTyrunt

Can that command be, example

await client.change_presence(activity=discord.Video(name="angus.help"))

Or it can't be? Tell me what can replace this part: discord.Game(name="angus.help")

1 year ago
1
RixTheTyrunt
RixTheTyrunt

Model named discord cannot be installed

1 year ago
1
FishballNooodle
FishballNooodle

@RixTheTyrunt

I dont even read comments here ahahaha msg me on discord any ways yes u need to use discord.Game

1 year ago
1
NewCoder103
NewCoder103

cmon how does the client.run(token) work
i cant get it to work

2 years ago
2
EmiratesPTFS
EmiratesPTFS

@NewCoder103
u have to do client.run('put your token here')

example:
client.run('asdawdasdwadaw3289es2d29d2829')

2 years ago
1

@FishballNooodle
this doesnt work bro

2 years ago
4
FishballNooodle
FishballNooodle

@FluidGaming

What IDE you using?
If you are not using repl u can read the comments below.
Else Rely to this message with the Error Message

2 years ago
1
aceprogramer
aceprogramer

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.

2 years ago
2
FishballNooodle
FishballNooodle

@aceprogramer
Did you install it in your computer using the pip method?
Importing the files by

import discord from discord.ext import commands

is somthing diffrent

2 years ago
Load more