From 8f4b230326462cdc54df06f84774e8693c5236a3 Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Mon, 10 Oct 2022 13:13:02 -0600 Subject: [PATCH] utility.presence: add activities --- src/modules/utility.js | 137 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 134 insertions(+), 3 deletions(-) diff --git a/src/modules/utility.js b/src/modules/utility.js index 6e99f8a..508b8bc 100644 --- a/src/modules/utility.js +++ b/src/modules/utility.js @@ -59,7 +59,7 @@ const CUSTOM_EMOTE_REGEX = /<(?:\u200b|&)?(a)?:(\w+):(\d+)>/; const sharp = require("sharp"); -const {hastebin, lookupUser} = require("../lib/utils.js"); +const {hastebin, lookupUser, formatTime} = require("../lib/utils.js"); const {getNamesFromString} = require("../lib/unicode.js"); const avatar = new Command("avatar"); @@ -580,7 +580,9 @@ jumbo.callback = async function (msg, line) { } if (set.suffix == ".svg") { - const svg = await fetch(url).then((res) => res.buffer()); + const svg = await fetch(url) + .then((res) => res.arrayBuffer()) + .then((b) => Buffer.from(b)); const converted = await sharp(svg, {density: 2400}) .resize(1024) .toBuffer(); @@ -663,6 +665,17 @@ const PRESENCE_ICONS = { }, }; +const PRESENCE_TYPES = [ + "Playing", + "Streaming", + "Listening to", + "Watching", + "", + "Competing in", +]; + +const NOWPLAYING_BAR_LENGTH = 30; + const presence = new Command("presence"); presence.category = CATEGORY; presence.helpText = "Get presences of a user."; @@ -690,8 +703,8 @@ presence.callback = async function (msg, line) { userIDs: [user.id], }); member = fetched[0]; - target = member; } + target = member; } } } else { @@ -709,9 +722,127 @@ presence.callback = async function (msg, line) { } const embeds = []; + const files = []; + + for (const index in target.presence.activities) { + const activity = target.presence.activities[index]; + if (activity.type == 4) { + let emote = ""; + if (activity.emoji.id) { + emote = `<${activity.emoji.animated ? "a" : ""}:${ + activity.emoji.name + }:${activity.emoji.id}>`; + } else { + emote = activity.emoji.name; + } + + embeds.push({ + title: `${emote} ${activity.state}`, + }); + } else { + const embed = { + title: `${PRESENCE_TYPES[activity.type]} **${activity.name}**`, + }; + const descLines = []; + if (activity.type == 2) { + if (activity.details) descLines.push(`**${activity.details}**`); + if (activity.state) descLines.push(activity.state); + if (activity.assets?.large_text) + descLines.push(activity.assets.large_text); + } else { + if (activity.details) descLines.push(activity.details); + if (activity.state) descLines.push(activity.state); + } + + if (activity.timestamps.start && !activity.timestamps.end) { + descLines.push( + formatTime(Date.now() - activity.timestamps.start) + " elapsed" + ); + } else if (!activity.timestamps.start && activity.timestamps.end) { + descLines.push( + formatTime(activity.timestamps.end - Date.now()) + " remaining" + ); + } else { + const position = Date.now() - activity.timestamps.start; + const length = activity.timestamps.end - activity.timestamps.start; + + const timeEnd = formatTime(length); + const timePos = formatTime(position); + + const progress = position / length; + const barLength = Math.round(progress * NOWPLAYING_BAR_LENGTH); + + const bar = `\`[${"=".repeat(barLength)}${" ".repeat( + NOWPLAYING_BAR_LENGTH - barLength + )}]\``; + const time = `\`${timePos}${" ".repeat( + NOWPLAYING_BAR_LENGTH + 2 - timePos.length - timeEnd.length + )}${timeEnd}\``; + + descLines.push(bar); + descLines.push(time); + } + + 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`; + } + + let smallUrl; + if (activity.assets.small_image) { + 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 smallImageSharp = sharp(smallImage).resize(20, 20); + + presenceImage.composite([ + { + input: smallImageSharp, + gravity: "southeast", + }, + ]); + } + + files.push({ + content: presenceImage.toBuffer(), + name: `${index}.png`, + }); + embed.thumbnail = { + url: `attachment://${index}.png`, + }; + + embeds.push(embed); + } + } + } return { content: `Presence for **${target.tag}**: ${icons}`, + embeds, + files, }; } else { return ":warning: Could not get user???";