-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
154 lines (142 loc) · 3.01 KB
/
bot.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var irc = require('irc');
var fs = require('fs');
var readline = require('readline');
var config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
var quiz = [];
var listeners = {
'registered': [
function handler(message){
console.log('IRC connection succeeded');
bot.send("MODE", bot.nick, "+B");
config.channels.forEach(function(name){
bot.join(name, function(){
var lowerName = name.toLowerCase();
if(!(lowerName in quiz)){
quiz[lowerName] = require('./quiz');
quiz[lowerName].init(name, bot);
}
});
});
}
],
'ctcp-version': [
function handler(from, to, message){
bot.ctcp(from, "notice", "VERSION PIRCbot/nodejs/irc (k4be) v0.02");
}
],
'error': [
function handler(message){
console.log('error: ', message);
}
]/*,
'message': [
function handler(nick, to, text, message){
}
]*/
};
var bot = new irc.Client(config.server, config.nick, config.options);
for(var key in listeners){
var functions = listeners[key];
for(var i=0; i<functions.length; i++){
bot.addListener(key, functions[i]);
}
}
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: true
});
rl.on('line', function(line){
processCommand(myConsole, line);
});
var cmdPrefix = "";
var myConsole = {
'send': function(text){
console.log(text);
}
};
var cmdBinds = {
'CMD': [
function(src, cmd, args){
if(args.length == 0){
src.send('Command prefix disabled');
cmdPrefix = '';
} else {
cmdPrefix = args.join(' ');
src.send('Command prefix set to "'+cmdPrefix+'"');
}
}
],
'SAY': [
function(src, cmd, args){
if(args.length < 2){
src.send('Usage: SAY #channel text');
return;
}
var chan = args.shift();
bot.say(chan, args.join(' '));
}
],
'DIE': [
function(src, cmd, args){
if(args.length > 0){
var reason = args.join(' ');
} else {
var reason = 'Quit';
}
bot.disconnect(reason);
setTimeout(function(){
process.exit();
}, 5000);
}
],
'QUIZ': [
function(src, cmd, args){
if(args.length < 2){
src.send('Usage: QUIZ #channel command');
return;
}
var chan = args.shift().toLowerCase();
var quizcmd = args.shift().toUpperCase();
if(chan in quiz){
var binds = quiz[chan].cmdBinds();
if(quizcmd in binds){
binds[quizcmd](src, quizcmd, args);
} else {
src.send('No such QUIZ command: '+quizcmd);
}
} else {
src.send('No such quiz channel: '+chan);
}
}
]
};
function processCommand(src, line){
if(line == '') return;
if(!line.toLowerCase().startsWith('cmd')){
line = cmdPrefix + ' ' + line;
}
line = line.trim();
var idx = line.indexOf(' ');
var cmd = '';
var arg = '';
if(idx < 0){
cmd = line;
} else {
cmd = line.slice(0, idx);
arg = line.slice(idx+1);
}
cmd = cmd.toUpperCase();
if(arg.length == 0){
var args = [];
} else {
var args = arg.split(' ');
}
if(cmd in cmdBinds){
cmdBinds[cmd].forEach(function(handler){
handler(src, cmd, args);
});
} else {
src.send('No such command: '+cmd);
}
};