-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtake5bot.js
216 lines (148 loc) · 5.04 KB
/
take5bot.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
'use strict';
var Botkit = require('botkit');
var slack = require('slack');
class Take5Bot {
constructor(config, controller) {
if (!config.token) {
throw new Error('Please provide a token');
}
if (!config.timeoutValue) {
config.timeoutValue = 15;
}
if (!config.name) {
config.name = 'take5bot';
}
this.shouldFetchUsers = typeof config.fetchUsers !== 'undefined'
? !!config.fetchUsers
: true;
this.token = config.token;
this.name = config.name;
this.messages = config.messages || ['Take 5'];
this.enableMessages = config.enableMessages || ['start'];
this.disableMessages = config.disableMessages || ['stop'];
this.enableMessages = this.enableMessages.map(m => m.toLowerCase());
this.disableMessages = this.disableMessages.map(m => m.toLowerCase());
this.enabledUsers = [];
this.userData = {};
this.enabled = {};
this.lastUpdated = {};
this.timeoutValue = config.timeoutValue * 60 * 1000;
this.controller = controller
? controller
: Botkit.slackbot({
debug: 1 // from 1 to 7
});
this.on('tick', this.onTick.bind(this));
this.on('direct_message', this.onDirectMessage.bind(this));
}
start () {
this.controller
.spawn({
token: this.token
})
.startRTM(this.onRTMStarted.bind(this));
}
onRTMStarted() {
console.log('RTM started');
}
on (messageType, handler) {
this.controller.on(messageType, handler);
}
onDirectMessage (bot, message) {
var userId = message.user;
var userData = {
bot: bot,
message: message,
id: userId,
user: null
};
this.userData[userId] = userData;
if (this.shouldFetchUsers) {
this.fetchUser(userId);
}
if (this.isDisableMessage(message.text)) {
this.handleDisableMessage(userData);
}
if (this.isEnableMessage(message.text)) {
this.handleEnableMessage(userData);
}
}
onTick() {
this.enabledUsers.forEach(userId => {
var userData = this.userData[userId];
if (this.shouldTake5(userId)) {
this.sendTake5Message(userData);
this.updateTake5(userId);
}
});
}
fetchUser(userId) {
slack.users.info({ token: this.token, user: userId }, (err, data) => {
if (err) {
console.error(err);
return;
}
this.userData[userId].user = data.user;
});
}
handleDisableMessage(userData) {
var userId = userData.id;
var message = userData.message;
var bot = userData.bot;
if (this.enabled[userId]) {
this.enabled[userId] = false;
this.enabledUsers.splice(this.enabledUsers.indexOf(userId), 1);
bot.reply(message, 'Disabled');
} else {
if (typeof this.enabled[userId] !== 'undefined') {
bot.reply(message, 'I\'m already disabled');
} else {
bot.reply(message, 'You haven\'t enabled me yet');
}
}
}
handleEnableMessage(userData) {
var userId = userData.id;
var message = userData.message;
var bot = userData.bot;
if (!this.enabled[userId]) {
this.enabled[userId] = true;
this.enabledUsers.push(userId);
this.updateTake5(userId);
bot.reply(message, 'Enabled');
} else {
bot.reply(message, 'I\'m already enabled');
}
}
sendTake5Message(userData) {
var userId = userData.id;
var bot = userData.bot;
var message = userData.message;
var user = userData.user;
if (this.enabled[userId]) {
bot.reply(message, this.getTake5Message(user));
}
}
getTake5Message(user) {
var message = this.messages[Math.floor(Math.random() * this.messages.length)];
return message;
}
shouldTake5(userId) {
if (typeof this.lastUpdated[userId] === 'undefined') {
return;
}
var now = (new Date()).getTime();
var lastUpdated = this.lastUpdated[userId].getTime();
return (now - lastUpdated) >= this.timeoutValue;
}
updateTake5(userId) {
this.lastUpdated[userId] = new Date();
}
isEnableMessage(text) {
return this.enableMessages.indexOf(text.toLowerCase()) !== -1;
}
isDisableMessage(text) {
return this.disableMessages.indexOf(text.toLowerCase()) !== -1;
}
}
module.exports = Take5Bot;