From da85e3253cf35e1a09d6abd9cc8bda495dfaebdd Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Mon, 8 Jan 2024 18:40:06 +0200 Subject: [PATCH] misc fixes log parser and inhibitor time await unawaited calls --- src/client/DiscordClient.ts | 6 +++--- src/client/components/ModerationManager.ts | 2 +- src/client/components/inhibitors/ClientPermissions.ts | 4 +++- src/client/components/observers/CommandHandler.ts | 8 +++++++- .../components/settings/moderation/LinkFilter.ts | 2 +- .../components/settings/moderation/WordFilter.ts | 2 +- src/client/interfaces/FilterSetting.ts | 10 +++++----- 7 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/client/DiscordClient.ts b/src/client/DiscordClient.ts index fdbec24..82e4ffd 100644 --- a/src/client/DiscordClient.ts +++ b/src/client/DiscordClient.ts @@ -494,11 +494,11 @@ class DiscordClient extends Client { if (this.#guildWrappers.has(id)) return this.#guildWrappers.get(id)!; - let guild = this.guilds.cache.get(id); + let guild = this.guilds.cache.get(id) ?? null; if (!guild) - guild = await this.guilds.fetch({ guild: id, cache: true }); + guild = await this.guilds.fetch({ guild: id, cache: true }).catch(() => null); if (!guild) - throw new Error('Guild does not exist'); + return Promise.reject(new Error('Guild does not exist')); const wrapper = new GuildWrapper(this, guild); this.#guildWrappers.set(id, wrapper); diff --git a/src/client/components/ModerationManager.ts b/src/client/components/ModerationManager.ts index 79c23e9..4a63bc8 100644 --- a/src/client/components/ModerationManager.ts +++ b/src/client/components/ModerationManager.ts @@ -539,7 +539,7 @@ class ModerationManager implements Initialisable const guild = await this.#client.getGuildWrapper(i.guild!); if (!guild) - throw new Error('Missing guild'); + throw new Error(`Missing guild for infraction: ${i.id}`); // await guild.settings(); //just incase let target = null; diff --git a/src/client/components/inhibitors/ClientPermissions.ts b/src/client/components/inhibitors/ClientPermissions.ts index 4480fe7..8b9f47f 100644 --- a/src/client/components/inhibitors/ClientPermissions.ts +++ b/src/client/components/inhibitors/ClientPermissions.ts @@ -17,7 +17,9 @@ class ClientPermissions extends Inhibitor async execute (invoker: InvokerWrapper, command: Command) { - const channel = invoker.channel as GuildTextBasedChannel; + const channel = invoker.channel as GuildTextBasedChannel | null; + if (!channel) + return super._fail(); const missing = channel.permissionsFor(this.client.user!)?.missing(command.clientPermissions); if (!missing || missing.length) return super._fail({ error: true, missing: missing?.join(', ') || 'all', silent: true }); diff --git a/src/client/components/observers/CommandHandler.ts b/src/client/components/observers/CommandHandler.ts index 4764eee..62595a7 100644 --- a/src/client/components/observers/CommandHandler.ts +++ b/src/client/components/observers/CommandHandler.ts @@ -78,6 +78,7 @@ class CommandHandler extends Observer }); } + const start = Date.now(); const messageWrapper = new MessageWrapper(this.client, message); const { command, parameters } = await this._getCommand(messageWrapper); if (!command) @@ -100,6 +101,8 @@ class CommandHandler extends Observer if (await this._parseResult(invoker, result)) return; + const end = Date.now(); + this.logger.info(`Message parse took ${end - start}ms`); await this._executeCommand(invoker, result.options ?? {}); } @@ -222,6 +225,7 @@ class CommandHandler extends Observer return []; const promises = []; + const start = Date.now(); for (const inhibitor of inhibitors.values()) { if (inhibitor.guild && !invoker.inGuild()) @@ -230,6 +234,8 @@ class CommandHandler extends Observer } const errors = (await Promise.all(promises)).filter((result) => result.error); + const end = Date.now(); + this.logger.info(`Inhibitors took ${end - start}ms to complete`); if (!errors.length) return []; @@ -271,7 +277,7 @@ class CommandHandler extends Observer { if (!(invoker.command instanceof SettingsCommand)) invoker.command.error(now); - this.logger.error(`\n[${invoker.type.toUpperCase()}] Command ${debugstr} errored:\nGuild: ${invoker.guild?.name || 'dms'} (${invoker.guild?.id || ''})\nOptions:\n${Object.keys(options).map((key) => `[${key}: ${options[key].asString} (${options[key].rawValue})]`).join('\n')}\n${error.stack || error}`); + this.logger.error(`\n[${invoker.type.toUpperCase()}] Command ${debugstr} errored:\nGuild: ${invoker.inGuild() ? invoker.guild?.name : 'dms'} (${invoker.guild?.id || ''})\nOptions:\n${Object.keys(options).map((key) => `[${key}: ${options[key].asString} (${options[key].rawValue})]`).join('\n')}\n${error.stack || error}`); this._generateError(invoker, { type: 'commandHandler' }); } return; diff --git a/src/client/components/settings/moderation/LinkFilter.ts b/src/client/components/settings/moderation/LinkFilter.ts index 4cb97bb..581a978 100644 --- a/src/client/components/settings/moderation/LinkFilter.ts +++ b/src/client/components/settings/moderation/LinkFilter.ts @@ -257,7 +257,7 @@ class LinkFilterSetting extends FilterSetting } actionObject.trigger = words; if (removed.length) - invoker.channel!.send(invoker.format( + await invoker.channel!.send(invoker.format( 'SETTING_LINKFILTER_WORD_IN_ACTION', { words: removed.join('`, `') } )); diff --git a/src/client/components/settings/moderation/WordFilter.ts b/src/client/components/settings/moderation/WordFilter.ts index ac2a4e1..7524b42 100644 --- a/src/client/components/settings/moderation/WordFilter.ts +++ b/src/client/components/settings/moderation/WordFilter.ts @@ -271,7 +271,7 @@ class WordFilterSetting extends FilterSetting } actionObject.trigger = words; if (removed.length) - invoker.channel!.send(invoker.format( + await invoker.channel!.send(invoker.format( 'SETTING_WORDFILTER_WORD_IN_ACTION', { words: removed.join('`, `') } )); diff --git a/src/client/interfaces/FilterSetting.ts b/src/client/interfaces/FilterSetting.ts index 880c06b..e34dc4c 100644 --- a/src/client/interfaces/FilterSetting.ts +++ b/src/client/interfaces/FilterSetting.ts @@ -135,7 +135,7 @@ abstract class FilterSetting extends Setting if (typeof time === 'number') actionObject.duration = time; else - invoker.channel!.send(invoker.format('SETTING_FILTER_ACTION_ADD_TIMER_FAIL')); + await invoker.channel!.send(invoker.format('SETTING_FILTER_ACTION_ADD_TIMER_FAIL')); } } @@ -161,14 +161,14 @@ abstract class FilterSetting extends Setting if (value < 0 || value > 100) { value = value < 0 ? 0 : 100; - invoker.channel!.send(invoker.format('SETTING_FILTER_ACTION_ADD_POINTS_RANGE', { value })); + await invoker.channel!.send(invoker.format('SETTING_FILTER_ACTION_ADD_POINTS_RANGE', { value })); } actionObject.points = value; } else { actionObject.points = null; - invoker.channel!.send(invoker.format('SETTING_FILTER_ACTION_ADD_POINTS_FAIL')); + await invoker.channel!.send(invoker.format('SETTING_FILTER_ACTION_ADD_POINTS_FAIL')); } @@ -190,7 +190,7 @@ abstract class FilterSetting extends Setting if (typeof time === 'number') actionObject.expiration = time; else - invoker.channel!.send(invoker.format('SETTING_FILTER_ACTION_ADD_EXPIRATION_FAIL')); + await invoker.channel!.send(invoker.format('SETTING_FILTER_ACTION_ADD_EXPIRATION_FAIL')); } } } @@ -407,7 +407,7 @@ abstract class FilterSetting extends Setting points = 0; else if (points > 100) points = 100; - invoker.channel?.send(invoker.format('SETTING_FILTER_ACTION_ADD_POINTS_RANGE', { value: points })); + await invoker.channel?.send(invoker.format('SETTING_FILTER_ACTION_ADD_POINTS_RANGE', { value: points })); } action.points = points;