-
Notifications
You must be signed in to change notification settings - Fork 2
/
registry.js
109 lines (104 loc) · 3.81 KB
/
registry.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Copyright 2021 Aman Verma. All rights reserved.
Use of this source code is governed by a MIT license that can be
found in the LICENSE file.
*/
const c = require("ansi-colors");
const fs = require("fs").promises;
const path = require("path");
const { checkCommandModule, checkProperties } = require("./validate");
const commandStatus = [
[
`${c.bold("Command")}`,
`${c.bold("Status")}`,
`${c.bold("Description")}`
]
],
eventStatus = [
[
`${c.bold("Event")}`,
`${c.bold("Status")}`,
`${c.bold("Description")}`
]
];
async function registerCommands(client, dir) {
let files = await fs.readdir(path.join(__dirname, dir));
// Loop through each file.
for (let file of files) {
let stat = await fs.lstat(path.join(__dirname, dir, file));
if (stat.isDirectory())
// If file is a directory, recursive call recurDir
registerCommands(client, path.join(dir, file));
else {
// Check if file is a .js file.
if (file.endsWith(".js")) {
let cmdNameFrmFile = file.substring(0, file.indexOf(".js"));
try {
let cmdModule = require(path.join(__dirname, dir, file));
if (checkCommandModule(cmdNameFrmFile, cmdModule)) {
let cmdName = cmdModule.name;
if (checkProperties(cmdModule)) {
let aliases = cmdModule.aliases;
client.commands.set(cmdName, cmdModule.run);
if (aliases.length !== 0)
aliases.forEach((alias) =>
client.commands.set(alias, cmdModule.run)
);
commandStatus.push([
`${c.cyan(`${cmdName}`)}`,
`${c.bgGreenBright("Success")}`,
`${cmdModule.description}`
]);
}
}
} catch (err) {
console.log(err);
commandStatus.push([
`${c.white(`${cmdNameFrmFile}`)}`,
`${c.bgRedBright("Failed")}`,
""
]);
}
}
}
}
// console.log(client.commands);
}
async function registerEvents(client, dir) {
let files = await fs.readdir(path.join(__dirname, dir));
// Loop through each file.
for (let file of files) {
let stat = await fs.lstat(path.join(__dirname, dir, file));
if (stat.isDirectory())
// If file is a directory, recursive call recurDir
registerEvents(client, path.join(dir, file));
else {
// Check if file is a .js file.
if (file.endsWith(".js")) {
let eventName = file.substring(0, file.indexOf(".js"));
try {
let eventModule = require(path.join(__dirname, dir, file));
client.on(eventName, eventModule.bind(null, client));
eventStatus.push([
`${c.cyan(`${eventName}`)}`,
`${c.bgGreenBright("Success")}`,
`${eventModule.description}`
]);
} catch (err) {
console.log(err);
eventStatus.push([
`${c.white(`${eventName}`)}`,
`${c.bgRedBright("Failed")}`,
""
]);
}
}
}
}
}
module.exports = {
commandStatus,
eventStatus,
registerEvents,
registerCommands
};