2021-03-15 01:41:40 +00:00
|
|
|
const {v3} = require("murmurhash");
|
|
|
|
const colorcolor = require("colorcolor");
|
2021-03-15 04:23:14 +00:00
|
|
|
const fetch = require("node-fetch");
|
2021-03-15 01:41:40 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-03-15 04:23:14 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function hastebin(body) {
|
|
|
|
const res = await fetch(`${hf.config.haste_provider}/documents`, {
|
|
|
|
method: "POST",
|
|
|
|
body,
|
|
|
|
}).then((r) => r.json());
|
|
|
|
return res.key;
|
|
|
|
}
|
|
|
|
|
2021-03-15 01:41:40 +00:00
|
|
|
module.exports = {
|
|
|
|
pastelize,
|
|
|
|
getTopColor,
|
2021-03-15 04:23:14 +00:00
|
|
|
safeString,
|
|
|
|
formatTime,
|
|
|
|
readableTime,
|
|
|
|
isGif,
|
|
|
|
findLastImage,
|
|
|
|
hastebin,
|
2021-03-15 01:41:40 +00:00
|
|
|
};
|