/
manager-main.ts
360 lines (300 loc) · 13.9 KB
/
manager-main.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
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
/*
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 Bluebird from "bluebird";
import {
assert,
} from "check-types";
import path from "path";
import pkgDir from "pkg-dir";
import http from "http";
import Koa from "koa";
import koaStatic from "koa-static";
import SocketIo from "socket.io";
import GracefulShutdownManager from "../../../shared/src/util/graceful-shutdown-manager";
import PinoLogger from "../../../shared/src/util/pino-logger";
import Config from "../config/config";
import MessageQueuePublisher from "../../../shared/src/message-queue/publisher";
import ITwitchIncomingIrcCommand from "../../../backend/src/twitch/irc/command/iincoming-irc-command";
/* tslint:disable max-line-length */
import MessageQueueSingleItemJsonTopicsSubscriber from "../../../shared/src/message-queue/single-item-topics-subscriber";
/* tslint:enable max-line-length */
/* tslint:disable max-line-length */
import IIncomingCheeringWithCheermotesEvent from "../../../backend/src/twitch/polling/event/iincoming-cheering-with-cheermotes-event";
/* tslint:enable max-line-length */
import IIncomingFollowingEvent from "../../../backend/src/twitch/polling/event/iincoming-following-event";
import IIncomingSubscriptionEvent from "../../../backend/src/twitch/polling/event/iincoming-subscription-event";
import IIncomingSearchResultEvent from "../../../backend/vidy/command/iincoming-search-result-event";
import managedMain from "./managed-main";
import { isValidColor } from "../../shared/colors";
export default async function managerMain(
config: Config,
mainLogger: PinoLogger,
rootLogger: PinoLogger,
gracefulShutdownManager: GracefulShutdownManager,
messageQueuePublisher: MessageQueuePublisher,
): Promise<void> {
const app = new Koa();
app.on("error", (err, ctx) => {
// TODO: shut down server.
mainLogger.error(err, ctx, "server error");
});
const projectRootDirectoryPath = await pkgDir(__dirname);
// TODO: better null handling.
assert.nonEmptyString(projectRootDirectoryPath!);
const staticPublicRootDirectoryPath = path.join(projectRootDirectoryPath!, config.staticPublicRootDirectory);
app.use(koaStatic(staticPublicRootDirectoryPath));
const server = http.createServer(app.callback());
const io = SocketIo(server);
// TODO: configurable.
const topicsStringSeparator = ":";
const splitTopics = (topicsString: string): string[] => topicsString.split(topicsStringSeparator);
const twitchMessageQueueSingleItemJsonTopicsSubscriberForITwitchIncomingIrcCommand =
new MessageQueueSingleItemJsonTopicsSubscriber<ITwitchIncomingIrcCommand>(
mainLogger,
config.zmqAddress,
// TODO: no backend events.
...splitTopics("external:backend:twitch:incoming:irc:command"),
);
await twitchMessageQueueSingleItemJsonTopicsSubscriberForITwitchIncomingIrcCommand.connect();
const twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingFollowingEvent =
new MessageQueueSingleItemJsonTopicsSubscriber<IIncomingFollowingEvent>(
mainLogger,
config.zmqAddress,
...splitTopics(config.topicTwitchIncomingFollowingEvent),
);
await twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingFollowingEvent.connect();
const twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingCheeringWithCheermotesEvent =
new MessageQueueSingleItemJsonTopicsSubscriber<IIncomingCheeringWithCheermotesEvent>(
mainLogger,
config.zmqAddress,
...splitTopics(config.topicTwitchIncomingCheeringWithCheermotesEvent),
);
await twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingCheeringWithCheermotesEvent.connect();
const twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingSubscriptionEvent =
new MessageQueueSingleItemJsonTopicsSubscriber<IIncomingSubscriptionEvent>(
mainLogger,
config.zmqAddress,
...splitTopics(config.topicTwitchIncomingSubscriptionEvent),
);
await twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingSubscriptionEvent.connect();
const vidyMessageQueueSingleItemJsonTopicsSubscriberForIIncomingSearchResultEvent =
new MessageQueueSingleItemJsonTopicsSubscriber<IIncomingSearchResultEvent>(
mainLogger,
config.zmqAddress,
...splitTopics(config.topicVidyIncomingSearchResultEvent),
);
await vidyMessageQueueSingleItemJsonTopicsSubscriberForIIncomingSearchResultEvent.connect();
twitchMessageQueueSingleItemJsonTopicsSubscriberForITwitchIncomingIrcCommand.dataObservable.forEach((data) => {
let msg = null;
const exampleCheermote1 = "https://d3aqoihi2n8ty8.cloudfront.net/actions/cheer/dark/animated/1/4.gif";
const exampleCheermote100 = "https://d3aqoihi2n8ty8.cloudfront.net/actions/cheer/dark/animated/100/4.gif";
switch (data.command) {
case "PRIVMSG":
if (data.message === null) {
return;
}
const commandPrefix = "!";
const messageParts = data.message.trim().split(/\s+/);
if (messageParts[0].startsWith(commandPrefix)) {
const messageCommand = messageParts[0].slice(commandPrefix.length);
const commandArguments = messageParts.slice(1);
switch (messageCommand) {
// TODO: check arguments.
case "animate":
let color = commandArguments[0];
if (!isValidColor(color)) {
color = "rgba(255,255,255,0.5)";
}
msg = {
data: {
color,
timestamp: data.timestamp,
username: data.username,
},
event: messageCommand,
};
break;
case "cowbell":
case "say":
// TODO: remove after testing.
case "following":
msg = {
data: {
args: commandArguments,
timestamp: data.timestamp,
username: data.username,
},
event: messageCommand,
};
break;
case "subscription":
msg = {
data: {
args: commandArguments,
// TODO: use random library.
months: Math.floor(Math.random() * 3),
timestamp: data.timestamp,
username: data.username,
},
event: messageCommand,
};
break;
case "cheering-with-cheermotes":
msg = {
data: {
args: commandArguments,
// TODO: use random library.
bits: Math.floor(Math.random() * 500),
cheermotes: [
{
cheerToken: {
amount: 1,
prefix: "cheer",
},
url: exampleCheermote1,
},
{
cheerToken: {
amount: 100,
prefix: "cheer",
},
url: exampleCheermote100,
},
],
message: "cheer100 cheer1 cheer1 My custom cheering message 😀 cheer100",
timestamp: data.timestamp,
// TODO: use random library.
total: Math.floor(Math.random() * 50000),
username: data.username,
},
event: messageCommand,
};
break;
}
} else {
const isSubscriber = (data.tags && data.tags.subscriber === "1") || false;
msg = {
data: {
isSubscriber,
message: data.message,
timestamp: data.timestamp,
username: data.username,
},
event: "chat-message",
};
}
break;
}
if (msg === null) {
return;
}
io.send(msg);
});
twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingFollowingEvent.dataObservable.forEach((data) => {
const msg = {
data: {
timestamp: data.timestamp,
username: data.triggerer.name,
},
event: "following",
};
io.send(msg);
});
twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingCheeringWithCheermotesEvent
.dataObservable.forEach((data) => {
const msg = {
data: {
// args: commandArguments,
bits: data.bits,
cheermotes: data.cheermotes,
message: data.message,
timestamp: data.timestamp,
total: data.total,
username: data.triggerer.name,
},
event: "cheering-with-cheermote",
};
io.send(msg);
});
twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingSubscriptionEvent.dataObservable.forEach((data) => {
const msg = {
data: {
timestamp: data.timestamp,
username: data.triggerer.name,
},
event: "subscription",
};
io.send(msg);
});
vidyMessageQueueSingleItemJsonTopicsSubscriberForIIncomingSearchResultEvent.dataObservable.forEach((data) => {
const rnd = Math.floor(Math.random() * data.clips.results.length);
const randomResult = data.clips.results[rnd];
const randomVideoUrl = randomResult.files.landscapeVideo240.url;
const msg = {
data: {
// TODO: don't assume too much.
videoUrl: randomVideoUrl,
// timestamp: data.timestamp,
// username: data.triggerer.name,
},
event: "vidy",
};
io.send(msg);
});
io.on("connection", (clientSocket) => {
// mainLogger.trace(clientSocket, "incoming connection");
mainLogger.trace(clientSocket.rooms, "incoming connection");
// clientSocket.on("HAI", (data) => {
// mainLogger.trace(data, "HAI");
// });
clientSocket.on("message", () => {
mainLogger.trace("message");
});
clientSocket.on("disconnect", () => {
mainLogger.trace("disconnect");
});
});
mainLogger.info("Managed.");
const shutdown = async (incomingError?: Error) => {
await vidyMessageQueueSingleItemJsonTopicsSubscriberForIIncomingSearchResultEvent.disconnect();
await twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingSubscriptionEvent.disconnect();
await twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingCheeringWithCheermotesEvent.disconnect();
await twitchMessageQueueSingleItemJsonTopicsSubscriberForIIncomingFollowingEvent.disconnect();
await twitchMessageQueueSingleItemJsonTopicsSubscriberForITwitchIncomingIrcCommand.disconnect();
await Bluebird.promisify(server.close, {
context: server,
})();
if (incomingError) {
mainLogger.error(incomingError, "Unmanaged.");
throw incomingError;
}
mainLogger.info("Unmanaged.");
return undefined;
};
try {
server.listen(config.port);
await managedMain(
config,
mainLogger,
rootLogger,
gracefulShutdownManager,
messageQueuePublisher,
);
await shutdown();
} catch (error) {
shutdown(error);
}
}