UNPKG

ts3-ladon

Version:

Ladon is a versatile framework designed for creating powerful TS3 (TeamSpeak 3) query bots. With Ladon, developers can effortlessly implement commands, handle events, and utilize a variety of utility functions to enhance their bot's capabilities. Whether

96 lines (95 loc) 5.26 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Command = void 0; const lodash_1 = __importDefault(require("lodash")); const connection_manager_1 = require("../connection-manager"); class Command { static callCommand(connectionId, client, text) { var _a; const connection = connection_manager_1.ConnectionManager.instance.get(connectionId); if (lodash_1.default.isUndefined(connection)) { throw new Error(`Connection ${connectionId} does not exist.`); } const args = text.slice(connection.botPrefix.length).split(/ +/); const command = (_a = args.shift()) === null || _a === void 0 ? void 0 : _a.toLowerCase(); const commandsOnInstance = this._commandList.get(connectionId); if (!command || !commandsOnInstance || !commandsOnInstance.has(command)) { client.message(`[b][color=#ff0000] ERROR: [color=#FFFFFF] Command ${command} does not exits.`); return; } const handler = commandsOnInstance.get(command); handler(client, lodash_1.default.join(args, " "), connection, ...args); } static loadCommands(connectionId, allCommands) { const connection = connection_manager_1.ConnectionManager.instance.get(connectionId); if (lodash_1.default.isUndefined(connection)) { throw new Error(`Connection ${connectionId} does not exist.`); } const commandsMap = this._commandList.get(connectionId) || new Map(); lodash_1.default.forEach(allCommands, (command) => { commandsMap.set(command.name, (client, fullText, connection, ...args) => { var _a; if (!!command.fullText) { if (lodash_1.default.isUndefined(args[0])) { client.message(`[b]${connection.botColor} USAGE: [color=#FFFFFF] ${connection.botPrefix}${command.name} [ ${lodash_1.default.get(command, "args.0.name")} ]`); return; } command.handler(client, fullText, ...args); } else { const commandArgs = []; let argsCorrect = true; if (!lodash_1.default.isEmpty(args)) { if (args.length !== ((_a = command.args) === null || _a === void 0 ? void 0 : _a.length)) { let args = ""; lodash_1.default.forEach(command.args, (arg) => { args += `[ ${arg.name} ] `; }); client.message(`[b]${connection.botColor} USAGE: [color=#FFFFFF] ${connection.botPrefix}${command.name} ${args}`); return; } lodash_1.default.forEach(command.args, (arg, current) => { const currentArgument = args[current]; let parsedArg = Number(currentArgument); if (arg.type === "number") { if (lodash_1.default.isNaN(parsedArg)) { argsCorrect = false; client.message(`[b]${connection.botColor} CMD: [color=#FFFFFF] ${connection.botPrefix}${command.name} || parameter ${arg.name} must be number.`); return; } } if (arg.type === "string") { if (!lodash_1.default.isNaN(parsedArg)) { argsCorrect = false; client.message(`[b]${connection.botColor} CMD: [color=#FFFFFF] ${connection.botPrefix}${command.name} || parameter ${arg.name} must be string.`); return; } else { parsedArg = currentArgument; } } commandArgs.push(parsedArg); }); } if (!argsCorrect) { if (command.help) { lodash_1.default.forEach(command.help, (help) => { client.message(`[b]${connection.botColor} CMD: [color=#FFFFFF] ${connection.botPrefix}${command.name} || ${help}`); return; }); return; } client.message(`${connection.botPrefix}${command.name} ${lodash_1.default.join(lodash_1.default.map(command.args, (arg) => `[${arg.name}]`), " ")}`); } command.handler(client, fullText, ...commandArgs); } }); }); this._commandList.set(connectionId, commandsMap); } } exports.Command = Command; Command._commandList = new Map();