Skip to content

Commit

Permalink
feat: allow breaking on hook result
Browse files Browse the repository at this point in the history
  • Loading branch information
codedealer committed Jun 19, 2024
1 parent 2fc3658 commit c61d1ac
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/hooks/imgur.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
public async run(message: Message): Promise<boolean> {
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++) {
Expand All @@ -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[] {
Expand Down
2 changes: 1 addition & 1 deletion src/listeners/messageCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class MessageCreateEvent extends Listener<typeof Events.MessageCreate> {
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}`);
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export const isPreconditionErrorContext = (

export interface MessageHook {
name: string;
run(message: Message): Promise<void>;
run(message: Message): Promise<boolean>;
}

declare module '@sapphire/pieces' {
Expand Down

0 comments on commit c61d1ac

Please sign in to comment.