-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommandMap.js
More file actions
62 lines (52 loc) · 1.37 KB
/
Copy pathcommandMap.js
File metadata and controls
62 lines (52 loc) · 1.37 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
'use strict';
module.exports.findAndRun = findAndRun;
var fs=require('fs');
var __dir='./commands/';
var data={};
console.log('\nRunning commandMap');
var map = [];
function findAndRun(message,replyCallback){
console.log("Searching for commands....");
var match = false;
var run, command;
map.every(function(commandModule){ // fun way of breaking from forEach
if(message.text.search(commandModule.pattern) === 0){
console.log("command matched");
match = true;
run = commandModule.run;
command = commandModule.command;
return false;
}
return true;
});
if(command === 'help'){
run(message.text,map,function(replyText){
message.text = replyText;
replyCallback(message);
});
}
else if(match){
run(message.text,function(replyText){
message.text = replyText;
replyCallback(message);
});
}
else{
console.error("No match found");
message.text = "Command Not Found.\nEnter help to know allowed commands.";
replyCallback(message);
}
}
fs.readdir(__dir,function(err,files){
if (err) throw err;
var c=0;
files.forEach(function(file){
console.log('Requiring Command: ',file);
var commandFile = require(__dir+file);
map.push({
pattern : commandFile.pattern,
run : commandFile.run,
command : commandFile.command
});
});
});