forked from RyanHirsch/irc-logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.js
47 lines (42 loc) · 1.37 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
var _ = require('lodash'),
fs = require('fs');
var invoke_string = '!log';
var commands = {
// join : require('./commands/join'),
// leave : require('./commands/leave'),
};
// rehash and reload our commands
var rehash = function(directory) {
var path = __dirname + '/' + directory,
hashMatch = new RegExp('^' + path + '/', 'gi');
_.forEach(require.cache, function(item) {
if(item.id.match(hashMatch)) {
delete require.cache[item.id];
}
});
fs.readdir(directory, function(err, files) {
if(err) console.log('ERROR: ' + err);
_.forEach(files, function(file) {
// join, leave, quit
var fileName = file.substring(0, file.length - 3)
commands[fileName] = require('./' + directory + '/' + fileName);
});
});
};
var processMessage = function(bot, from, to, message) {
var tokens = message.split(' ');
if(!_.isArray(tokens) || tokens[0] !== invoke_string) { return; }
if(commands.hasOwnProperty(tokens[1])) {
var joinedTokens = tokens.slice(2).join(' ');
commands[tokens[1]]({
bot: bot,
from: from,
to: to,
fullMessage: message,
message: joinedTokens
});
}
}
exports.processMessage = processMessage;
exports.rehash = function() { rehash('commands'); };
// !log leave <channel>