/
follow-reminder.ts
119 lines (87 loc) Β· 4.16 KB
/
follow-reminder.ts
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
/*
This file is part of botten-nappet -- a Twitch bot and streaming tool.
<https://joelpurra.com/projects/botten-nappet/>
Copyright (c) 2018 Joel Purra <https://joelpurra.com/>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {
assert,
} from "check-types";
import PinoLogger from "../../../util/pino-logger";
import IIRCConnection from "../iirc-connection";
import IParsedMessage from "../iparsed-message";
import IrcManager from "../irc-manager";
export default class FollowReminderIrcHandler extends IrcManager {
private reminderMessages: string[];
private reminderIntervalMilliseconds: number;
private reminderIntervalId: (number | NodeJS.Timer | null);
constructor(logger: PinoLogger, connection: IIRCConnection) {
super(logger, connection);
assert.hasLength(arguments, 2);
assert.equal(typeof logger, "object");
assert.equal(typeof connection, "object");
this.logger = logger.child("FollowReminderIrcHandler");
this.reminderIntervalId = null;
this.reminderIntervalMilliseconds = 15 * 60 * 1000;
/* tslint:disable:max-line-length */
this.reminderMessages = [
"Remember to follow to catch the next live stream π",
"Enjoying the stream? Hit that follow button! π",
"I know not everyone wants to be a follower, but keep in mind that followers receive super-handy notifications when a live stream starts π",
"Want to ask a question but haven't prepared it yet? Follow and then ask it the next live stream π",
// NOTE: per-restart calculation of the random value. Re-calculate per reminder?
`Did you know followers are up to ${(50 + (Math.random() * 50)).toString().substring(0, 5)}% more likely to not miss the next live stream? π`,
];
/* tslint:enable:max-line-length */
}
public async start() {
assert.hasLength(arguments, 0);
assert.equal(this.reminderIntervalId, null);
await super.start();
// TODO: use an observable interval?
this.reminderIntervalId = setInterval(() => this.remind(), this.reminderIntervalMilliseconds);
}
public async stop() {
assert.hasLength(arguments, 0);
assert.not.equal(this.reminderIntervalId, null);
clearInterval(this.reminderIntervalId as NodeJS.Timer);
this.reminderIntervalId = null;
return super.stop();
}
protected async dataHandler(data: IParsedMessage): Promise<void> {
assert.hasLength(arguments, 1);
assert.equal(typeof data, "object");
throw new Error("Unexpected call to dataHandler.");
}
protected async filter(data: IParsedMessage): Promise<boolean> {
assert.hasLength(arguments, 1);
assert.equal(typeof data, "object");
return false;
}
private getReminder() {
assert.hasLength(arguments, 0);
// TODO: get library for random integers.
const randomReminderMessageIndex = Math.floor(Math.random() * this.reminderMessages.length);
const randomReminderMessage = this.reminderMessages[randomReminderMessageIndex];
return randomReminderMessage;
}
private remind() {
assert.hasLength(arguments, 0);
this.logger.trace("Sending reminder", "remind");
// TODO: use a string templating system.
// TODO: configure message.
const reminder = this.getReminder();
const message = `PRIVMSG ${this.connection.channel} :${reminder}`;
// TODO: handle errors, re-reconnect, or shut down server?
this.connection.send(message);
}
}