From c61d1aca64e1e7cece11248ba5097a6032a10adf Mon Sep 17 00:00:00 2001 From: Thomas Mello Date: Wed, 19 Jun 2024 21:11:50 +0300 Subject: [PATCH] feat: allow breaking on hook result --- src/hooks/imgur.ts | 6 ++++-- src/listeners/messageCreate.ts | 2 +- src/types.ts | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/hooks/imgur.ts b/src/hooks/imgur.ts index cab2f7f..f80cf6b 100644 --- a/src/hooks/imgur.ts +++ b/src/hooks/imgur.ts @@ -5,12 +5,12 @@ class ImgurHook implements MessageHook { private readonly imgurVideoRegex = /https:\/\/(i\.)?imgur\.com\S+\.mp4\/?/gi; public readonly name = 'Imgur link fixer'; - public async run(message: Message): Promise { + public async run(message: Message): Promise { const originalUrls = this.findImgurVideoUrls(message.content); const fixedUrls = originalUrls.map(ImgurHook.replacer); - if (fixedUrls.length === 0) return; + if (fixedUrls.length === 0) return false; let content = message.content; for (let i = 0; i < originalUrls.length; i++) { @@ -21,6 +21,8 @@ class ImgurHook implements MessageHook { await message.channel.send( `Imgur links were fixed. Original message by ${message.author.displayName}:\n${content}`, ); + + return true; } private findImgurVideoUrls(content: string): URL[] { diff --git a/src/listeners/messageCreate.ts b/src/listeners/messageCreate.ts index b561eb3..c60f19d 100644 --- a/src/listeners/messageCreate.ts +++ b/src/listeners/messageCreate.ts @@ -88,7 +88,7 @@ export class MessageCreateEvent extends Listener { if (config && Array.isArray(config.guilds) && !config.guilds.includes(message.guildId)) continue; - await hook.run(message); + if (await hook.run(message)) break; } catch (e) { this.container.logger.error(e, `Failed to run hook ${hook.name} in ${message.guild!.name}`); void alert(`Failed to run hook ${hook.name}`); diff --git a/src/types.ts b/src/types.ts index e90250b..8cd779e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -188,7 +188,7 @@ export const isPreconditionErrorContext = ( export interface MessageHook { name: string; - run(message: Message): Promise; + run(message: Message): Promise; } declare module '@sapphire/pieces' {