-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.js
76 lines (62 loc) · 1.56 KB
/
commands.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
class Command {
constructor(name, description, callback, config = {}) {
this.name = name;
this.description = description;
this.callback = callback;
this.config = {
requiresArgs: config.requiresArgs || false,
minimumArgs: config.minimumArgs || 0,
usage: config.usage || "",
};
}
async execute(args = [], terminal) {
return new Promise(async (resolve, reject) => {
if(this.config.requiresArgs && args.length < this.config.minimumArgs) {
terminal.write(`<cr>Usage: ${this.config.usage}</cr>`, true);
resolve(false);
return;
}
const request = await this.callback(args, terminal);
console.log(request);
resolve(true);
});
}
getName() {
return this.name;
}
getDescription() {
return this.description;
}
getCallback() {
return this.callback;
}
setName(name) {
this.name = name;
}
setDescription(description) {
this.description = description;
}
setCallback(callback) {
this.callback = callback;
}
}
exports.Command = Command;
function createCommand(name, description, callback) {
return new Command(name, description, callback);
}
exports.createCommand = createCommand;
function init() {
const config = {
dir: __dirname + "/commands",
ext: ".js",
}
const commands = [];
const fs = require("fs");
fs.readdirSync(config.dir).forEach((file) => {
if(!file.endsWith(config.ext)) return;
const command = require(config.dir + "/" + file);
commands.push(command);
});
return commands;
}
exports.init = init;