UNPKG

@csegames/camelot-unchained

Version:

Camelot Unchained Client Library

73 lines (72 loc) 2.5 kB
"use strict"; /** * 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/. */ Object.defineProperty(exports, "__esModule", { value: true }); 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 cmd = command.toLowerCase(); var found = false; for (var i = 0; i < registry.length; ++i) { if (registry[i].command === cmd) { found = true; break; } } if (!found) registry.push({ helpText: helpText, command: cmd }); events_1.default.on(prefix(cmd), 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 cmd = command.toLowerCase(); var index = -1; for (var i = 0; i < registry.length; ++i) { if (registry[i].command === cmd) { events_1.default.off(prefix(cmd)); 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 cmd = split[0].toLowerCase(); var found = false; for (var i = 0; i < registry.length; ++i) { if (registry[i].command === cmd.toLowerCase()) { events_1.default.fire(prefix(cmd), split[1]); found = true; break; } } return found; } exports.parseMessageForSlashCommand = parseMessageForSlashCommand; function getSlashCommands() { return registry.slice(0); } exports.getSlashCommands = getSlashCommands;