From be5e820807afa51739c69f28c0ae4aee2ba8bc8a Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Wed, 11 Aug 2021 21:31:56 -0600 Subject: [PATCH] utility: add jumbo --- src/modules/utility.js | 140 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) diff --git a/src/modules/utility.js b/src/modules/utility.js index 8fff737..6373ceb 100644 --- a/src/modules/utility.js +++ b/src/modules/utility.js @@ -5,6 +5,7 @@ const ICON_BASE = "https://cdn.discordapp.com/icons/"; const AVATAR_BASE = "https://cdn.discordapp.com/avatars/"; const SPLASH_BASE = "https://cdn.discordapp.com/splashes/"; const BANNER_BASE = "https://cdn.discordapp.com/banners/"; +const EMOTE_BASE = "https://cdn.discordapp.com/emojis/"; const STATUS_ICONS = { online: "<:online:493173082421461002>", @@ -13,7 +14,54 @@ const STATUS_ICONS = { offline: "<:offline:493173082253426688>", }; +const EMOJI_SETS = { + blobs: { + prefix: + "https://cdn.jsdelivr.net/gh/googlefonts/noto-emoji@e456654119cc3a5f9bebb7bbd00512456f983d2d/svg/emoji_u", + sep: "_", + suffix: ".svg", + }, + noto: { + prefix: "gitcdn.xyz/repo/googlefonts/noto-emoji/master/svg/emoji_u", + sep: "_", + suffix: ".svg", + }, + twemoji: { + prefix: "https://twemoji.maxcdn.com/v/latest/svg/", + sep: "-", + suffix: ".svg", + }, + mustd: { + prefix: + "https://cdn.jsdelivr.net/gh/Mstrodl/mutant-standard-mirror@0435227d9d8c0d6a346c8ae4c12b08a5cdc37041/emoji/", + sep: "-", + suffix: ".svg", + }, + apple: { + prefix: "https://intrnl.github.io/assetsEmoji/AppleColor/emoji_u", + sep: "_", + suffix: ".png", + }, + facebook: { + prefix: "https://intrnl.github.io/assetsEmoji/facebook/emoji_u", + sep: "_", + suffix: ".png", + }, +}; +EMOJI_SETS["noto-old"] = EMOJI_SETS.blobs; +EMOJI_SETS.mutant = EMOJI_SETS.mustd; +EMOJI_SETS.mutstd = EMOJI_SETS.mustd; +EMOJI_SETS.ms = EMOJI_SETS.mustd; +EMOJI_SETS.twitter = EMOJI_SETS.twemoji; +EMOJI_SETS.fb = EMOJI_SETS.facebook; + +const CUSTOM_EMOTE_REGEX = /^<(?:\u200b|&)?(a)?:(\w+):(\d+)>/; + +const fetch = require("node-fetch"); +const sharp = require("sharp"); + const {lookupUser} = require("../lib/utils.js"); +const {getNamesFromString} = require("../lib/unicode.js"); const avatar = new Command("avatar"); avatar.category = CATEGORY; @@ -371,3 +419,95 @@ flagdump.callback = async function (msg, line) { } }; hf.registerCommand(flagdump); + +const emojiNames = []; +fetch("https://unpkg.com/emoji.json/emoji.json") + .then((res) => res.json()) + .then((body) => + body.map( + (emoji) => (emojiNames[emoji.char] = emoji.name.replace(/ /g, "_")) + ) + ); + +const jumbo = new Command("jumbo"); +jumbo.category = CATEGORY; +jumbo.helpText = "Gets the raw image of an emoji."; +jumbo.usage = ""; +jumbo.addAlias("e"); +jumbo.addAlias("emote"); +jumbo.addAlias("emoji"); +jumbo.callback = async function (msg, line) { + if (CUSTOM_EMOTE_REGEX.test(line)) { + const [_, __, animatedFlag, name, id] = line.match(CUSTOM_EMOTE_REGEX); + const animated = animatedFlag === "a"; + + return { + embed: { + title: `:${name}: - \`${id}\``, + url: `${EMOTE_BASE}${id}.${animated ? "gif" : "png"}?v=1`, + image: { + url: `${EMOTE_BASE}${id}.${animated ? "gif" : "png"}?v=1`, + }, + }, + }; + } else { + let setName = "twemoji"; + for (const set in EMOJI_SETS) { + if (line.startsWith(`--${set} `)) { + setName = set; + line = line.replace(`--${set} `, ""); + } + } + + const set = EMOJI_SETS[setName]; + + const emoji = Array.from(line) + .map((char) => char.codePointAt().toString(16)) + .join(set.sep); + const url = set.prefix + emoji + set.suffix; + + const name = emojiNames[line] + ? `\\:${emojiNames[line]}\\:` + : getNamesFromString(line)[0][1]; + + const statusCode = await fetch(url, {method: "HEAD"}).then( + (res) => res.status + ); + + if (statusCode !== 200) { + return "Emoji not found. The emoji set chosen might not have this emote as an image."; + } + + if (set.suffix == ".svg") { + const svg = await fetch(url).then((res) => res.buffer()); + const converted = await sharp(svg, {density: 2400}) + .resize(1024) + .toBuffer(); + + return { + embed: { + title: `${name} (${emoji.toUpperCase().replace(/[-_]/g, ", ")})`, + url, + image: { + url: "attachment://emoji.png", + }, + }, + file: { + file: converted, + name: "emoji.png", + }, + }; + } else { + return { + embed: { + title: `${name} (${emoji.toUpperCase().replace(/[-_]/g, ", ")})`, + url, + image: { + url, + }, + }, + }; + } + } +}; +hf.registerCommand(jumbo);