UNPKG

pockybot

Version:

Spark bot that handles team recognition

93 lines (92 loc) 2.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const logger_1 = require("./logger"); class Config { constructor(database) { this.database = database; this.roles = []; this.config = []; this.stringConfig = []; } getRoles(userid) { if (this.roles.length === 0) { return []; } let userRoles = this.roles.filter(x => x.userid === userid); return userRoles.map(x => x.role); } getConfig(config) { if (this.config.length === 0) { return null; } let configValue = this.config.find(x => x.name.toUpperCase() === config.toUpperCase()); if (configValue != null) { return configValue.value; } return null; } getStringConfig(config) { return this.stringConfig.filter(x => x.name.toUpperCase() === config.toUpperCase()) .map(function (x) { return x.value; }); } checkRole(userid, role) { if (!this.roles) return false; return this.roles.some(x => x.userid === userid && x.role === role); } getAllRoles() { return this.roles; } getAllConfig() { return this.config; } getAllStringConfig() { return this.stringConfig; } async updateAll() { await Promise.all([this.updateRoles(), this.updateConfig(), this.updateStringConfig()]); } async updateRoles() { let data = await this.database.getRoles(); this.roles = data; logger_1.Logger.debug(`[Config.updateRoles] Roles: ${JSON.stringify(this.roles)}`); } async updateConfig() { let data = await this.database.getConfig(); this.config = data; logger_1.Logger.debug(`[Config.updateConfig] Config: ${JSON.stringify(this.config)}`); } async updateStringConfig() { let data = await this.database.getStringConfig(); this.stringConfig = data; logger_1.Logger.debug(`[Config.updateStringConfig] ${JSON.stringify(this.stringConfig)}`); } async setRole(userid, role) { await this.database.setRoles(userid, role); await this.updateRoles(); } async setConfig(config, value) { await this.database.setConfig(config, value); await this.updateConfig(); } async setStringConfig(config, value) { await this.database.setStringConfig(config, value); await this.updateStringConfig(); } async deleteRole(userid, role) { await this.database.deleteRole(userid, role); await this.updateRoles(); } async deleteConfig(config) { await this.database.deleteConfig(config); await this.updateConfig(); } async deleteStringConfig(config, value) { await this.database.deleteStringConfig(config, value); await this.updateStringConfig(); } } exports.default = Config;