From ccb652f737e151299488e31dd425d6eeb64ecc9f Mon Sep 17 00:00:00 2001 From: janderedev Date: Sat, 30 Apr 2022 21:01:37 +0200 Subject: [PATCH] replace tenor URLs --- bridge/src/discord/events.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/bridge/src/discord/events.ts b/bridge/src/discord/events.ts index e46854e..57b7175 100644 --- a/bridge/src/discord/events.ts +++ b/bridge/src/discord/events.ts @@ -14,6 +14,8 @@ const MAX_BRIDGED_FILE_SIZE = 8_000_000; // 8 MB const RE_MENTION_USER = /<@!*[0-9]+>/g; const RE_MENTION_CHANNEL = /<#[0-9]+>/g; const RE_EMOJI = /<(a?)?:\w+:\d{18}?>/g; +const RE_TENOR = /^https:\/\/tenor.com\/view\/[^\s]+$/g; +const RE_TENOR_META = //g client.on('messageDelete', async message => { try { @@ -208,6 +210,33 @@ client.on('messageCreate', async message => { // Replaces @mentions and #channel mentions async function renderMessageBody(message: string): Promise { + + // Replace Tenor URLs so they render properly. + // We have to download the page first, then extract + // the `c.tenor.com` URL from the meta tags. + // Could query autumn but that's too much effort and I already wrote this. + if (RE_TENOR.test(message)) { + try { + logger.debug('Replacing tenor URL'); + + const res = await axios.get( + message, + { + headers: { + 'User-Agent': 'AutoMod/1.0; https://github.com/janderedev/automod', + } + } + ); + + const metaTag = RE_TENOR_META.exec(res.data as string)?.[0]; + if (metaTag) { + return metaTag + .replace('', ''); + } + } catch(e) { logger.warn(`Replacing tenor URL failed: ${e}`) } + } + // @mentions message = await smartReplace(message, RE_MENTION_USER, async (match: string) => { const id = match.replace('<@!', '').replace('<@', '').replace('>', '');