UNPKG

pockybot

Version:

Spark bot that handles team recognition

96 lines (95 loc) 4.23 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 command_1 = require("../../models/command"); const xmlMessageParser_1 = require("../parsers/xmlMessageParser"); class NumberConfig 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.NumberConfig); } async createMessage(message) { let parsedMessage = xmlMessageParser_1.default.parseNonPegMessage(message); let words = parsedMessage.command.trim().split(' ').filter(x => x !== ''); if (words.length < 2) { return { markdown: `Please specify a command. Possible values are ${Object.values(config_action_1.ConfigAction).join(', ')}` }; } let newMessage; switch (words[1]) { case config_action_1.ConfigAction.Get: newMessage = this.getConfigMessage(); break; case config_action_1.ConfigAction.Set: newMessage = NumberConfig.validateConfigActionSet(words); if (newMessage) break; const value = Number(words[3]); if (words[2] === 'minimum' && value > this.config.getConfig('limit')) { newMessage = 'Minimum pegs must be less than or equal to peg limit.'; break; } await this.config.setConfig(words[2], value); newMessage = 'Config has been set'; break; case config_action_1.ConfigAction.Refresh: await this.config.updateConfig(); newMessage = 'Config has been updated'; break; case config_action_1.ConfigAction.Delete: if (words.length < 3) { newMessage = 'You must specify a config to be deleted'; break; } if (this.config.getConfig(words[2]) == null) { newMessage = `Config value "${words[2]}" does not exist`; break; } await this.config.deleteConfig(words[2]); newMessage = 'Config has been deleted'; break; default: newMessage = 'Unknown config command'; break; } return { markdown: newMessage }; } getConfigMessage() { const numberConfig = this.config.getAllConfig(); let columnWidths = tableHelper_1.default.getColumnWidths(numberConfig, [(x) => x.name, (x) => x.value.toString()], ['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'; numberConfig.forEach((config) => { message += config.name.padEnd(columnWidths[0]) + ' | ' + config.value + '\n'; }); message += '```'; return message; } static validateConfigActionSet(words) { if (words.length < 4) { return 'You must specify a config name and value to set'; } const value = Number(words[3]); if (isNaN(value)) { return 'Config must be set to a number'; } if (value < 0) { return 'Config should be greater than or equal to 0.'; } return null; } } exports.default = NumberConfig;