UNPKG

pockybot

Version:

Spark bot that handles team recognition

123 lines (122 loc) 5.62 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 xmlMessageParser_1 = require("../parsers/xmlMessageParser"); const tableHelper_2 = require("../parsers/tableHelper"); const command_1 = require("../../models/command"); class RoleConfig extends trigger_1.default { constructor(dbUsers, config) { super(); this.dbUsers = dbUsers; 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.RoleConfig); } async createMessage(message) { const parsedMessage = xmlMessageParser_1.default.parseXmlMessage(message); let response; if (parsedMessage.length < 2) { return { markdown: `Please specify a command. Possible values are ${Object.values(config_action_1.ConfigAction).join(', ')}` }; } let pattern = new RegExp('^' + command_1.Command.RoleConfig, 'ui'); const command = parsedMessage[1].text().toLowerCase().trim().replace(pattern, '').trim(); try { switch (command) { case config_action_1.ConfigAction.Get: response = await this.getConfigMessage(); break; case config_action_1.ConfigAction.Set: { const { userId, username, role } = await this.parseSetDeleteMessage(parsedMessage); if (this.config.getRoles(userId).includes(role)) { response = `Role "${role}" is already set for user "${username}".`; break; } this.config.setRole(userId, role); response = 'Role has been set'; break; } case config_action_1.ConfigAction.Refresh: this.config.updateRoles(); response = 'Roles has been updated'; break; case config_action_1.ConfigAction.Delete: { const { userId, username, role } = await this.parseSetDeleteMessage(parsedMessage); if (!this.config.getRoles(userId).includes(role)) { response = `Role "${role}" is not set for user "${username}"`; break; } this.config.deleteRole(userId, role); response = 'Role has been deleted'; break; } default: response = 'Unknown config command'; break; } } catch (error) { return { markdown: error.message }; } return { markdown: response }; } async parseSetDeleteMessage(parsedMessage) { if (parsedMessage.length < 4) { throw new Error('You must specify a user and a role to set/delete.'); } if (!xmlMessageParser_1.default.isMentionOfPerson(parsedMessage[2])) { throw new Error('Please mention a user you want to set/delete a role for'); } let role = parsedMessage[3].text().toUpperCase().trim(); if (!Object.values(database_1.Role).map(x => x.valueOf()).includes(role)) { throw new Error(`Invalid role ${role}. Valid values are: ${Object.values(database_1.Role).join(', ')}`); } const userId = xmlMessageParser_1.default.getPersonId(parsedMessage[2].attr('data-object-id').value()); const username = parsedMessage[2].text(); try { let exists = await this.dbUsers.existsOrCanBeCreated(userId); if (!exists) { throw new Error(`User ${username} could not be found or created. Exiting.`); } } catch (error) { throw new Error(`Error: User ${username} could not be found or created. Exiting.`); } return { userId: userId, username: username, role: role }; } async getConfigMessage() { const roles = this.config.getAllRoles(); const mappedPromise = roles.map(async (x) => { const user = await this.dbUsers.getUser(x.userid); return { username: user.username, role: x.role }; }); const mapped = await Promise.all(mappedPromise); let columnWidths = tableHelper_2.default.getColumnWidths(mapped, [x => x.username, x => x.role], ['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'; for (const config of mapped) { message += config.username.padEnd(columnWidths[0]) + ' | ' + config.role + '\n'; } message += '```'; return message; } } exports.default = RoleConfig;