utility.presence: more image fixes

This commit is contained in:
Cynthia Foxwell 2022-10-10 13:21:17 -06:00
parent 147e1d5028
commit 6176ba0b07

View file

@ -787,19 +787,62 @@ presence.callback = async function (msg, line) {
embed.description = descLines.join("\n"); embed.description = descLines.join("\n");
if (activity.assets?.large_image) { if (activity.assets) {
let largeUrl; if (activity.assets.large_image != null) {
if (activity.assets.large_image.startsWith("mp:")) { let largeUrl;
largeUrl = activity.large_image.replace( if (activity.assets.large_image.startsWith("mp:")) {
"mp:", largeUrl = activity.large_image.replace(
"https://media.discordapp.net/" "mp:",
); "https://media.discordapp.net/"
} else { );
largeUrl = `https://cdn.discordapp.com/app-assets/${activity.application_id}/${activity.assets.large_image}.png`; } else {
} largeUrl = `https://cdn.discordapp.com/app-assets/${activity.application_id}/${activity.assets.large_image}.png`;
}
let smallUrl; let smallUrl;
if (activity.assets.small_image) { 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:")) { if (activity.assets.small_image.startsWith("mp:")) {
smallUrl = activity.small_image.replace( smallUrl = activity.small_image.replace(
"mp:", "mp:",
@ -808,38 +851,24 @@ presence.callback = async function (msg, line) {
} else { } else {
smallUrl = `https://cdn.discordapp.com/app-assets/${activity.application_id}/${activity.assets.small_image}.png`; 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) const smallImage = await fetch(smallUrl)
.then((res) => res.arrayBuffer()) .then((res) => res.arrayBuffer())
.then((b) => Buffer.from(b)); .then((b) => Buffer.from(b));
const smallImageBuffer = await sharp(smallImage) const presenceImage = await sharp(smallImage)
.resize(20, 20) .resize(60, 60)
.toBuffer(); .toBuffer();
presenceImage.composite([ files.push({
{ content: presenceImage,
input: smallImageBuffer, name: `${index}.png`,
gravity: "southeast", });
}, 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);
} }
} }