UNPKG

pockybot

Version:

Spark bot that handles team recognition

91 lines (90 loc) 4.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const trigger_1 = require("../../models/trigger"); const database_1 = require("../../models/database"); const command_1 = require("../../models/command"); const constants_1 = require("../../constants"); const xmlMessageParser_1 = require("../parsers/xmlMessageParser"); const location_action_1 = require("../../models/location-action"); const logger_1 = require("../logger"); class LocationConfig extends trigger_1.default { constructor(dbLocation, config) { super(); this.dbLocation = dbLocation; this.config = config; } isToTriggerOn(message) { let parsedMessage = xmlMessageParser_1.default.parseNonPegMessage(message); return parsedMessage.botId === constants_1.default.botId && parsedMessage.command.toLowerCase().startsWith(command_1.Command.LocationConfig); } async createMessage(message) { let parsedMessage = xmlMessageParser_1.default.parseNonPegMessage(message); let words = parsedMessage.command.trim().split(' ').filter(x => x !== ''); let response; if (words.length < 2) { return { markdown: `Please specify a command. Possible values are ${Object.values(location_action_1.LocationAction).join(', ')}` }; } let locations; try { locations = await this.dbLocation.getLocations(); } catch (error) { logger_1.Logger.error(`[Location.createMessage] Error getting locations ${error.message}`); return { markdown: 'Error getting locations' }; } switch (words[1].toLowerCase()) { case location_action_1.LocationAction.Get: response = await this.getLocationMessage(locations); break; case location_action_1.LocationAction.Set: response = await this.setLocation(message, words, locations); break; case location_action_1.LocationAction.Delete: response = await this.deleteLocation(message, words, locations); break; default: response = `Unknown command. Possible values are ${Object.values(location_action_1.LocationAction).join(', ')}`; break; } return { markdown: response }; } async getLocationMessage(locations) { if (locations.length === 0) { return 'No locations set'; } let message = 'Here are the current locations:\n'; locations.forEach(item => { message += `* ${item}\n`; }); return message; } async setLocation(message, words, locations) { if (!(this.config.checkRole(message.personId, database_1.Role.Admin) || this.config.checkRole(message.personId, database_1.Role.Config))) { return 'Permission denied. You may only use the \'get\' command'; } if (words.length !== 3) { return 'You must specify a location name to set'; } if (locations.map(x => x.toLowerCase()).includes(words[2].toLowerCase())) { return `Location value "${words[2]}" has already been set`; } await this.dbLocation.setLocation(words[2]); return 'Location has been set'; } async deleteLocation(message, words, locations) { if (!(this.config.checkRole(message.personId, database_1.Role.Admin) || this.config.checkRole(message.personId, database_1.Role.Config))) { return 'Permission denied. You may only use the \'get\' command'; } if (words.length !== 3) { return 'You must specify a location name to delete'; } if (!locations.map(x => x.toLowerCase()).includes(words[2].toLowerCase())) { return `Location value "${words[2]}" does not exist`; } await this.dbLocation.deleteLocation(words[2]); return 'Location has been deleted'; } } exports.default = LocationConfig;