How to Make A Discord Bot - Part 3 (Updated 2021)
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:

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:

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:

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:

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:)

You are viewing a single comment. View All
JustAWalrus

Too many 'OMG's?

HahaYes

@Wuru lol we gonna hate on others

JustAWalrus

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

HahaYes

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

JustAWalrus

Jeez, then get a handle of yourself. @HahaYes

HahaYes

@Wuru hehe no, got enough confidence.

JustAWalrus

kk lol xD @HahaYes