diff --git a/src/modules/foxwells.js b/src/modules/foxwells.js index e15b1cf..df6e653 100644 --- a/src/modules/foxwells.js +++ b/src/modules/foxwells.js @@ -177,20 +177,34 @@ async function findSuitableImage(msg) { if (attachment) { const url = attachment.url; - if (/(jpe?g|png|gif|webp)$/.test(url)) { + const parsed = new URL(url); + if (/(jpe?g|png|gif|webp)$/.test(parsed.pathname)) { out.url = url; - } else if (/(mp4|webm|mov)$/.test(url)) { + } else if (/(mp4|webm|mov)$/.test(parsed.pathname)) { out.video = true; out.attachment = true; out.url = "attachment://thumb.jpg"; - out.file = await fetch(attachment.proxyURL + "?format=jpeg") + + let thumbUrl = attachment.proxyURL; + if (thumbUrl.includes("media.discordapp.net") && thumbUrl.includes("?")) { + if (thumbUrl.endsWith("&")) { + thumbUrl = thumbUrl += "format=jpeg"; + } else { + thumbUrl = thumbUrl += "&format=jpeg"; + } + } else { + thumbUrl = thumbUrl += "?format=jpeg"; + } + + out.file = await fetch(thumbUrl) .then((res) => res.arrayBuffer()) .then((buf) => Buffer.from(buf)); } } else { for (const embed of msg.embeds) { if (!embed.url) continue; - if (embed.url && /(jpe?g|png|gif|webp)$/.test(embed.url)) { + const parsed = new URL(embed.url); + if (embed.url && /(jpe?g|png|gif|webp)$/.test(parsed.pathname)) { out.url = embed.url; } else if (embed.image) { out.url = embed.image.url; @@ -198,15 +212,40 @@ async function findSuitableImage(msg) { } else if (embed.video) { out.video = true; out.url = "attachment://thumb.jpg"; - out.file = await fetch( - (embed.video.proxyURL ?? - embed.video.url.replace( - "cdn.discordapp.com", - "media.discordapp.net" - )) + "?format=jpeg" - ) - .then((res) => res.arrayBuffer()) - .then((buf) => Buffer.from(buf)); + + let thumbUrl = + embed.video.proxyURL ?? + embed.video.url.replace("cdn.discordapp.com", "media.discordapp.net"); + if ( + thumbUrl.includes("media.discordapp.net") && + thumbUrl.includes("?") + ) { + if (thumbUrl.endsWith("&")) { + thumbUrl = thumbUrl += "format=jpeg"; + } else { + thumbUrl = thumbUrl += "&format=jpeg"; + } + } else { + thumbUrl = thumbUrl += "?format=jpeg"; + } + if (embed.thumbnail?.url) thumbUrl = embed.thumbnail.url; + + let valid = await fetch(thumbUrl, {method: "HEAD"}).then( + (res) => res.ok + ); + if (!valid && thumbUrl.includes("media.discordapp.net")) { + thumbUrl = await hf.bot.requestHandler + .request("POST", "/attachments/refresh-urls", true, { + attachment_urls: [thumbUrl], + }) + .then((res) => res.refreshed_urls[0].refreshed); + valid = true; + } + + if (valid) + out.file = await fetch(thumbUrl) + .then((res) => res.arrayBuffer()) + .then((buf) => Buffer.from(buf)); break; } } @@ -216,16 +255,11 @@ async function findSuitableImage(msg) { } async function createBoardMessage(msg, count, fetchAttachment = true) { + const name = msg.member?.nick ?? msg.author.globalName ?? msg.author.username; const embed = { - title: `${count} \u2b50`, - color: pastelize(msg.author.username), + title: `${count} \u2b50 - ${msg.jumpLink}`, + color: pastelize(name), description: msg.content, - fields: [ - { - name: "Jump Link", - value: `[Jump](${msg.jumpLink})`, - }, - ], timestamp: new Date(msg.timestamp).toISOString(), }; @@ -246,7 +280,7 @@ async function createBoardMessage(msg, count, fetchAttachment = true) { return { avatarURL: msg.member?.avatarURL ?? msg.author.avatarURL, - username: msg.member?.displayName ?? msg.author.username, + username: name, threadID: VINBOARD_THREAD_ID, embeds: [embed], attachments: image?.file