From c02eda1b2a7bfb0ef8d77c7ed7b02be8196123ee Mon Sep 17 00:00:00 2001 From: derpystuff <3515180-derpystuff@users.noreply.gitlab.com> Date: Wed, 25 May 2022 21:53:03 +0200 Subject: [PATCH] [cmd] enlarge --- commands/message/utils/enlarge.js | 105 ++++++++++++++++++++++++++++++ labscore/api/endpoints.js | 10 ++- package.json | 1 + 3 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 commands/message/utils/enlarge.js diff --git a/commands/message/utils/enlarge.js b/commands/message/utils/enlarge.js new file mode 100644 index 0000000..34aa29f --- /dev/null +++ b/commands/message/utils/enlarge.js @@ -0,0 +1,105 @@ +const { Constants, Utils } = require("detritus-client"); +const superagent = require('superagent'); + +const { Static } = require("../../../labscore/api/endpoints"); +const { createEmbed } = require("../../../labscore/utils/embed"); +const { editOrReply } = require("../../../labscore/utils/message"); + +const onlyEmoji = require('emoji-aware').onlyEmoji; + +function toCodePoint(unicodeSurrogates) { + const r = []; + let c = 0; + let p = 0; + let i = 0; + + while (i < unicodeSurrogates.length) { + c = unicodeSurrogates.charCodeAt(i++); + if (p) { + r.push((0x10000 + ((p - 0xD800) << 10) + (c - 0xDC00)).toString(16)); + p = 0; + } else if (0xD800 <= c && c <= 0xDBFF) { + p = c; + } else { + r.push(c.toString(16)); + } + } + return r.join('-'); +} + +module.exports = { + label: "emoji", + name: "enlarge", + aliases: ['e', 'emote'], + metadata: { + description: 'Enlarge Emoji.', + examples: ['enlarge 😀'], + category: 'util', + usage: 'enlarge ' + }, + args: [ + {name: 'type', default: 'twitter'} + ], + run: async (context, args) => { + await context.triggerTyping(); + const { matches } = Utils.regex( + Constants.DiscordRegexNames.EMOJI, + args.emoji + ); + embeds = [] + if (matches.length) { + let form = '.png' + if(matches[0].animated) form = '.gif' + return editOrReply(context, {embeds:[ + createEmbed("default", context, { + description: `**${matches[0].name}**`, + image: { + url: `https://cdn.discordapp.com/emojis/${matches[0].id}${form}` + } + }) + ]}) + } else { + const emoji = onlyEmoji(args.emoji) + if(!emoji){ + return editOrReply(context, {embeds:[ + createEmbed("warning", context, "No emoji found.") + ]}) + } else { + if(!Static[args.type.toUpperCase()]){ + return editOrReply(context, {embeds:[ + createEmbed("warning", context, "Invalid type.") + ]}) + } + + const emojiCodepoint = emoji.map((e) => toCodePoint(e)); + if(!emojiCodepoint.length){ + return editOrReply(context, {embeds:[ + createEmbed("warning", context, "No emoji found.") + ]}) + } + targetEmoji = emojiCodepoint[0] + if(args.type.toUpperCase() == "TWITTER"){ + targetEmoji = emojiCodepoint[0].replace('-fe0f', '') + } + if(args.type.toUpperCase() == "FLUENT"){ + targetEmoji = emojiCodepoint[0].replace('-fe0f', '') + } + let emojiUrl = Static.HOST + Static[args.type.toUpperCase()](targetEmoji) + try{ + let e = await superagent.get(emojiUrl) + }catch(e){ + return editOrReply(context, {embeds:[ + createEmbed("error", context, "No image for provided emoji.") + ]}) + } + return editOrReply(context, {embeds:[ + createEmbed("default", context, { + image: { + url: emojiUrl + } + }) + ]}) + } + } + } +}; \ No newline at end of file diff --git a/labscore/api/endpoints.js b/labscore/api/endpoints.js index 13a1e46..4cd448e 100644 --- a/labscore/api/endpoints.js +++ b/labscore/api/endpoints.js @@ -32,9 +32,13 @@ const Api = Object.freeze({ const Static = Object.freeze({ HOST: Hosts.emoji, - TWITTER: (codepoint) => { - return `twemoji-JedKxRr7RNYrgV9Sauy8EGAu/${codepoint}.png` - } + TWITTER: (codepoint) => { return `twemoji-JedKxRr7RNYrgV9Sauy8EGAu/${codepoint}.png` }, + FLUENT: (codepoint) => { return `fluent-6vbne6euaxy2y9f98iub2xtr/${codepoint}.png` }, + //APPLE: (codepoint) => { return `twemoji-JedKxRr7RNYrgV9Sauy8EGAu/${codepoint}.png` }, // TODO: host these in-house + MICROSOFT: (codepoint) => { return `microsoft-ZzRAzYE6LgxVTrQ5rvL7nLyC/${codepoint}.png` }, + EMOJIONE: (codepoint) => { return `emojione-XghVAypW8jttjFL2tQFb2z7n/${codepoint}.png` }, + GOOGLE: (codepoint) => { return `google-tqzSNjYw8MVMYfSBLTLTFgmw/${codepoint}.png` }, + BLOBS: (codepoint) => { return `blobs-KpDmEXYD3VTC2VT6PSQAc99y/${codepoint}.png` } }) module.exports = { diff --git a/package.json b/package.json index f65aea8..7a29ba8 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "dependencies": { "detritus-client": "^0.17.0-beta.12", "dotenv": "^16.0.1", + "emoji-aware": "^3.1.0", "express": "^4.17.1", "superagent": "^7.1.1" }