This repository has been archived by the owner on Sep 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.js
60 lines (53 loc) · 1.64 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
const config = require('./config.json')
const commandMap = new Map()
const helpMap = new Map()
const helpCommands = new Array()
async function parseCommand(msg) {
// Split commands into sections
const args = msg.content.slice(config.prefix.length).split(/ +/);
// The first "arg" is the command
const cmd = args.shift().toLowerCase();
// Lookup a command from the HashMap --> O(1)
const qry = commandMap[cmd]
if (qry) {
const res = await qry.exec(msg, args)
if (!res) {
msg.reply('There were some problems running your command...')
msg.react('❌')
}
} else {
// Do this if none of the above commands are correct...
msg.reply('Command not found...')
msg.react('❌')
}
console.log(`${msg.author.tag} executed ${msg.content}`);
};
function init() {
registerCommand('help')
registerCommand('suggest')
registerCommand('join')
registerCommand('leave')
console.log(commandMap)
console.log(helpMap)
}
// hashes a map to do a look up whenever a command is ran
// basically, hash the command once and save time every time someone runs a command
function registerCommand(command) {
const c = require(`./commands/${command}`)
const o = new c()
const aliases = o.getAliases()
aliases.forEach(alias => {
alias = alias.toLowerCase()
commandMap[alias] = o
helpMap[alias] = o.getHelp()
});
helpCommands.push(aliases[0])
}
// ES5 exports (since this isn't ES6)
module.exports = {
commandMap: commandMap,
helpMap: helpMap,
helpCommands: helpCommands,
init: init,
parse: parseCommand,
}