From 6882af65100c05d220de537e1c3c80f30daa2751 Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Tue, 1 Jun 2021 12:59:24 -0600 Subject: [PATCH] utils: getImage function --- src/lib/utils.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/lib/utils.js b/src/lib/utils.js index 2ec0d90..225fb4e 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -96,6 +96,37 @@ async function findLastImage(msg, gif = false) { }); } +const urlRegex = /((https?):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+)/; + +async function getImage(msg, str) { + const img = await findLastImage(msg, false); + if (!str) { + if (img) return img; + } + + if (msg.attachments[0] && msg.attachments[0].url) { + return msg.attachments[0].url; + } else if (urlRegex.test(str)) { + return str; + } else if (//.test(str)) { + const id = str.match(//)[1]; + return `https://cdn.discordapp.com/emojis/${id}.png?v=1`; + } else if (/<@!?([0-9]*)>/.test(str)) { + const id = url.match(/<@?!([0-9]*)>/)[1]; + const user = await hf.bot.requestHandler.request( + "GET", + "/users/" + id, + true + ); + if (user) + return `https://cdn.discordapp.com/avatars/${id}/${user.avatar}.png?size=1024`; + } else if (img) { + return img; + } + + return null; +} + async function hastebin(body) { const res = await fetch(`${hf.config.haste_provider}/documents`, { method: "POST", @@ -112,5 +143,6 @@ module.exports = { readableTime, isGif, findLastImage, + getImage, hastebin, };