Skip to content

Commit 7fa43d3

Browse files
committed
Add embed helper
1 parent bf01d1a commit 7fa43d3

3 files changed

Lines changed: 28 additions & 30 deletions

File tree

src/commands/instagram.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Message } from "discord.js";
33
import type { SpecialCommand } from "#/commands/command.ts";
44
import type { BotContext } from "#/context.ts";
55
import * as instagramService from "#/service/instagram.ts";
6+
import { replyWithFallbackEmbed } from "#/utils/embedUtils.ts";
67

78
const instagramOptions = {
89
uriPattern:
@@ -14,24 +15,6 @@ const instagramOptions = {
1415
},
1516
} as const;
1617

17-
const FALLBACK_EMBED_WAIT_MS = 5000;
18-
19-
async function replyWithFallback(message: Message<true>, fallbackUrl: string) {
20-
const reply = await message.reply({
21-
content: fallbackUrl,
22-
allowedMentions: { repliedUser: false },
23-
});
24-
25-
await new Promise(resolve => setTimeout(resolve, FALLBACK_EMBED_WAIT_MS));
26-
const fetched = await reply.fetch();
27-
28-
if (fetched.embeds.length > 0) {
29-
await message.suppressEmbeds(true);
30-
} else {
31-
await fetched.delete();
32-
}
33-
}
34-
3518
export default class InstagramLink implements SpecialCommand {
3619
name = "Instagram";
3720
description = "Embedded Instagram Links";
@@ -87,7 +70,7 @@ export default class InstagramLink implements SpecialCommand {
8770
try {
8871
const result = await instagramService.downloadInstagramContent(context, postUri);
8972
if (!result.success) {
90-
await replyWithFallback(message, fallbackUrl);
73+
await replyWithFallbackEmbed(message, fallbackUrl);
9174
return;
9275
}
9376

@@ -106,7 +89,7 @@ export default class InstagramLink implements SpecialCommand {
10689
allowedMentions: { repliedUser: false },
10790
});
10891
} catch {
109-
await replyWithFallback(message, fallbackUrl);
92+
await replyWithFallbackEmbed(message, fallbackUrl);
11093
}
11194
}
11295
await message.suppressEmbeds(true);

src/commands/tiktok.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Message } from "discord.js";
22

33
import type { SpecialCommand } from "#/commands/command.ts";
4+
import { replyWithFallbackEmbed } from "#/utils/embedUtils.ts";
45

56
const tiktokUrlPattern = /https?:\/\/(?:(?:www|m|vm)\.)?tiktok\.com\/([^?&\s/]+)/i;
67

@@ -24,15 +25,6 @@ export default class TikTokLink implements SpecialCommand {
2425
return;
2526
}
2627

27-
const reply = await message.reply({
28-
content: `https://kktiktok.com/${match[1]}`,
29-
allowedMentions: { repliedUser: false },
30-
});
31-
32-
if (reply.embeds.length > 0) {
33-
await message.suppressEmbeds(true);
34-
} else {
35-
await reply.delete();
36-
}
28+
await replyWithFallbackEmbed(message, `https://kktiktok.com/${match[1]}`);
3729
}
3830
}

src/utils/embedUtils.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { Message } from "discord.js";
2+
3+
const EMBED_WAIT_MS = 5000;
4+
5+
/**
6+
* Sends a fallback embed URL as a reply, waits for Discord to resolve the embed,
7+
* then suppresses the original message's embeds if the reply has one, or deletes the reply otherwise.
8+
*/
9+
export async function replyWithFallbackEmbed(message: Message<true>, fallbackUrl: string) {
10+
const reply = await message.reply({
11+
content: fallbackUrl,
12+
allowedMentions: { repliedUser: false },
13+
});
14+
15+
await new Promise(resolve => setTimeout(resolve, EMBED_WAIT_MS));
16+
const fetched = await reply.fetch();
17+
18+
if (fetched.embeds.length > 0) {
19+
await message.suppressEmbeds(true);
20+
} else {
21+
await fetched.delete();
22+
}
23+
}

0 commit comments

Comments
 (0)