HiddenPhox/src/lib/utils.js

149 lines
3.6 KiB
JavaScript

const {v3} = require("murmurhash");
const colorcolor = require("colorcolor");
const fetch = require("node-fetch");
function pastelize(id) {
const hue = v3(id) % 360;
const hex = colorcolor(`hsl(${hue},75%,60%)`, "hex");
return parseInt(hex.substring(1), 16);
}
function getTopColor(msg, id, fallback = 0x7289da) {
if (!msg.channel.guild) return fallback;
const roles = msg.channel.guild.members
.get(id)
.roles.map((role) => msg.channel.guild.roles.get(role))
.filter((role) => role.color);
roles.sort((a, b) => b.position - a.position);
return roles[0]?.color || fallback;
}
function safeString(string, newLines = true) {
string = string ? string.toString() : "";
string = string.replace(/`/g, "'");
string = string.replace(/<@/g, "<@\u200b");
string = string.replace(/<#/g, "<#\u200b");
string = string.replace(/<&/g, "<&\u200b");
if (newLines) string = string.replace(/\n/g, " ");
return string;
}
function formatTime(number) {
let seconds = parseInt(number) / 1000;
const days = seconds / 86400;
seconds = seconds % 86400;
const hours = seconds / 3600;
seconds = seconds % 3600;
const minutes = seconds / 60;
seconds = seconds % 60;
return (
(days !== 0 ? `${days.padStart(2, "0")}:` : "") +
(hours !== 0 ? `${hours.padStart(2, "0")}:` : "") +
`${minutes.padStart(2, "0")}:${seconds.padStart(2, "0")}`
);
}
function readableTime(number) {
const seconds = number / 1000;
const days = seconds / 60 / 60 / 24;
const years = days / 365.25;
if (years >= 1) {
return `${years.toFixed(2)} years`;
} else {
return `${days.toFixed(2)} days`;
}
}
async function isGif(url) {
const type = await fetch(url).then((res) => res.headers.get("Content-Type"));
return type == "image/gif";
}
async function findLastImage(msg, gif = false) {
const messages = await msg.channel.getMessages(20);
let img;
for (const message of messages) {
if (message.attachments.length > 0) {
img = message.attachments[0].url;
if (gif && (await isGif(img))) {
break;
} else {
break;
}
} else if (message.embeds.length > 0) {
img = message.embeds[0]?.thumbnail?.url || message.embeds[0]?.image?.url;
if (img) {
if (gif && (await isGif(img))) {
break;
} else {
break;
}
}
}
}
return await new Promise((resolve, reject) => {
if (!img) {
reject("Image not found in last 20 messages.");
} else {
resolve(img);
}
});
}
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) {
const res = await fetch(`${hf.config.haste_provider}/documents`, {
method: "POST",
body,
}).then((r) => r.json());
return res.key;
}
module.exports = {
pastelize,
getTopColor,
safeString,
formatTime,
readableTime,
isGif,
findLastImage,
getImage,
hastebin,
};