camelot-unchained
Version:
Camelot Unchained Client Library
69 lines (68 loc) • 2.33 kB
JavaScript
/**
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
;
var events_1 = require('../events');
var registry = [];
function prefix(command) {
return "slash_" + command;
}
/**
* Registers a method to be executed when a slash command is entered in the chat
* window.
*/
function registerSlashCommand(command, helpText, callback) {
var found = false;
for (var i = 0; i < registry.length; ++i) {
if (registry[i].command == command) {
found = true;
break;
}
}
if (!found) registry.push({ command: command, helpText: helpText });
events_1.default.on(prefix(command), callback);
}
exports.registerSlashCommand = registerSlashCommand;
/**
* Un registers a slash command. WARNING: this will register all occurances
* of this command. If this was registered by multiple modules, ALL other modules
* listening for this command will stop working.
*/
function unregisterSlashCommand(command) {
var index = -1;
for (var i = 0; i < registry.length; ++i) {
if (registry[i].command == command) {
events_1.default.off(command);
index = i;
break;
}
}
if (index >= 0) registry.slice(index, 1);
}
exports.unregisterSlashCommand = unregisterSlashCommand;
/**
* parseMessageForSlashCommand is meant to be run on every entered line of text
* entered into the chat window. If the line of text was a registered slash
* command then an event is fired for that command and the function returns true.
* If no slash command is found, the function returns false and the chat system
* should handle it however it would normally.
*/
function parseMessageForSlashCommand(command) {
var split = command.split(/ (.+)/);
var found = false;
for (var i = 0; i < registry.length; ++i) {
if (registry[i].command == split[0]) {
events_1.default.fire(prefix(split[0]), split[1]);
found = true;
break;
}
}
return found;
}
exports.parseMessageForSlashCommand = parseMessageForSlashCommand;
function getSlashCommands() {
return registry.slice(0);
}
exports.getSlashCommands = getSlashCommands;