To make a normal command, we create something like this:
newds.Command({name:'ping',// name of command aliases: ['p'],// other names that can be used asyncexecute({arguments}){// function that runs when the command is used *MUST BE ASYNC* // args }});
where you see arguments, you can add any of the following:
message - the message sent
author - the user who sent the message
channel- the channel the message was sent in
guild- the server the message was sent in
send()- a function to send a message in the channel that the message was in
args[]- the arguments (split by a space) after the command
label- the command they used (shows which alias / name they used)
so lets finish the ping command, and add which alias they used:
newds.Command({name:'ping',aliases: ['p'],asyncexecute({send,label}){// getting "send" and "label"send(`Pong! (${label})`);}});
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:
in discord
then if you type !p you will get this:
in discord
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!":
in discord
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 discord
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:
in discord
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):
in discord
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 argument
type - the way it failed
number - which argument failed (starting from 0)
There are three ways an argument can fail. "missing", "incorrect", and "notfound"
missing - the argument parameter required was set to true and the argument wasn't in the command
incorrect - the argument was the wrong type
notfound - 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:
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
new ds.Command({
name: 'amianadmin',
aliases: ['aiaa'],
permissions: ['ADMINISTRATOR'],
async execute({send}) {
send('you are an admin!');
}
});
new ds.Command({
name: 'amianadmin',
aliases: ['aiaa'],
permissions: ['ADMINISTRATOR'],
async execute({send}) {
send('you are an admin!');
},
async faledPermissions({send}) {
send('you are not an admin!');
}
});