utils: getImage function

This commit is contained in:
Cynthia Foxwell 2021-06-01 12:59:24 -06:00
parent 416b79ca97
commit 6882af6510

View file

@ -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 (/<a?:[a-zA-Z0-9_]*:([0-9]*)>/.test(str)) {
const id = str.match(/<a?:[a-zA-Z0-9_]*:([0-9]*)>/)[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) { async function hastebin(body) {
const res = await fetch(`${hf.config.haste_provider}/documents`, { const res = await fetch(`${hf.config.haste_provider}/documents`, {
method: "POST", method: "POST",
@ -112,5 +143,6 @@ module.exports = {
readableTime, readableTime,
isGif, isGif,
findLastImage, findLastImage,
getImage,
hastebin, hastebin,
}; };