2020-04-08 18:08:46 +02:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
2020-04-09 16:30:52 +02:00
|
|
|
const fetch = require('node-fetch');
|
2020-07-04 12:23:10 +02:00
|
|
|
const { Util: DiscordUtil } = require('discord.js');
|
2020-04-09 16:30:52 +02:00
|
|
|
|
|
|
|
const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
|
2020-04-08 18:08:46 +02:00
|
|
|
|
|
|
|
class Util {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
throw new Error("Class may not be instantiated.");
|
|
|
|
}
|
|
|
|
|
2020-06-20 22:28:53 +02:00
|
|
|
static downloadAsBuffer(source) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-07-20 00:42:21 +02:00
|
|
|
fetch(source).then((res) => {
|
2020-06-20 22:28:53 +02:00
|
|
|
if(res.ok) resolve(res.buffer());
|
|
|
|
else reject(res.statusText);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-08 18:08:46 +02:00
|
|
|
static readdirRecursive(directory) {
|
|
|
|
|
|
|
|
const result = [];
|
|
|
|
|
|
|
|
(function read(directory) {
|
|
|
|
const files = fs.readdirSync(directory);
|
|
|
|
for(const file of files) {
|
|
|
|
const filePath = path.join(directory, file);
|
|
|
|
|
|
|
|
if(fs.statSync(filePath).isDirectory()) {
|
|
|
|
read(filePath);
|
|
|
|
} else {
|
|
|
|
result.push(filePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(directory));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-09 16:30:52 +02:00
|
|
|
static delayFor(ms) {
|
2020-06-02 12:09:28 +02:00
|
|
|
return new Promise((resolve) => {
|
2020-04-09 16:30:52 +02:00
|
|
|
setTimeout(resolve, ms);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static mergeDefault(def, given) {
|
|
|
|
if (!given) return def;
|
|
|
|
for (const key in def) {
|
|
|
|
if (!has(given, key) || given[key] === undefined) {
|
|
|
|
given[key] = def[key];
|
|
|
|
} else if (given[key] === Object(given[key])) {
|
|
|
|
given[key] = Util.mergeDefault(def[key], given[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return given;
|
|
|
|
}
|
|
|
|
|
2020-06-02 12:09:28 +02:00
|
|
|
//Shard Managing
|
|
|
|
|
2020-04-09 16:30:52 +02:00
|
|
|
static fetchRecommendedShards(token, guildsPerShard = 1000) {
|
|
|
|
if(!token) throw new Error("[util] Token missing.");
|
|
|
|
return fetch("https://discordapp.com/api/v7/gateway/bot", {
|
|
|
|
method: 'GET',
|
2020-06-04 19:59:09 +02:00
|
|
|
headers: { Authorization: `Bot ${token.replace(/^Bot\s*/iu, '')}` }
|
2020-06-02 12:09:28 +02:00
|
|
|
}).then((res) => {
|
2020-04-09 16:30:52 +02:00
|
|
|
if (res.ok) return res.json();
|
|
|
|
throw res;
|
2020-06-02 12:09:28 +02:00
|
|
|
})
|
|
|
|
.then((data) => data.shards * (1000 / guildsPerShard));
|
|
|
|
}
|
|
|
|
|
|
|
|
static makePlainError(err) {
|
|
|
|
return {
|
|
|
|
name: err.name,
|
|
|
|
message: err.message,
|
|
|
|
stack: err.stack
|
|
|
|
};
|
2020-04-09 16:30:52 +02:00
|
|
|
}
|
|
|
|
|
2020-07-16 09:54:39 +02:00
|
|
|
static escapeMarkdown(text, options) {
|
|
|
|
return DiscordUtil.escapeMarkdown(text, options);
|
2020-06-04 19:59:09 +02:00
|
|
|
}
|
|
|
|
|
2020-06-16 00:15:13 +02:00
|
|
|
static duration(seconds) {
|
|
|
|
const { plural } = this;
|
|
|
|
let s = 0,
|
|
|
|
m = 0,
|
|
|
|
h = 0,
|
|
|
|
d = 0,
|
|
|
|
w = 0;
|
|
|
|
s = Math.floor(seconds);
|
|
|
|
m = Math.floor(s/60);
|
|
|
|
s %= 60;
|
|
|
|
h = Math.floor(m/60);
|
|
|
|
m %= 60;
|
|
|
|
d = Math.floor(h/24);
|
|
|
|
h %= 24;
|
|
|
|
w = Math.floor(d/7);
|
|
|
|
d %= 7;
|
2020-07-16 09:54:39 +02:00
|
|
|
return `${w ? `${w} ${plural(w, 'week')} ` : ''}${d ? `${d} ${plural(d, 'day')} ` : ''}${h ? `${h} ${plural(h, 'hour')} ` : ''}${m ? `${m} ${plural(m, 'minute')} ` : ''}${s ? `${s} ${plural(s, 'second')} ` : ''}`.trim();
|
2020-06-16 00:15:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static plural(amt, word) {
|
|
|
|
if(amt === 1) return word;
|
|
|
|
return `${word}s`;
|
|
|
|
}
|
|
|
|
|
2020-04-09 16:30:52 +02:00
|
|
|
|
2020-04-08 18:08:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Util;
|