diff --git a/src/features/fix/embedFixer.ts b/src/features/fix/embedFixer.ts index 7c43aee..e72b11f 100644 --- a/src/features/fix/embedFixer.ts +++ b/src/features/fix/embedFixer.ts @@ -18,7 +18,9 @@ export class EmbedFixerEventHandler { enum MediaProvider { Facebook = "facebook", Instgram = "instagram", + Threads = "threads", Tiktok = "tiktok", + Reddit = "reddit", } class MediaEmbedReplaceFix { @@ -77,6 +79,17 @@ const MEDIA_EMBED_FIXERS: MediaEmbedFixer[] = [ ], [new MediaEmbedReplaceFix("instagram.com", "kkinstagram.com")], ), + new MediaEmbedFixer( + MediaProvider.Threads, + [ + /https:\/\/(www\.)?threads\.(net|com)\/@[\w.]+\/?/, + /https:\/\/(www\.)?threads\.(net|com)\/@[\w.]+\/post\/[\w]+\/?/, + ], + [ + new MediaEmbedReplaceFix("threads.net", "fixthreads.net"), + new MediaEmbedReplaceFix("threads.com", "fixthreads.net"), + ], + ), new MediaEmbedFixer( MediaProvider.Tiktok, [ @@ -86,45 +99,57 @@ const MEDIA_EMBED_FIXERS: MediaEmbedFixer[] = [ ], [new MediaEmbedReplaceFix("tiktok.com", "a.tnktok.com")], ), + new MediaEmbedFixer( + MediaProvider.Reddit, + [ + /https:\/\/(www\.|old\.)?reddit\.com\/r\/[\w]+\/comments\/[\w]+\/[\w]+\/?/, + /https:\/\/(www\.|old\.)?reddit\.com\/r\/[\w]+\/s\/[\w]+\/?/, + /https:\/\/(www\.|old\.)?reddit\.com\/user\/[\w]+\/comments\/[\w]+\/[\w]+\/?/, + ], + [new MediaEmbedReplaceFix("reddit.com", "rxddit.com")], + ), ]; function findEmbedFixer(url: string): MediaEmbedFixer | null { - for (const fixer of MEDIA_EMBED_FIXERS) { - if (fixer.matchUrl(url)) { - return fixer; - } - } - - return null; + return MEDIA_EMBED_FIXERS.find((fixer) => fixer.matchUrl(url)) ?? null; } function applyFix(message: Message): void { - let content = message.content; - let urls = extractUrls(content); + const urls = extractUrls(message.content); + + // No need to proceed if no URLs found + if (urls.length === 0) return; - for (let _url of urls) { - let url = _url[0]; + const fixed_urls: string[] = []; + + for (const _url of urls) { + const url = _url[0]; let cleanUrl: string; try { cleanUrl = removeQueryParams(url).replace("www.", ""); } catch { + console.warn(`Failed to parse URL during apply embed fix: ${url}`); continue; } const fixer = findEmbedFixer(cleanUrl); - if (fixer === null) continue; + if (!fixer) continue; for (const fix of fixer.fixes) { - if (!checkUrlDomain(cleanUrl, fix.originalDomain)) continue; - let new_url = replaceDomain(cleanUrl, fix.replacementDomain); - - if ( - message.channel && - message.channel.isTextBased() && - "send" in message.channel - ) { - message.channel.send(`[Fixed Embed URL](${new_url})`); + if (checkUrlDomain(cleanUrl, fix.originalDomain)) { + const new_url = replaceDomain(cleanUrl, fix.replacementDomain); + fixed_urls.push(`[Preview Embed URL](${new_url})`); } } } + + // Only proceed if we have fixed URLs and valid channel + if (fixed_urls.length > 0 && message.channel && "send" in message.channel) { + message.suppressEmbeds(true); + + message.reply({ + content: `${fixed_urls.join("\n")}`, + allowedMentions: { repliedUser: false }, + }); + } }