forked from jacopofar/ircbotjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (67 loc) · 2.09 KB
/
index.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
'use strict';
var fs = require('fs');
var irc = require('irc');
var nconf = require('nconf');
var path = require('path');
nconf.argv()
.env()
.file({ file: 'config.json' })
.defaults({script_directory:'scripts',
sasl:false,
irc_server_password:'',
irc_realname:'ircbotjs',
irc_username:'ircbotjs',
});
if(!nconf.get('irc_nickname')){
console.error("irc_nickname is not defined, exiting!");
process.exit(1);
}
if(!nconf.get('irc_server')){
console.error("irc_server is not defined, exiting!");
process.exit(1);
}
var client = new irc.Client(nconf.get('irc_server'), nconf.get('irc_nickname'), {
userName: nconf.get('irc_username'),
realName: nconf.get('irc_realname'),
port: nconf.get('irc_server_port'),
channels: nconf.get('irc_channels'),
sasl: nconf.get('sasl'),
password: nconf.get('irc_server_password'),
});
client.addListener('error', function(message) {
console.log('IRC error: ', message);
});
//descriptions of the modules having one (that is, module.exports.description)
var visible_modules = [];
fs.readdir(nconf.get('script_directory'),function(err,files){
files.forEach(fileName => {
if(!/.+\.js$/.test(fileName))
return;
fs.stat(path.join(nconf.get('script_directory'),fileName),(err,fstat) => {
if (!fstat.isDirectory()){
var thisModule = require(path.resolve(path.join('.',nconf.get('script_directory'),fileName)));
thisModule.main(client);
if(thisModule.description){
visible_modules.push(fileName+" "+thisModule.description);
console.log("found module "+fileName+" ("+thisModule.description+")");
}
else{
console.log("found module without description "+fileName);
}
}
});
});
});
client.addListener('message#', function(nick,to,text,message) {
if(text === '!help'){
client.say(to,visible_modules.length+" modules visible:");
visible_modules.forEach((m,i) => {
setTimeout(() =>{
client.say(to," * "+m);
},600*i);
});
}
});
client.addListener('error', function(message) {
console.log('error: ', message);
});