Skip to content
← Back to Community
How to Make A Discord Bot - Part 3 (Updated 2021)
Profile icon
FishballNooodle

OMG, we are back with another tutorial, today we will be making EVEN MORE commands. A the end we will be making our custom help command where users will know how to use all your functions.

Clear Command

When there was some mess in your chat and you wanted to delete it, you had to delete it 1 by 1? Yeah, your bot can help you do that.
Here's How the command goes:

@client.command() async def clear(ctx,amount=10): await ctx.channel.purge(limit=amount+1)

In Part 2 of this series, we already explained what is ctx, so be sure to check that out again if you're still not so sure.

OMG, a new argument?
Yes, the amount is an argument we made. It takes in additional input after the command. This is how it's being typed out:
image

In this case, we wanted to find out how many messages they wanted to clear, so we can assign that extra argument to a variable. In the example above, 20 is assigned to the amount.

In line 4 of the code, we used yet another built-in function called purge that comes under channel. But you might ask, where's channel? We didn't pass it as an argument did we? Oh yes, we did, remember that ctx has all the info you need about the context (ironic). We can find the channel that the user typed the command it by doing ctx.channel, we can even find the user who typed it by doing, ctx.user.name. Anyways, channel has a function called purge which literally purges out the amount of message stated - purge means to vomit, in this case, it means to clear the messages.

If you were observant enough, you would have also noticed that when we passed amount, it was also set to 10. This is because we want to make the last number optional. If the user doesn't type any number, it will automatically be set to 10.

Lastly, you would notice we +1 to amount. Why Fishball, are you trolling people? Actually, if you were to think about it or even try it, it will not actually clear the exact number of messages you wanted. Let's Say someone spammed the numbers 1 to 5 separately, and you typed in |clear 5. The moment you typed it, you add 6 messages to the total, the bot then runs the script to clear 5 messages (Assuming you didn't +1) it will clear your command message and 2, 3, 4, 5 only, leaving out 1. Get it? Ok, let's move on to the last part.

Help Command

Before we can make this help command we have to realise that discord has already a default help command which will list all the commands. We don't want that, we want our own custom help command, so we remove the existing default help command by doing this:
client.remove_command('help')
This part is quite self-explanatory.
For the main part of the code, I will show you what you can do before you try it. Your code shouldn't be the same as mine, try injecting some creativity into how you will interact with the user.

Remember how to use ctx.send to send a message

Here's how mine looks like:

@client.command() async def help(ctx): await ctx.send("Help Requested By {}\nBot Prefix: "|"\nCommands Available:\m1. help - This function Lol\n2. ping - Shows your latency with the server\n3. clear <Amount of Messages> - Clears Specified Amount of Messages, default 10 messages".format(ctx.user.name)) #\n creates a newline if you didn't know

Here's the same code just that previously you couldn't see the whole code.
await ctx.send("Help Requested By {}\nBot Prefix: "|"\nCommands Available:\m1. help - This function Lol\n2. ping - Shows your latency with the server\n3. clear <Amount of Messages> - Clears Specified Amount of Messages, default 10 messages".format(ctx.user.name))
This is how it will look like in Discord:
image

We used ctx.user.name which is obviously the name of the user who typed the command.

Lastly, Don't Forget to at the run command to get your bot online and working
client.run(token)

Hooray, we have successfully finished making our help command and easily clear messages. Hope you enjoyed this tutorial, if you faced any problems feel free to voice it out in the comments. Happy Coding:)

Voters
Profile icon
giovandres1325
Profile icon
MusicTrapVK
Profile icon
RohanAbhilash
Profile icon
LeXoLz
Profile icon
JurjenSloetjes
Profile icon
jedi-NydroNydro
Profile icon
Arbuzzz
Profile icon
kevinKareckas
Profile icon
Raf02
Profile icon
Avlone
Comments
hotnewtop
Profile icon
FishballNooodle

Thanks for the Upvotes Everyone!!!
Please comment here if you managed to complete the tutorial.

Profile icon
CYB10

is there a code for node.js?

Profile icon
Tasfinbd

clear command work but, Help command not work

Profile icon
Imzikazodo

For some reason, when I want to run the bot, it says that "bot_token" is undefined. Can someone help me please?

Profile icon
HansGabriel

idk how to make that purge command can someone teach me how?

Profile icon
FishballNooodle

@HansGabriel
await channel.purge(limit=10)

Profile icon
HansGabriel

is this for node.js? @FishballNooodle

Profile icon
HansGabriel

do you have discord? @FishballNooodle

Profile icon
HansGabriel

yeah imma send you the link idk what to do lol
@FishballNooodle

Profile icon
FishballNooodle

@HansGabriel
i didnt recieve any invite

Profile icon
HansGabriel
Profile icon
HansGabriel

@FishballNooodle DONEEEE check your invites!

Profile icon
FishballNooodle

@HansGabriel
i dont have anyth

Profile icon
MarcusLee

I would use it, but I'd rather code my own. Great tutorial though! I like it

Profile icon
FishballNooodle

@MarcusLee
You said you liked it but you didn't upvote it lmao..

Profile icon
MarcusLee

@FishballNooodle I had to go. I just upvoted it.

Profile icon
HenryZelonka1

Thank you, this tutorial is great.

Profile icon
FishballNooodle

@HenryZelonka1
Thanks. Do Check out my other discord turorials

Profile icon
DSACharlie

I want you to write some code for me to fork please?

Profile icon
FishballNooodle
Profile icon
[deleted]

This wont work... ctx.send returns something along the lines of ctx.send() was not awaited.

Profile icon
FishballNooodle

@eekboi
it means you didn't await it lmao
did you do await ctx.send

Profile icon
[deleted]

@FishballNooodle I was saying. Your tutorial didn't work. I know how to make discord bots. For example my tutorials or repls.

Profile icon
Navinor

@eekboi wat he did put await ctx.send

Profile icon
[deleted]

@Warhawk947 I swear I saw ctx.send() only. But my eyes may have decieved me.

Profile icon
FishballNooodle

@eekboi
ok so rule of thumb is to have the async and await function whenever you want to communicate with discord

Profile icon
[deleted]

@FishballNooodle Yeah, I know how the API calls work. Like I said before.

Profile icon
HahaYes

Cool!

Profile icon
JustAWalrus

Too many 'OMG's?

Profile icon
HahaYes

@Wuru lol we gonna hate on others

Profile icon
JustAWalrus

its not hate. also try to look at yourself. @HahaYes

Profile icon
HahaYes

@Wuru sorry, ego too big to look at myself.

Profile icon
JustAWalrus

Jeez, then get a handle of yourself. @HahaYes

Profile icon
HahaYes

@Wuru hehe no, got enough confidence.

Profile icon
JustAWalrus

kk lol xD @HahaYes