Easy handling of slash commands.
There are three ways to use a custom slash command:
- type
`command argsinto any chat channel /w `command argsto send a whisper to a target starting with the command prefix/w /command argsto send a whisper to a target starting with a slash
Currently the prefix ` is not settable anywhere but in the code.
If strict mode is enabled (it is by default), any message that starts with the prefix and is not a registered command will not be sent. If you want to send a message starting with the command prefix, put a backslash (\) before it and the backslash will be removed and the message sent as normal.
To register slash commands, instantiate an object of Slash passing dispatch as the sole argument to the constructor, and then use Slash#on(command, callback) to register a hook.
callback will be passed only one parameter, args, which is an array of all command arguments with all HTML tags removed, HTML entities decoded, and all extra whitespace removed. args[0] is the command name.
args also has two properties:
raw: the original, raw text of the slash commandtarget: either an integer for the chat channel, or a string of the whisper target. If the whisper command syntax was used,targetwill be the whisper channel (7), otherwise it will be the name of whomever was the whisper target when the command was entered.
In all cases, args and raw will be normalized to always look like prefix form. In other words, args[0] will always begin with the prefix, and raw will always begin with either <FONT> and the HTML encoded prefix, or in some cases, just the HTML encoded prefix.
An instance of Slash has three read-only properties:
prefix: the HTML encoded prefix (only<and>conversions)prefixRaw: the prefix without HTML encodingstrict:trueif invalid commands are silenced, false otherwise
const Slash = require('slash');
module.exports = function Example(dispatch) {
const slash = new Slash(dispatch);
slash.on('command', (args) => {
// ...
});
};