1
1
import type { Message } from 'discord.js' ;
2
2
import ogs from 'open-graph-scraper' ;
3
3
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
+
4
25
export const coolLinksManagement = async ( message : Message ) => {
5
26
const urlRegex = / ( ( ( h t t p s ? : \/ \/ ) | ( w w w \. ) ) [ ^ \s ] + ) / g;
6
27
const detectedURLs = message . content . match ( urlRegex ) ;
@@ -12,16 +33,10 @@ export const coolLinksManagement = async (message: Message) => {
12
33
13
34
await message . react ( '✅' ) ;
14
35
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
+ } ) ;
27
42
} ;
0 commit comments