From 280d21ad91dad281ec2269d10db87909c58c8eb2 Mon Sep 17 00:00:00 2001 From: Khaazz Date: Mon, 13 Aug 2018 01:14:24 +0200 Subject: [PATCH] Fixed a crit bug in permissions checkers. Added updateGuildConf method --- src/AxonClient.js | 64 ++++++++++++++++++++++++--------------- src/Structures/Command.js | 34 ++++++++++----------- 2 files changed, 56 insertions(+), 42 deletions(-) diff --git a/src/AxonClient.js b/src/AxonClient.js index b27b5280..4cb9728c 100755 --- a/src/AxonClient.js +++ b/src/AxonClient.js @@ -810,27 +810,6 @@ class AxonClient { return guildConf.modules.find(m => m === command.module.label); } - /** - * Get guildConfig from cache || DB - * Cache it if not cached - * - * @param {String} gID - * @returns {Promise} guildConf object fetched - * @memberof AxonClient - */ - async getGuildConf(gID) { - let guildConf = this.guildConfigs.get(gID); - if (!guildConf) { - try { - guildConf = await this.fetchGuildConf(gID); // retrieve DB get guildConf - } catch (err) { - throw new AxonCommandError('OnMessage', 'DB ERROR - guildConfig', `Guild: ${gID}`, err); - } - this.guildConfigs.set(gID, guildConf); - } - return guildConf; - } - // // ****** DATABASE ****** // initialisation/fetch @@ -945,6 +924,43 @@ class AxonClient { return command; } + /** + * Get guildConfig from cache || DB + * Cache it if not cached + * + * @param {String} gID + * @returns {Promise} guildConf object fetched + * @memberof AxonClient + */ + async getGuildConf(gID) { + let guildConf = this.guildConfigs.get(gID); + if (!guildConf) { + try { + guildConf = await this.fetchGuildConf(gID); // retrieve DB get guildConf + } catch (err) { + throw new AxonCommandError('OnMessage', 'DB ERROR - guildConfig', `Guild: ${gID}`, err); + } + this.guildConfigs.set(gID, guildConf); + } + return guildConf; + } + + /** + * Update guild Conf in cache + DB + * Uses Json Object directly. (Client cache leaned Guild schema) + * + * @param {String} gID - Guild id + * @param {Object} guildSchema - Guild schema Object + * @returns {Promise} Updated guildSchema + * @memberof AxonClient + */ + updateGuildConf(gID, guildSchema) { + return this.DBprovider.saveGuildSchema(gID, guildSchema) + .then(() => { + this.guildConfigs.set(gID, guildSchema); + }); + } + // // ****** EXTERNAL ****** // @@ -1037,7 +1053,7 @@ class AxonClient { async updateStateModule(guildID, label, boolean) { let conf; try { - conf = this.guildConfigs.get(guildID) || this.fetchGuildConf(guildID); // get from cache or from DB if not found + conf = await this.getGuildConf(guildID); // get from cache or from DB if not found } catch (err) { throw err; } @@ -1068,7 +1084,7 @@ class AxonClient { async updateStateCommand(guildID, label, boolean) { let conf; try { - conf = this.guildConfigs.get(guildID) || this.fetchGuildConf(guildID); // get from cache or from DB if not found + conf = await this.getGuildConf(guildID); // get from cache or from DB if not found } catch (err) { throw err; } @@ -1099,7 +1115,7 @@ class AxonClient { async updateStateEvent(guildID, label, boolean) { let conf; try { - conf = this.guildConfigs.get(guildID) || this.fetchGuildConf(guildID); // get from cache or from DB if not found + conf = await this.getGuildConf(guildID); // get from cache or from DB if not found } catch (err) { throw err; } diff --git a/src/Structures/Command.js b/src/Structures/Command.js index 3c6946bb..bd388229 100755 --- a/src/Structures/Command.js +++ b/src/Structures/Command.js @@ -212,35 +212,26 @@ class Command extends Base { _execute(message) { const { msg, args, guildConf } = message; - /** Test for Mod Only / serverMod command | serverAdmin command */ - if (((guildConf.modOnly || this.permissions.serverMod) && !this.AxonUtils.isMod(msg.member, guildConf)) || (this.permissions.serverAdmin && !this.AxonUtils.isAdmin(msg.member))) { - /** Sends invalid perm message in case of invalid perm [option enabled] */ - if (!guildConf.modOnly && this.options.invalidPermissionMessage) { // doesn't send back invalid perm if mod Only - return this.sendUserPerms(msg.channel); - } - return Promise.resolve(); - } - /** Test for bot permissions */ if (!this._checkPermsBot(msg)) { return this.sendBotPerms(msg.channel); } - /** Test for Cooldown - Send Cooldown message */ - const cd = this._shouldCooldown(msg); - if (typeof (cd) === 'number') { - return this.sendCooldown(msg.channel, cd); - } - /** Permissions checkers */ - if (!this.canExecute(msg)) { + if (!this.canExecute(msg, guildConf)) { /** Sends invalid perm message in case of invalid perm [option enabled] */ - if (this.options.invalidPermissionMessage) { + if (!guildConf.modOnly && this.options.invalidPermissionMessage) { return this.sendUserPerms(msg.channel); } return Promise.resolve(); } + /** Test for Cooldown - Send Cooldown message */ + const cd = this._shouldCooldown(msg); + if (typeof (cd) === 'number') { + return this.sendCooldown(msg.channel, cd); + } + if (this.options.deleteCommand) { // delete input msg.delete(); } @@ -373,12 +364,14 @@ class Command extends Base { * Permission checker - Does the user has perm to exec command/not * Bypass - one of the perms (override) => doesn't go through others chercker * Needed - all perms => still go through other checkers + * ServerMod * * @param {Object} msg - Message Object + * @param {Object} guildConf - Guild Config * @returns {Boolean} true: user can execute command * @memberof Command */ - canExecute(msg) { + canExecute(msg, guildConf) { /** Bypass: if one of the perm is true => Exec the command */ if (this._checkPermsUserBypass(msg) || this._checkUserBypass(msg) @@ -388,6 +381,11 @@ class Command extends Base { return true; } + if (((guildConf.modOnly || this.permissions.serverMod) && !this.AxonUtils.isMod(msg.member, guildConf)) + || (this.permissions.serverAdmin && !this.AxonUtils.isAdmin(msg.member))) { + return false; + } + /** Needed: if one of the perms is false => doesn't exec the command */ if (!this._checkPermsUserNeeded(msg) || !this._checkUserNeeded(msg)