Skip to content

Commit 46321ba

Browse files
feat: clean thread name
1 parent 03bc9d2 commit 46321ba

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

src/cool-links-management.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
import type { Message } from 'discord.js';
22
import ogs from 'open-graph-scraper';
33

4+
const getThreadNameFromOpenGraph = async (url: string): Promise<string | null> => {
5+
try {
6+
const { result } = await ogs({ url });
7+
if (!result.success) throw new Error('No OG data found');
8+
9+
const ogSiteName = result.ogSiteName;
10+
const ogTitle = result.ogTitle;
11+
if (ogSiteName && ogTitle) {
12+
return `${ogSiteName} - ${ogTitle}`;
13+
} else if (ogSiteName) {
14+
return ogSiteName;
15+
} else if (ogTitle) {
16+
return ogTitle;
17+
}
18+
} catch (error) {
19+
console.error(error);
20+
}
21+
22+
return null;
23+
};
24+
425
export const coolLinksManagement = async (message: Message) => {
526
const urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
627
const detectedURLs = message.content.match(urlRegex);
@@ -12,16 +33,10 @@ export const coolLinksManagement = async (message: Message) => {
1233

1334
await message.react('✅');
1435
await message.react('❌');
15-
try {
16-
const { result } = await ogs({ url: detectedURLs[0] });
17-
const threadName = result.success
18-
? `${result.ogSiteName} - ${result.ogTitle}`
19-
: message.content;
20-
await message.startThread({
21-
name: threadName,
22-
autoArchiveDuration: 4320,
23-
});
24-
} catch (error) {
25-
console.error(error);
26-
}
36+
37+
const threadName = await getThreadNameFromOpenGraph(detectedURLs[0]);
38+
await message.startThread({
39+
name: threadName ?? message.content,
40+
autoArchiveDuration: 4320,
41+
});
2742
};

0 commit comments

Comments
 (0)