From c69d2f562db626ac1673917da1b63d6b2e6693bc Mon Sep 17 00:00:00 2001 From: Emily J Date: Sat, 17 Oct 2020 23:03:39 +1100 Subject: [PATCH] prefix caching + working database functions! --- bot/events/message.js | 90 +++++++++++++++++++++---------------------- bot/index.js | 5 ++- bot/util/database.js | 2 +- 3 files changed, 49 insertions(+), 48 deletions(-) diff --git a/bot/events/message.js b/bot/events/message.js index 8ce3413..e50954e 100644 --- a/bot/events/message.js +++ b/bot/events/message.js @@ -10,15 +10,33 @@ module.exports = class { async run (message) { if (message.author.bot) return; - const data = {}; - data.user = await this.client.db.getUser(message.author.id); + let userPrefix; + let guildPrefix; - const prefixes = [data.user.prefix]; + const prefixes = []; + const data = {}; + + data.user = await this.client.db.getUser(message.author.id); + + // Prefix cache + if (this.client.prefixCache.has(message.author.id)) { + userPrefix = this.client.prefixCache.get(message.author.id); + prefixes.push(userPrefix); + } else { + userPrefix = data.user.prefix; + prefixes.push(userPrefix); + } if (message.guild) { data.guild = await this.client.db.getGuild(message.guild.id); // data.member = await this.client.db.getMember(message.guild.id, message.author.id); - prefixes.push(data.guild.prefix); + if (this.client.prefixCache.has(message.guild.id)) { + guildPrefix = this.client.prefixCache.get(message.guild.id); + prefixes.push(guildPrefix); + } else { + guildPrefix = data.guild.prefix; + prefixes.push(guildPrefix); + } } prefixes.push(`<@${this.client.user.id}> `, `<@!${this.client.user.id}> `); @@ -31,14 +49,22 @@ module.exports = class { if (message.content.indexOf(prefix) !== 0) return; + // Prefix cache + if (userPrefix === prefix && !this.client.prefixCache.has(message.author.id)) { + this.client.prefixCache.set(message.author.id, prefix); + } + + if (guildPrefix === prefix && !this.client.prefixCache.has(message.guild.id)) { + this.client.prefixCache.set(message.guild.id, prefix); + } + if (prefix === `<@${this.client.user.id}> ` || prefix === `<@!${this.client.user.id}> `) { message.prefix = '@Woomy '; } else { message.prefix = prefix; } - // Naughty users can't run commands! - /* TO-DO: CREATE BLACKLIST */ + // TO-DO: CREATE BLACKLIST const args = message.content.slice(prefix.length).trim().split(/ +/g); const command = args.shift().toLowerCase(); @@ -56,19 +82,7 @@ module.exports = class { } catch (err) {} //eslint-disable-line no-empty } - /* NEED A WAY TO STORE ARRAYS IN A HASH - if (data.guild.disabledCommands.includes(cmd.help.name)) { - if (data.guild.systemNotice.enabled === true) { - return message.channel.send('This command has been disabled in this server.'); - }; - }; - - if (data.guild.disabledCategories.includes(cmd.help.category)) { - if (data.guild.systemNotice.enabled === true) { - return message.channel.send('The category this command is apart of has been disabled in this server.'); - }; - }; - */ + // TO-DO: DISABLED COMMANDS / CATEGORIES } if (cmd && cmd.conf.enabled === false) { @@ -83,31 +97,17 @@ module.exports = class { return message.channel.send('This command is unavailable via private message. Please run this command in a guild.'); } - // Permission handler, for both Woomy and the user + // Permission handler if (message.guild) { - // User - let missingUserPerms = new Array(); - - cmd.conf.userPerms.forEach((p) => { - if (!message.channel.permissionsFor(message.member).has(p)) missingUserPerms.push(p); - }); - - if (missingUserPerms.length > 0) { - missingUserPerms = '`' + (missingUserPerms.join('`, `')) + '`'; - return message.channel.send(`You don't have sufficient permissions to run this command! Missing: ${missingUserPerms}`); - } - - // Bot - let missingBotPerms = []; - - cmd.conf.botPerms.forEach((p) => { - if (!message.channel.permissionsFor(message.guild.member(this.client.user)).has(p)) missingBotPerms.push(p); - }); - - if (missingBotPerms.length > 0) { - missingBotPerms = '`' + (missingBotPerms.join('`, `')) + '`'; - return message.channel.send(`I can't run this command because I'm missing these permissions: ${missingBotPerms}`); - } + const missingUserPerms = this.client.functions.checkPermissions(cmd, message, message.member); + if (missingUserPerms) return message.channel.send(` + You don't have sufficient permissions to run this command! Missing: ${missingUserPerms} + `); + + const missingBotPerms = this.client.functions.checkPermissions(cmd, message, message.guild.member(this.client.user)); + if (missingBotPerms) return message.channel.send(` + I can't run this command because I'm missing these permissions: ${missingBotPerms} + `); } // Cooldown @@ -130,9 +130,7 @@ module.exports = class { message.flags.push(args.shift().slice(1)); } - cmd.run(message, args); + cmd.run(message, args, data); this.client.logger.cmd(`Command ran: ${message.content}`); - - // TODO: Command caching if it's worth it } }; diff --git a/bot/index.js b/bot/index.js index db6edc2..df5252c 100644 --- a/bot/index.js +++ b/bot/index.js @@ -35,6 +35,9 @@ class WoomyClient extends Client { this.aliases = new Collection(); this.cooldowns = new Collection(); + // Caches + this.prefixCache = new Collection(); + // Handlers, to load commands and events this.commandHandler = new CommandHandler(this); this.eventHandler = new EventHandler(this); @@ -42,7 +45,7 @@ class WoomyClient extends Client { } async function init () { - const client = new WoomyClient(); + const client = new WoomyClient({ ws: {}}); client.logger.info(`Initializing Woomy v${client.version}`); diff --git a/bot/util/database.js b/bot/util/database.js index a57e63f..5685621 100644 --- a/bot/util/database.js +++ b/bot/util/database.js @@ -27,7 +27,7 @@ class Database { } async getUser (id) { - const res = await this.pool.query('SELECT * FROM guilds WHERE user_id = $1;', [id]); + const res = await this.pool.query('SELECT * FROM users WHERE user_id = $1;', [id]); return res.rows[0]; }