Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Getting started

adri326 edited this page Oct 30, 2017 · 3 revisions

Installation

You can install chloecore by adding this repository in your package.json, by adding:

  "dependencies": {
    "chloecore": "https://github.com/AperLambda/chloecore.git"
  }

Then, import chloecore in your code with:

var chloecore = require("chloecore");

Logging in on discord

To log in, just call

chloecore.login("<your token>");

You could also save your token in another file, for example token.json, then load the file using require("./token.json").

Basic command

We will now create a basic ping command, and load it using the commands manager. First, create a folder where all the commands will be in. We will call it commands. Create a javascript file in it, and name it with the name of the command (ping.js).

Then, add the following code:

module.exports.handler = (args, context) {
  context.channel.send("Pong!");
}

The commands manager, which we will setup in a moment, will load this javascript file and trigger the handler function (module.exports just means that the function will be exported and available from outside of the file). args is a list of arguments, which the user has sent (they are separated with spaces)

Now, let's setup the commands manager. In our main javascript file, where we already had loaded chloecore and logged in, add the following code:

var commands_handler = chloecore.commands.group().set_prefix("&").set_commands_directory("./commands");
commands_handler.load_commands().then().catch(console.error);

This will first create a new instance of a commands group (chloecore.commands.group()), will then set its prefix (set_prefix("prefix"); prefix can be any string) and will finally set its directory, where he will be looking it for commands (set_commands_directory).

Then, we load the commands. The load_commands() returns a Promise, so we handle the success (.then()) and the fail (.catch()) of it. You could make the bot log in only after having loaded the commands:

commands_handler.load_commands().then(_ => {
  chloecore.login(require("./token.json"));
}).catch(console.error);

Middleware

There are two places where a middleware can be added: in the commands group and in each command.

To add a middleware in the commands group, you can call set_middleware(middleware). middleware will have to be a function with (command, args, context) as arguments; command is the command that is about to be triggered, args is a list of arguments given by the command call (if the command uses arguments, these will also be passed with their name in the list), context is the context of the command.

To add a middleware in a command, you can add module.exports.middleware = function(command, args, context) { }. The arguments for this function are the same as the one above.

The given command in the middleware will only be triggered if the middleware returns true. If the middleware returns false, the commands handler will not look at any subcommands, but won't send back any error message to the user.

Arguments

Arguments use the following syntax:

  • <argument>: a simple, non-typed, required argument
  • [<argument>]: a simple, non-typed, optional argument
  • <argument:type>: a simple, typed argument
  • <argument:type 0..>: an argument which has a minimum value (0) but no maximum value
  • <argument:type 0..5>: an argument which has a minimum value (0) and a maximum value (5)

They can be easily added to a command with module.exports.args = ["<argument>"];

After having added those arguments to a command, the command will require them (unless module.exports.strict_mode is set to false) to be ran. A parsed argument from the user will then be added to the list of arguments given in the handler, named by the name of it. For example:

// log.js
module.exports.handler = (args, context) {
  console.log(args);
};
module.exports.args = ["<foo:int>"];

Will output, when triggered with log 5: [0: "log", 1: "5", "foo": "5"]

Clone this wiki locally