Normal Commands
To make a normal command, we create something like this:
where you see arguments
, you can add any of the following:
message
- the message sentauthor
- the user who sent the messagechannel
- the channel the message was sent inguild
- the server the message was sent insend()
- a function to send a message in the channel that the message was inargs[]
- the arguments (split by a space) after the commandlabel
- the command they used (shows which alias / name they used)
so lets finish the ping command, and add which alias they used:
overall the file should look like this:
we can then run the bot using node
and now if you go to your discord server, and type !ping
you will get this:
then if you type !p
you will get this:
You can also check if people have permissions, all you need to do is add the permissions
variable:
since I am an admin in my server, it will display the text "you are an admin!":
we can also add a function to run if the person doesn't have the needed permissions:
so if someone without admin permissions run this command, they will get this:
In the failed permissions function, you can get any of the arguments that you can use in the execute function plus these:
permissions[]
- the permissions that the sender didn't have
Although this feature is mostly for slash commands, you can use it on normal commands too. If you want, you can add certain arguments that a command needs to follow, like this:
When you run the command, you will get this:
Whenever you specify an argument type, it will change the argument to that value. If the argument fails the transformation, a different function will be called:
Now when we run it in discord, it will do this (if the second argument is not a number):
The argument type can be any of these:
any
string
number
boolean
user
(user mention)channel
(channel mention)role
(role mention)mention
(role or user mention)
In the failed arguments function, you can get any of the arguments that you can use in the execute function plus these:
argument
- incorrect argumenttype
- the way it failednumber
- which argument failed (starting from 0)
There are three ways an argument can fail. "missing", "incorrect", and "notfound"
missing
- the argument parameterrequired
was set totrue
and the argument wasn't in the commandincorrect
- the argument was the wrong typenotfound
- if the argument was some kind of mention, and the bot couldn't find the user / role / channel
We can use the type
parameter like this:
Then if we run the command in discord:
Another feature with commands is adding cooldowns. You can add them like this:
every value of cooldown
is 1 second. You can also add a failedCooldown
Then we get this:
Last updated