A lightweight Discord bot framework designed to receive and handle commands via Discord messages. The bot client is built on top of discord.js, so you retain all other functionality without any issues.
This is one of the simplest examples of registering a command using DiscordCommander. If you are familiar with discord.js, the first half of the code below is simply their demo example.
const DiscordCommander = require("discord-commander");
// Create the Discord client
const client = new DiscordCommander.Bot();
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
})
client.login("token");
// Let's register a command
client.register((registrar) => {
// Using the given registrar, we'll register our command
registrar.register("mycommand", (invocation) => {
console.log("My command has been executed!");
});
});
If we now send !mycommand
in a Discord text channel that the bot can access, we'll get My command has been executed!
in our console!
It all begins with a registrar...
bot.register(registrar => {
// register commands here
});
The register
method will generate a new Registrar
for you to use to register your commands with.
The registrar gives you many different options when it comes to registering commands. Anywhere from a simple callback function, to creating your own Command object and authorization requirements.
The simplemest way to register a command is to use a callback function
command: string
The name of the commandcallback: function(CommandInvocation): void
The callback invoked when the command is executedcontext: any
The context to use for the callback function
var myCallback = (invocation) => {
// Do stuff...
};
bot.register(registrar => {
registrar.register("testcmd", myCallback);
});
In some cases, you may need to specify the context of your callback function, especially when using a class method as the callback function.
class MyClass {
myCommand (invocation) {
// Do stuff...
}
}
var myClass = new MyClass();
bot.register(registrar => {
// Pass in `myClass` as the context of the callback function
registrar.register("mycmd", myClass.myCommand, myClass);
});
In some cases, you may want to register a group of callback functions at once, or even group them all under a specific context
Usage: register(map, [context])
map: JSON Object<string, function(CommandInvocation): void>
The commands to registercontext: any
The context to use for the callback functions
const myCallback1 = (invocation) => { console.log("Callback 1"); };
const myCallback2 = (invocation) => { console.log("Callback 2"); };
bot.register(registrar => {
registrar.register({
"mycmd1": myCallback1,
"mycmd2": myCallback2
});
});
In an object oriented approach, it can get a bit tedious having to alwasy include the context for class methods. Luckily, there's an interface to help you out!
commandRegistrar
ICommandRegistrar
class MyModule implements ICommandRegistrar {
myCallback (invocation) {
// Do stuff
}
// Invoked upon module registration
register (registrar) {
// The context for this class is passed implicitly
registrar.register("mycmd", this.myCallback);
}
}
var myModule = new MyModule();
bot.register(registrar => {
// Register the module
registrar.register(myModule);
});
And like before, you may register multiple modules at a time
bot.register(registrar => {
registrar.register([
myModule1,
myModule2,
// ...
]);
});