forked from tmijs/tmi.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.js
492 lines (416 loc) · 18.5 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
var _ = require("./utils");
// Enable followers-only mode on a channel..
function followersonly(channel, minutes) {
channel = _.channel(channel);
minutes = _.get(minutes, 30);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), channel, `/followers ${minutes}`, (resolve, reject) => {
// Received _promiseFollowers event, resolve or reject..
this.once("_promiseFollowers", (err) => {
if (!err) { resolve([channel, ~~minutes]); }
else { reject(err); }
});
});
}
// Disable followers-only mode on a channel..
function followersonlyoff(channel) {
channel = _.channel(channel);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), channel, "/followersoff", (resolve, reject) => {
// Received _promiseFollowersoff event, resolve or reject..
this.once("_promiseFollowersoff", (err) => {
if (!err) { resolve([channel]); }
else { reject(err); }
});
});
}
// Leave a channel..
function part(channel) {
channel = _.channel(channel);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), null, `PART ${channel}`, (resolve, reject) => {
// Received _promisePart event, resolve or reject..
this.once("_promisePart", (err) => {
if (!err) { resolve([channel]); }
else { reject(err); }
});
});
}
// Enable R9KBeta mode on a channel..
function r9kbeta(channel) {
channel = _.channel(channel);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), channel, "/r9kbeta", (resolve, reject) => {
// Received _promiseR9kbeta event, resolve or reject..
this.once("_promiseR9kbeta", (err) => {
if (!err) { resolve([channel]); }
else { reject(err); }
});
});
}
// Disable R9KBeta mode on a channel..
function r9kbetaoff(channel) {
channel = _.channel(channel);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), channel, "/r9kbetaoff", (resolve, reject) => {
// Received _promiseR9kbetaoff event, resolve or reject..
this.once("_promiseR9kbetaoff", (err) => {
if (!err) { resolve([channel]); }
else { reject(err); }
});
});
}
// Enable slow mode on a channel..
function slow(channel, seconds) {
channel = _.channel(channel);
seconds = _.get(seconds, 300);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), channel, `/slow ${seconds}`, (resolve, reject) => {
// Received _promiseSlow event, resolve or reject..
this.once("_promiseSlow", (err) => {
if (!err) { resolve([channel, ~~seconds]); }
else { reject(err); }
});
});
}
// Disable slow mode on a channel..
function slowoff(channel) {
channel = _.channel(channel);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), channel, "/slowoff", (resolve, reject) => {
// Received _promiseSlowoff event, resolve or reject..
this.once("_promiseSlowoff", (err) => {
if (!err) { resolve([channel]); }
else { reject(err); }
});
});
}
module.exports = {
// Send action message (/me <message>) on a channel..
action: function action(channel, message) {
channel = _.channel(channel);
message = `\u0001ACTION ${message}\u0001`;
// Send the command to the server and race the Promise against a delay..
return this._sendMessage(this._getPromiseDelay(), channel, message, (resolve, reject) => {
// At this time, there is no possible way to detect if a message has been sent has been eaten
// by the server, so we can only resolve the Promise.
resolve([channel, message]);
});
},
// Ban username on channel..
ban: function ban(channel, username, reason) {
channel = _.channel(channel);
username = _.username(username);
reason = _.get(reason, "");
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), channel, `/ban ${username} ${reason}`, (resolve, reject) => {
// Received _promiseBan event, resolve or reject..
this.once("_promiseBan", (err) => {
if (!err) { resolve([channel, username, reason]); }
else { reject(err); }
});
});
},
// Clear all messages on a channel..
clear: function clear(channel) {
channel = _.channel(channel);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), channel, "/clear", (resolve, reject) => {
// Received _promiseClear event, resolve or reject..
this.once("_promiseClear", (err) => {
if (!err) { resolve([channel]); }
else { reject(err); }
});
});
},
// Change the color of your username..
color: function color(channel, newColor) {
newColor = _.get(newColor, channel);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), "#tmijs", `/color ${newColor}`, (resolve, reject) => {
// Received _promiseColor event, resolve or reject..
this.once("_promiseColor", (err) => {
if (!err) { resolve([newColor]); }
else { reject(err); }
});
});
},
// Run commercial on a channel for X seconds..
commercial: function commercial(channel, seconds) {
channel = _.channel(channel);
seconds = _.get(seconds, 30);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), channel, `/commercial ${seconds}`, (resolve, reject) => {
// Received _promiseCommercial event, resolve or reject..
this.once("_promiseCommercial", (err) => {
if (!err) { resolve([channel, ~~seconds]); }
else { reject(err); }
});
});
},
// Enable emote-only mode on a channel..
emoteonly: function emoteonly(channel) {
channel = _.channel(channel);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), channel, "/emoteonly", (resolve, reject) => {
// Received _promiseEmoteonly event, resolve or reject..
this.once("_promiseEmoteonly", (err) => {
if (!err) { resolve([channel]); }
else { reject(err); }
});
});
},
// Disable emote-only mode on a channel..
emoteonlyoff: function emoteonlyoff(channel) {
channel = _.channel(channel);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), channel, "/emoteonlyoff", (resolve, reject) => {
// Received _promiseEmoteonlyoff event, resolve or reject..
this.once("_promiseEmoteonlyoff", (err) => {
if (!err) { resolve([channel]); }
else { reject(err); }
});
});
},
// Enable followers-only mode on a channel..
followersonly: followersonly,
// Alias for followersonly()..
followersmode: followersonly,
// Disable followers-only mode on a channel..
followersonlyoff: followersonlyoff,
// Alias for followersonlyoff()..
followersmodeoff: followersonlyoff,
// Host a channel..
host: function host(channel, target) {
channel = _.channel(channel);
target = _.username(target);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(2000, channel, `/host ${target}`, (resolve, reject) => {
// Received _promiseHost event, resolve or reject..
this.once("_promiseHost", (err, remaining) => {
if (!err) { resolve([channel, target, ~~remaining]); }
else { reject(err); }
});
});
},
// Join a channel..
join: function join(channel) {
channel = _.channel(channel);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), null, `JOIN ${channel}`, (resolve, reject) => {
// Received _promiseJoin event, resolve or reject..
this.once("_promiseJoin", (err) => {
if (!err) { resolve([channel]); }
else { reject(err); }
});
});
},
// Mod username on channel..
mod: function mod(channel, username) {
channel = _.channel(channel);
username = _.username(username);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), channel, `/mod ${username}`, (resolve, reject) => {
// Received _promiseMod event, resolve or reject..
this.once("_promiseMod", (err) => {
if (!err) { resolve([channel, username]); }
else { reject(err); }
});
});
},
// Get list of mods on a channel..
mods: function mods(channel) {
channel = _.channel(channel);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), channel, "/mods", (resolve, reject) => {
// Received _promiseMods event, resolve or reject..
this.once("_promiseMods", (err, mods) => {
if (!err) {
// Update the internal list of moderators..
mods.forEach((username) => {
if (!this.moderators[channel]) { this.moderators[channel] = []; }
if (this.moderators[channel].indexOf(username) < 0) { this.moderators[channel].push(username); }
});
resolve(mods);
} else { reject(err); }
});
});
},
// Leave a channel..
part: part,
// Alias for part()..
leave: part,
// Send a ping to the server..
ping: function ping() {
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), null, "PING", (resolve, reject) => {
// Update the internal ping timeout check interval..
this.latency = new Date();
this.pingTimeout = setTimeout(() => {
if (this.ws !== null) {
this.wasCloseCalled = false;
this.log.error("Ping timeout.");
this.ws.close();
clearInterval(this.pingLoop);
clearTimeout(this.pingTimeout);
}
}, _.get(this.opts.connection.timeout, 9999));
// Received _promisePing event, resolve or reject..
this.once("_promisePing", (latency) => { resolve([parseFloat(latency)]); });
});
},
// Enable R9KBeta mode on a channel..
r9kbeta: r9kbeta,
// Alias for r9kbeta()..
r9kmode: r9kbeta,
// Disable R9KBeta mode on a channel..
r9kbetaoff: r9kbetaoff,
// Alias for r9kbetaoff()..
r9kmodeoff: r9kbetaoff,
// Send a raw message to the server..
raw: function raw(message) {
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), null, message, (resolve, reject) => {
resolve([message]);
});
},
// Send a message on a channel..
say: function say(channel, message) {
channel = _.channel(channel);
if ((message.startsWith(".") && !message.startsWith("..")) || message.startsWith("/") || message.startsWith("\\")) {
// Check if the message is an action message..
if (message.substr(1, 3) === "me ") {
return this.action(channel, message.substr(4));
}
else {
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), channel, message, (resolve, reject) => {
// At this time, there is no possible way to detect if a message has been sent has been eaten
// by the server, so we can only resolve the Promise.
resolve([channel, message]);
});
}
}
// Send the command to the server and race the Promise against a delay..
return this._sendMessage(this._getPromiseDelay(), channel, message, (resolve, reject) => {
// At this time, there is no possible way to detect if a message has been sent has been eaten
// by the server, so we can only resolve the Promise.
resolve([channel, message]);
});
},
// Enable slow mode on a channel..
slow: slow,
// Alias for slow()..
slowmode: slow,
// Disable slow mode on a channel..
slowoff: slowoff,
// Alias for slowoff()..
slowmodeoff: slowoff,
// Enable subscribers mode on a channel..
subscribers: function subscribers(channel) {
channel = _.channel(channel);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), channel, "/subscribers", (resolve, reject) => {
// Received _promiseSubscribers event, resolve or reject..
this.once("_promiseSubscribers", (err) => {
if (!err) { resolve([channel]); }
else { reject(err); }
});
});
},
// Disable subscribers mode on a channel..
subscribersoff: function subscribersoff(channel) {
channel = _.channel(channel);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), channel, "/subscribersoff", (resolve, reject) => {
// Received _promiseSubscribersoff event, resolve or reject..
this.once("_promiseSubscribersoff", (err) => {
if (!err) { resolve([channel]); }
else { reject(err); }
});
});
},
// Timeout username on channel for X seconds..
timeout: function timeout(channel, username, seconds, reason) {
channel = _.channel(channel);
username = _.username(username);
if (!_.isNull(seconds) && !_.isInteger(seconds)) {
reason = seconds;
seconds = 300;
}
seconds = _.get(seconds, 300);
reason = _.get(reason, "");
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), channel, `/timeout ${username} ${seconds} ${reason}`, (resolve, reject) => {
// Received _promiseTimeout event, resolve or reject..
this.once("_promiseTimeout", (err) => {
if (!err) { resolve([channel, username, ~~seconds, reason]); }
else { reject(err); }
});
});
},
// Unban username on channel..
unban: function unban(channel, username) {
channel = _.channel(channel);
username = _.username(username);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), channel, `/unban ${username}`, (resolve, reject) => {
// Received _promiseUnban event, resolve or reject..
this.once("_promiseUnban", (err) => {
if (!err) { resolve([channel, username]); }
else { reject(err); }
});
});
},
// End the current hosting..
unhost: function unhost(channel) {
channel = _.channel(channel);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(2000, channel, "/unhost", (resolve, reject) => {
// Received _promiseUnhost event, resolve or reject..
this.once("_promiseUnhost", (err) => {
if (!err) { resolve([channel]); }
else { reject(err); }
});
});
},
// Unmod username on channel..
unmod: function unmod(channel, username) {
channel = _.channel(channel);
username = _.username(username);
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), channel, `/unmod ${username}`, (resolve, reject) => {
// Received _promiseUnmod event, resolve or reject..
this.once("_promiseUnmod", (err) => {
if (!err) { resolve([channel, username]); }
else { reject(err); }
});
});
},
// Send an whisper message to a user..
whisper: function whisper(username, message) {
username = _.username(username);
// The server will not send a whisper to the account that sent it.
if (username === this.getUsername()) {
return Promise.reject("Cannot send a whisper to the same account.");
}
// Send the command to the server and race the Promise against a delay..
return this._sendCommand(this._getPromiseDelay(), "#tmijs", `/w ${username} ${message}`, (resolve, reject) => {
var from = _.channel(username),
userstate = _.merge({
"message-type": "whisper",
"message-id": null,
"thread-id": null,
username: this.getUsername()
}, this.globaluserstate);
// Emit for both, whisper and message..
this.emits(["whisper", "message"], [
[from, userstate, message, true],
[from, userstate, message, true]
]);
// At this time, there is no possible way to detect if a message has been sent has been eaten
// by the server, so we can only resolve the Promise.
resolve([username, message]);
});
}
}