From 8c95848a7054effd1bf387b8d402d90443fd500b Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Sun, 7 Jul 2024 13:15:43 -0600 Subject: [PATCH] utility.presence: remap media proxy urls --- src/modules/utility.js | 74 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/src/modules/utility.js b/src/modules/utility.js index 9e89520..a259fb3 100644 --- a/src/modules/utility.js +++ b/src/modules/utility.js @@ -1907,7 +1907,30 @@ presence.callback = async function (msg, line) { largeUrl = `https://cdn.discordapp.com/app-assets/${activity.application_id}/${activity.assets.large_image}.png`; } - image_links.push(`[Large Image](${largeUrl})`); + let fixedLargeUrl = largeUrl; + if (fixedLargeUrl.includes("media.discordapp.net")) { + if (fixedLargeUrl.includes("/external/")) { + const split = fixedLargeUrl + .replace("https://media.discordapp.net/external/", "") + .split("/"); + split.shift(); + + let query = ""; + if (split[0].startsWith("%")) { + query = decodeURIComponent(split.shift()); + } + split[0] = split[0] + ":/"; + + fixedLargeUrl = split.join("/") + query; + } else { + fixedLargeUrl = fixedLargeUrl.replace( + "media.discordapp.net", + "cdn.discordapp.com" + ); + } + } + + image_links.push(`[Large Image](${fixedLargeUrl})`); let smallUrl; if (activity.assets.small_image != null) { @@ -1925,7 +1948,30 @@ presence.callback = async function (msg, line) { smallUrl = `https://cdn.discordapp.com/app-assets/${activity.application_id}/${activity.assets.small_image}.png`; } - image_links.push(`[Small Image](${smallUrl})`); + let fixedSmallUrl = smallUrl; + if (fixedSmallUrl.includes("media.discordapp.net")) { + if (fixedSmallUrl.includes("/external/")) { + const split = fixedSmallUrl + .replace("https://media.discordapp.net/external/", "") + .split("/"); + split.shift(); + + let query = ""; + if (split[0].startsWith("%")) { + query = decodeURIComponent(split.shift()); + } + split[0] = split[0] + ":/"; + + fixedSmallUrl = split.join("/") + query; + } else { + fixedSmallUrl = fixedSmallUrl.replace( + "media.discordapp.net", + "cdn.discordapp.com" + ); + } + } + + image_links.push(`[Small Image](${fixedSmallUrl})`); } const largeImage = await fetch(largeUrl) @@ -1987,6 +2033,30 @@ presence.callback = async function (msg, line) { embed.thumbnail = { url: `attachment://${index}.png`, }; + + let fixedSmallUrl = smallUrl; + if (fixedSmallUrl.includes("media.discordapp.net")) { + if (fixedSmallUrl.includes("/external/")) { + const split = fixedSmallUrl + .replace("https://media.discordapp.net/external/", "") + .split("/"); + split.shift(); + + let query = ""; + if (split[0].startsWith("%")) { + query = decodeURIComponent(split.shift()); + } + split[0] = split[0] + ":/"; + + fixedSmallUrl = split.join("/") + query; + } else { + fixedSmallUrl = fixedSmallUrl.replace( + "media.discordapp.net", + "cdn.discordapp.com" + ); + } + } + embed.fields.push({ name: "\u200b", value: `[Small Image](${smallUrl})`,