This repository was archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathUtil.js
More file actions
66 lines (56 loc) · 1.91 KB
/
Util.js
File metadata and controls
66 lines (56 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const path = require('path');
const { promisify } = require('util');
const glob = promisify(require('glob'));
const Command = require('./Command.js');
const Event = require('./Event.js');
module.exports = class Util {
constructor(client) {
this.client = client;
}
isClass(input) {
return typeof input === 'function' &&
typeof input.prototype === 'object' &&
input.toString().substring(0, 5) === 'class';
}
get directory() {
return `${path.dirname(require.main.filename)}${path.sep}`;
}
trimArray(arr, maxLen = 10) {
if (arr.length > maxLen) {
const len = arr.length - maxLen;
arr = arr.slice(0, maxLen);
arr.push(`${len} more...`);
}
return arr;
}
formatBytes(bytes) {
if (bytes === 0) return '0 Bytes';
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return `${parseFloat((bytes / Math.pow(1024, i)).toFixed(2))} ${sizes[i]}`;
}
removeDuplicates(arr) {
return [...new Set(arr)];
}
capitalise(string) {
return string.split(' ').map(str => str.slice(0, 1).toUpperCase() + str.slice(1)).join(' ');
}
async loadCommands() {
return glob(`${this.directory}Commands/**/*.js`).then(commands => {
for (const commandFile of commands) {
delete require.cache[commandFile];
const { name } = path.parse(commandFile);
const File = require(commandFile);
if (!this.isClass(File)) throw new TypeError(`Command ${name} doesn't export a class.`);
const command = new File(this.client, name.toLowerCase());
if (!(command instanceof Command)) throw new TypeError(`Comamnd ${name} doesnt belong in Commands.`);
this.client.commands.set(command.name, command);
if (command.aliases.length) {
for (const alias of command.aliases) {
this.client.aliases.set(alias, command.name);
}
}
}
});
}
};