From 8db0c0d368e6691a35c43242fe57f4dc1a21b6e7 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Thu, 28 Jul 2022 10:46:43 +0300 Subject: [PATCH] misc fixes --- src/middleware/ApiClientUtil.js | 12 +++++++----- src/middleware/Logger.js | 2 +- src/middleware/rest/SlashCommandManager.js | 7 ++++++- src/structure/client/RateLimiter.js | 6 +++--- src/structure/client/wrappers/InteractionWrapper.js | 2 +- 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/middleware/ApiClientUtil.js b/src/middleware/ApiClientUtil.js index f55c381..23d2359 100644 --- a/src/middleware/ApiClientUtil.js +++ b/src/middleware/ApiClientUtil.js @@ -94,15 +94,17 @@ class ApiClientUtil { const { guildId } = message; const evalFunc = (client, { guildId }) => { - const guild = client.guilds.cache.get(guildId); - if (!guild) return null; - const wrapper = new client.wrapperClasses.GuildWrapper(client, guild); - return wrapper.toJSON(); + try { + const wrapper = client.getGuildWrapper(guildId); + return wrapper.toJSON(); + } catch { + return null; + } }; this.client.logger.debug(`guild-live request - shard: ${message.shard}, message id ${message.id}`); const result = await this.client.shardingManager.broadcastEval(evalFunc, { context: { guildId } }); - const guild = result.find((elem) => elem !== undefined); + const guild = result.find((elem) => elem !== null); return guild; } diff --git a/src/middleware/Logger.js b/src/middleware/Logger.js index 73e756a..7e61a15 100644 --- a/src/middleware/Logger.js +++ b/src/middleware/Logger.js @@ -70,7 +70,7 @@ class Logger { const maximumCharacters = Math.max(...Constants.Types.map((t) => t.length)); const spacers = maximumCharacters - type.length; - const text = `${chalk[color](type)}${" ".repeat(spacers)} ${header} : ${string}`; + const text = `${chalk[color](type)}${" ".repeat(spacers)} ${header}: ${string}`; const strippedText = text.replace(stripRegex, ''); console.log(text); //eslint-disable-line no-console diff --git a/src/middleware/rest/SlashCommandManager.js b/src/middleware/rest/SlashCommandManager.js index 400f75f..7955bcb 100644 --- a/src/middleware/rest/SlashCommandManager.js +++ b/src/middleware/rest/SlashCommandManager.js @@ -3,6 +3,7 @@ const { Routes } = require('discord-api-types/v9'); const fs = require('fs'); const path = require('path'); const hash = require('object-hash'); +const { inspect } = require('util'); class SlashCommandManager { @@ -43,7 +44,7 @@ class SlashCommandManager { this.client.logger.write('info', `Commands hash: ${cmdHash}, ${guilds.length} out of date`); if (!guilds.length) return; const promises = []; - //console.log(JSON.stringify(commands)); + //fs.writeFileSync(path.join(process.cwd(), 'commands.json'), JSON.stringify(commands)); for(const guild of guilds) { promises.push(this.rest.put( Routes.applicationGuildCommands(clientId, guild), @@ -72,6 +73,10 @@ class SlashCommandManager { str += `${command.name}: `; const options = Object.keys(invalid[key].options); for (const optKey of options) { + if (!command.options[optKey]) { + this.client.logger.warn(`Missing properties for ${command.name}: ${optKey}\nOptions: ${inspect(command.options)}`); + continue; + } str += `${command.options[optKey].name}\t`; } str += `\n\n`; diff --git a/src/structure/client/RateLimiter.js b/src/structure/client/RateLimiter.js index 9035b3c..5d839d4 100644 --- a/src/structure/client/RateLimiter.js +++ b/src/structure/client/RateLimiter.js @@ -37,7 +37,7 @@ class RateLimiter { if (!channel || !(channel instanceof TextChannel)) reject(new Error('Missing channel')); if (!message || !(message instanceof Message)) reject(new Error('Missing message')); - if (!channel.permissionsFor(channel.guild.members.me).has('ManageMessages')) reject(new Error('Missing permission ManageMessages')); + if (!channel.permissionsFor(this.client.user).has('ManageMessages')) reject(new Error('Missing permission ManageMessages')); if (!this.deleteQueue[channel.id]) this.deleteQueue[channel.id] = []; this.deleteQueue[channel.id].push({ message, resolve, reject }); @@ -106,7 +106,7 @@ class RateLimiter { if (!channel || !(channel instanceof TextChannel)) reject(new Error('Missing channel.')); if (!message || !message.length) reject(new Error('Missing message.')); - if (!channel.permissionsFor(channel.guild.members.me).has('SendMessages')) reject(new Error('Missing permission SendMessages')); + if (!channel.permissionsFor(this.client.user).has('SendMessages')) reject(new Error('Missing permission SendMessages')); //Initiate queue if (!this.sendQueue[channel.id]) this.sendQueue[channel.id] = []; @@ -173,7 +173,7 @@ class RateLimiter { return new Promise((resolve, reject) => { if (!channel || !(channel instanceof TextChannel)) reject(new Error('Missing channel')); - if (!channel.permissionsFor(channel.guild.members.me).has('SendMessages')) reject(new Error('Missing permission SendMessages')); + if (!channel.permissionsFor(this.client.user).has('SendMessages')) reject(new Error('Missing permission SendMessages')); if (!message) reject(new Error('Missing message')); if (limit === null) limit = 15; diff --git a/src/structure/client/wrappers/InteractionWrapper.js b/src/structure/client/wrappers/InteractionWrapper.js index 5b0d859..b2c60c7 100644 --- a/src/structure/client/wrappers/InteractionWrapper.js +++ b/src/structure/client/wrappers/InteractionWrapper.js @@ -217,7 +217,7 @@ class InteractionWrapper { } isContextMenu() { - return this.interaction.isContextMenu(); + return this.interaction.isContextMenuCommand(); } isSelectMenu() {