From 6176ba0b07fb9bd0d7d3c2283917958be62291c2 Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Mon, 10 Oct 2022 13:21:17 -0600 Subject: [PATCH] utility.presence: more image fixes --- src/modules/utility.js | 101 ++++++++++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 36 deletions(-) diff --git a/src/modules/utility.js b/src/modules/utility.js index f3ff7e9..e4c06aa 100644 --- a/src/modules/utility.js +++ b/src/modules/utility.js @@ -787,19 +787,62 @@ presence.callback = async function (msg, line) { embed.description = descLines.join("\n"); - if (activity.assets?.large_image) { - let largeUrl; - if (activity.assets.large_image.startsWith("mp:")) { - largeUrl = activity.large_image.replace( - "mp:", - "https://media.discordapp.net/" - ); - } else { - largeUrl = `https://cdn.discordapp.com/app-assets/${activity.application_id}/${activity.assets.large_image}.png`; - } + if (activity.assets) { + if (activity.assets.large_image != null) { + let largeUrl; + if (activity.assets.large_image.startsWith("mp:")) { + largeUrl = activity.large_image.replace( + "mp:", + "https://media.discordapp.net/" + ); + } else { + largeUrl = `https://cdn.discordapp.com/app-assets/${activity.application_id}/${activity.assets.large_image}.png`; + } - let smallUrl; - if (activity.assets.small_image) { + let smallUrl; + if (activity.assets.small_image != null) { + if (activity.assets.small_image.startsWith("mp:")) { + smallUrl = activity.small_image.replace( + "mp:", + "https://media.discordapp.net/" + ); + } else { + smallUrl = `https://cdn.discordapp.com/app-assets/${activity.application_id}/${activity.assets.small_image}.png`; + } + } + + const largeImage = await fetch(largeUrl) + .then((res) => res.arrayBuffer()) + .then((b) => Buffer.from(b)); + const presenceImage = sharp(largeImage).resize(60, 60); + if (smallUrl) { + const smallImage = await fetch(smallUrl) + .then((res) => res.arrayBuffer()) + .then((b) => Buffer.from(b)); + const smallImageBuffer = await sharp(smallImage) + .resize(20, 20) + .toBuffer(); + + presenceImage.composite([ + { + input: smallImageBuffer, + gravity: "southeast", + }, + ]); + } + + files.push({ + content: await presenceImage.toBuffer(), + name: `${index}.png`, + }); + embed.thumbnail = { + url: `attachment://${index}.png`, + }; + } else if ( + !activity.assets.large_image && + activity.assets.small_image != null + ) { + let smallUrl; if (activity.assets.small_image.startsWith("mp:")) { smallUrl = activity.small_image.replace( "mp:", @@ -808,38 +851,24 @@ presence.callback = async function (msg, line) { } else { smallUrl = `https://cdn.discordapp.com/app-assets/${activity.application_id}/${activity.assets.small_image}.png`; } - } - const largeImage = await fetch(largeUrl) - .then((res) => res.arrayBuffer()) - .then((b) => Buffer.from(b)); - const presenceImage = sharp(largeImage).resize(60, 60); - if (smallUrl) { const smallImage = await fetch(smallUrl) .then((res) => res.arrayBuffer()) .then((b) => Buffer.from(b)); - const smallImageBuffer = await sharp(smallImage) - .resize(20, 20) + const presenceImage = await sharp(smallImage) + .resize(60, 60) .toBuffer(); - presenceImage.composite([ - { - input: smallImageBuffer, - gravity: "southeast", - }, - ]); + files.push({ + content: presenceImage, + name: `${index}.png`, + }); + embed.thumbnail = { + url: `attachment://${index}.png`, + }; } - - files.push({ - content: await presenceImage.toBuffer(), - name: `${index}.png`, - }); - embed.thumbnail = { - url: `attachment://${index}.png`, - }; - - embeds.push(embed); } + embeds.push(embed); } }