/
index.js
132 lines (110 loc) · 5.47 KB
/
index.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
/*
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 PinoLogger from "./src/util/pino-logger";
import ShutdownManager from "./src/util/shutdown-manager";
import TwitchPubSubConnection from "./src/twitch/pubsub/pubsub-connection";
import TwitchPubSubManager from "./src/twitch/pubsub/pubsub-manager";
import TwitchIrcConnection from "./src/twitch/irc/irc-connection";
import TwitchIrcManager from "./src/twitch/irc/irc-manager";
import TwitchIrcPingManager from "./src/twitch/irc/commands/ping";
const assert = require("assert");
const Promise = require("bluebird");
const BOTTEN_NAPPET_DEFAULT_LOGGING_LEVEL = "error";
// TODO: better token/config handling.
const loggingLevel = process.env.BOTTEN_NAPPET_LOGGING_LEVEL || BOTTEN_NAPPET_DEFAULT_LOGGING_LEVEL;
const twitchAppAccessToken = process.env.TWITCH_APP_ACCESS_TOKEN;
const twitchUserAccessToken = process.env.TWITCH_USER_ACCESS_TOKEN;
const twitchUserName = process.env.TWITCH_USER_NAME;
// NOTE: use a twitch api lookup to get the id from twitchUserName.
const twitchUserId = parseInt(process.env.TWITCH_USER_ID, 10);
// TODO: simplify validation and validation error messages.
assert.strictEqual(typeof loggingLevel, "string", "BOTTEN_NAPPET_LOGGING_LEVEL");
assert(loggingLevel.length > 0, "BOTTEN_NAPPET_LOGGING_LEVEL");
assert.strictEqual(typeof twitchAppAccessToken, "string", "TWITCH_APP_ACCESS_TOKEN");
assert(twitchAppAccessToken.length > 0, "TWITCH_APP_ACCESS_TOKEN");
assert.strictEqual(typeof twitchUserAccessToken, "string", "TWITCH_USER_ACCESS_TOKEN");
assert(twitchUserAccessToken.length > 0, "TWITCH_USER_ACCESS_TOKEN");
assert.strictEqual(typeof twitchUserName, "string", "TWITCH_USER_NAME");
assert(twitchUserName.length > 0, "TWITCH_USER_NAME");
assert(!isNaN(twitchUserId), "TWITCH_USER_ID");
assert(twitchUserId > 0, "TWITCH_USER_ID");
const twitchPubSubWebSocketUri = "wss://pubsub-edge.twitch.tv/";
const twitchIrcWebSocketUri = "wss://irc-ws.chat.twitch.tv:443/";
// NOTE: assuming that the user only joins their own channel, with a "#" prefix.
const twitchChannelName = `#${twitchUserName}`;
const applicationName = "botten-nappet";
const rootLogger = new PinoLogger(applicationName, loggingLevel);
const shutdownManager = new ShutdownManager(rootLogger);
const twitchPubSubConnection = new TwitchPubSubConnection(rootLogger, twitchPubSubWebSocketUri);
const twitchPubSubManager = new TwitchPubSubManager(rootLogger, twitchPubSubConnection, twitchUserId, twitchUserAccessToken);
const twitchIrcConnection = new TwitchIrcConnection(rootLogger, twitchIrcWebSocketUri, twitchChannelName, twitchUserName, twitchUserAccessToken);
const twitchIrcManager = new TwitchIrcManager(rootLogger, twitchIrcConnection);
const twitchIrcPingManager = new TwitchIrcPingManager(rootLogger, twitchIrcConnection);
Promise.resolve()
.then(() => shutdownManager.start())
.then(() => Promise.all([
twitchPubSubConnection.connect(),
twitchIrcConnection.connect(),
]))
.then(() => {
rootLogger.info("Connected.");
const disconnect = (incomingError) => Promise.all([
twitchPubSubConnection.disconnect(),
twitchIrcConnection.disconnect(),
])
.then(() => {
if (incomingError) {
rootLogger.error("Disconnected.", incomingError);
} else {
rootLogger.info("Disconnected.");
}
return undefined;
});
return Promise.resolve()
.then(() => Promise.all([
twitchPubSubManager.start(),
twitchIrcManager.start(),
twitchIrcPingManager.start(),
]))
.then(() => {
rootLogger.info(`Started listening to events for ${twitchUserName} (${twitchUserId}).`);
const stop = (incomingError) => Promise.all([
twitchPubSubManager.stop(),
twitchIrcManager.stop(),
twitchIrcPingManager.stop(),
])
.then(() => {
if (incomingError) {
rootLogger.error("Stopped.", incomingError);
} else {
rootLogger.info("Stopped.");
}
return undefined;
});
return shutdownManager.waitForShutdownSignal()
.then(() => stop(), (error) => stop(error));
})
.then(() => disconnect(), (error) => disconnect(error));
})
.then(() => shutdownManager.stop())
.then(() => {
process.exitCode = 0;
return undefined;
})
.catch((error) => {
rootLogger.info("Error.", error);
process.exitCode = 1;
});