-
Notifications
You must be signed in to change notification settings - Fork 5
/
bot.js
149 lines (135 loc) · 3.77 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
'use strict';
var config = require('./setup/config.json');
var irc = require('irc');
var hotload = require('hotload');
var commands = hotload('./commands');
var responses = hotload('./responses');
var reminders = hotload('./modules/reminder');
var helper = hotload('./helper');
var mongoCon = require('./setup/mongoConnection').connect(config.db, config.dbName);
var Q = require('q');
var express = require('express');
var app = express();
var setup = require('./setup/express')(app, config.secret);
var server = require('http').createServer(app);
var routes = require('./routes')(app);
var twitterStreams = require('./modules/twitterStreams/');
var https = require('https');
server.listen(config.httpPort);
var bot = new irc.Client(config.server, config.name, config);
var userList = { '#general': {},
'#dottest': {}
}
var previousMessage = [""];
var previousCommand = [""];
var count = 1;
var quiet = {};
var timeout = null;
function reminder(){
reminders.actions.check(bot);
}
setInterval(reminder, 30000);
function vocal(channel){
quiet[channel] = false;
bot.say(channel, '...');
}
bot.on('join', function(channel, who) {
var text = ['Hey, ', 'Howdy, ', 'Hi, ', 'Greetings, '];
if (who !== config.name){
//bot.say(channel, helper.choose(text) + who );
} else {
bot.say(channel, '...');
}
});
bot.on('message', function(from, to, text, message) {
console.log(message);
var sendTo = from;
if (to.indexOf('#') > -1) {
sendTo = to;
}
var split = text.split(' ');
var resp = null;
if (split[0].charAt(0) === '.'){
var command = split[0].split('.')[1];
if (command === 'quiet'){
quiet[sendTo] = true;
timeout = setTimeout(vocal, 600000, sendTo);
resp = "Shutting up.";
} else if(command === 'vocal' && timeout){
quiet[sendTo] = false;
clearTimeout(timeout);
} else {
try{
resp = commands[command](bot, from, to, text, split, sendTo, userList);
previousCommand = [command];
} catch(err){
if (!quiet[sendTo]){
resp = responses.parse(bot, from, split, sendTo);
}
}
}
} else if (!quiet[sendTo]){
resp = responses.parse(bot, from, split, sendTo);
}
if(resp){
bot.say(sendTo, resp);
}
});
bot.on('response', function(resp, sendTo) {
if(typeof resp === 'string') {
resp = [resp];
}
if (previousMessage[0] !== resp[0] || previousCommand[0] === "ud" || previousCommand[0] === "list" || previousCommand[0] === "pool"){
previousCommand[0] = "";
previousMessage[0] = resp[0];
resp.forEach(function(string) {
bot.say(sendTo, string);
});
};
});
bot.on('command', function(args) {
var resp = '';
var split = args.command.split(' ');
if (split[0].charAt(0) === '.'){
var command = split[0].split('.')[1];
resp = commands[command](bot, args.from, args.to, args.command, split, args.sendTo, userList);
};
if(resp){
bot.say(args.sendTo, resp);
}
});
bot.on('names', function(channel, nicks) {
userList[channel] = nicks;
console.log(userList);
twitterStreams.query(bot);
});
var getUserList = function() {
var options = {
host: "slack.com",
path: "/api/users.list?token=" + config.token
};
var response = "";
var callback = function(res) {
res.on("error", function(err) {
console.log(err);
});
res.on("data", function(chunk){
response += chunk;
});
res.on("end", function(){
console.log(response)
var json = JSON.parse(response);
userList={'#general':{}, '#dottest':{}};
json.members.forEach(function(member){
userList['#general'][member.name] = 1;
userList['#dottest'][member.name] = 1;
});
console.log(userList);
});
}
https.request(options, callback).end();
};
getUserList();
bot.on('error', function(message) {
console.log('error: ', message);
});