UNPKG

pockybot

Version:

Spark bot that handles team recognition

88 lines (87 loc) 4.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const trigger_1 = require("../../models/trigger"); const constants_1 = require("../../constants"); const tableHelper_1 = require("../parsers/tableHelper"); const database_1 = require("../../models/database"); const config_action_1 = require("../../models/config-action"); const tableHelper_2 = require("../parsers/tableHelper"); const command_1 = require("../../models/command"); const xmlMessageParser_1 = require("../parsers/xmlMessageParser"); class StringConfig extends trigger_1.default { constructor(config) { super(); this.config = config; } isToTriggerOn(message) { if (!(this.config.checkRole(message.personId, database_1.Role.Admin) || this.config.checkRole(message.personId, database_1.Role.Config))) { return false; } let parsedMessage = xmlMessageParser_1.default.parseNonPegMessage(message); return parsedMessage.botId === constants_1.default.botId && parsedMessage.command.toLowerCase().startsWith(command_1.Command.StringConfig); } async createMessage(message) { let parsedMessage = xmlMessageParser_1.default.parseNonPegMessage(message); let words = parsedMessage.command.trim().split(' ').filter(x => x !== ''); let newMessage; if (words.length < 2) { return { markdown: `Please specify a command. Possible values are ${Object.values(config_action_1.ConfigAction).join(', ')}` }; } switch (words[1].toLowerCase()) { case config_action_1.ConfigAction.Get: newMessage = this.getConfigMessage(); break; case config_action_1.ConfigAction.Set: if (words.length < 4) { newMessage = 'You must specify a config name and value to set'; break; } if (this.config.getStringConfig(words[2]) .map(x => x.toUpperCase()) .includes(words[3].toUpperCase())) { newMessage = `Config value "${words[3]}" already exists in string config under name "${words[2]}".`; break; } await this.config.setStringConfig(words[2].toLowerCase(), words[3]); newMessage = 'Config has been set'; break; case config_action_1.ConfigAction.Refresh: await this.config.updateStringConfig(); newMessage = 'Config has been updated'; break; case config_action_1.ConfigAction.Delete: if (words.length < 4) { newMessage = 'You must specify a config name and value to be deleted'; break; } if (!this.config.getStringConfig(words[2]) .map(x => x.toUpperCase()) .includes(words[3].toUpperCase())) { newMessage = `Value "${words[3]}" does not exist in string config under name "${words[2]}"`; break; } await this.config.deleteStringConfig(words[2].toLowerCase(), words[3]); newMessage = 'Config has been deleted'; break; default: newMessage = 'Unknown config command'; break; } return { markdown: newMessage }; } getConfigMessage() { const stringConfig = this.config.getAllStringConfig(); let columnWidths = tableHelper_2.default.getColumnWidths(stringConfig, [(x) => x.name, (x) => x.value], ['Name', 'Value']); let message = 'Here is the current config:\n```\n'; message += tableHelper_1.default.padString('Name', columnWidths[0]) + ' | Value\n'; message += ''.padEnd(columnWidths[0], '-') + '-+-' + ''.padEnd(columnWidths[1], '-') + '\n'; stringConfig.forEach((config) => { message += config.name.padEnd(columnWidths[0]) + ' | ' + config.value + '\n'; }); message += '```'; return message; } } exports.default = StringConfig;