Normal Commands

To make a normal command, we create something like this:

new ds.Command({    
    name: 'ping', // name of command    
    aliases: ['p'], // other names that can be used    
    async execute({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:

new ds.Command({
    name: 'ping',    
    aliases: ['p'],    
    async execute({send, label}) { // getting "send" and "label"
        send(`Pong! (${label})`);    
    }
});

overall the file should look like this:

example.js
const Discord = require('discord.short');
const ds = new Discord.ShortClient('example');

ds.login(
{  
    "botToken": "token",
    "mongo": {    ]
        "username": "ephf",    
        "password": "password",    
        "database": "example"  
    },  
    "heroku": {    
        "name": "example"  
    }
});​

ds.setPrefix('!');

new ds.Command({
    name: 'ping',    
    aliases: ['p'],    
    async execute({send, label}) {
        send(`Pong! (${label})`);    
    }
});

we can then run the bot using node

$ node example.js

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:

new ds.Command({
    name: 'amianadmin',    
    aliases: ['aiaa'],
    permissions: ['ADMINISTRATOR'],    
    async execute({send}) {
        send('you are an admin!');    
    }
});

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:

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!');
    }
});

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:

new ds.Command({
    name: 'add1',
    arguments: [
        {
            type: 'number',
            required: true
        }
    ],
    execute({send, args}) {
        send(args[0] + 1);
    }
});

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:

new ds.Command({
    name: 'add1',
    arguments: [
        {
            type: 'number',
            required: true
        }
    ],
    execute({send, args}) {
        send(args[0] + 1);
    },
    failedArguments({send}) { // here
        send('That\'s not a number!');
    }
});

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

new ds.Command({
    name: 'add1',
    arguments: [
        {
            type: 'number',
            required: true
        }
    ],
    execute({send, args}) {
        send(args[0] + 1);
    },
    failedArguments({send, type}) {
        if(type == 'missing') {
            send('You are missing the first argument!');
        } else {
            send('That\'s not a number!');
        }
    }
});

Then if we run the command in discord:

Another feature with commands is adding cooldowns. You can add them like this:

new ds.Command({
    name: 'ping',
    aliases: ['p'],
    cooldown: 10, // here
    execute({send}) {
        send('Pong!');
    }
});

every value of cooldown is 1 second. You can also add a failedCooldown

new ds.Command({
    name: 'ping',
    aliases: ['p'],
    cooldown: 10,
    execute({send}) {
        send('Pong!');
    },
    failedCooldown({send, timeleft}) {
        send(`Wait for another \`${timeleft}s\`!`);
    }
});

Then we get this:

Last updated