106 lines
2.8 KiB
JavaScript
106 lines
2.8 KiB
JavaScript
const path = require('path');
|
|
const fs = require('fs');
|
|
const fetch = require('node-fetch');
|
|
const { Util: DiscordUtil } = require('discord.js');
|
|
|
|
const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
|
|
|
|
class Util {
|
|
|
|
constructor() {
|
|
throw new Error("Class may not be instantiated.");
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
static delayFor(ms) {
|
|
return new Promise((resolve) => {
|
|
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;
|
|
}
|
|
|
|
//Shard Managing
|
|
|
|
static fetchRecommendedShards(token, guildsPerShard = 1000) {
|
|
if(!token) throw new Error("[util] Token missing.");
|
|
return fetch("https://discordapp.com/api/v7/gateway/bot", {
|
|
method: 'GET',
|
|
headers: { Authorization: `Bot ${token.replace(/^Bot\s*/iu, '')}` }
|
|
}).then((res) => {
|
|
if (res.ok) return res.json();
|
|
throw res;
|
|
})
|
|
.then((data) => data.shards * (1000 / guildsPerShard));
|
|
}
|
|
|
|
static makePlainError(err) {
|
|
return {
|
|
name: err.name,
|
|
message: err.message,
|
|
stack: err.stack
|
|
};
|
|
}
|
|
|
|
static escapeMarkdown(text) {
|
|
return DiscordUtil.escapeMarkdown(text);
|
|
}
|
|
|
|
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;
|
|
return `${w ? `${w} ${plural(w, 'week')} ` : ''}${d ? `${d} ${plural(w, 'day')} ` : ''}${h ? `${h} ${plural(w, 'hour')} ` : ''}${m ? `${m} ${plural(w, 'minute')} ` : ''}${s ? `${s} ${plural(w, 'second')} ` : ''}`.trim();
|
|
}
|
|
|
|
static plural(amt, word) {
|
|
if(amt === 1) return word;
|
|
return `${word}s`;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
module.exports = Util; |