-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
50 lines (41 loc) · 1.26 KB
/
helpers.js
File metadata and controls
50 lines (41 loc) · 1.26 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
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const config = yaml.load(
fs.readFileSync(path.resolve(__dirname, 'config.yml'))
);
module.exports = {};
module.exports.Commands = class {
constructor() {
this.commands = { };
}
add(command, handler) {
[command.name, ...command.aliases || []].map((cmd) => {
this.commands[cmd] = handler;
});
}
exists(command) {
return command in this.commands;
}
execute(command, message, args=[]) {
this.commands[command].apply(null, [message, ...args]);
}
};
module.exports.messageHandler = function(commands, message) {
if (message.content.startsWith(config.commandPrefix)) {
const command = message.content.substring(config.commandPrefix.length)
.split(' ')[0];
if (commands.exists(command)) {
const args = message.content.split(' ').slice(1);
commands.execute(command, message, args);
}
}
};
module.exports.login = function(client) {
const token = process.env.DISCORD_API_TOKEN || config.token;
if (token) {
client.login(token);
} else {
console.log('No token provided in environment variables or config.yml');
}
};