/
index.js
138 lines (117 loc) · 5.61 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
133
134
135
136
137
138
/*
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 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");
// TODO: better token/config handling.
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 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 shutdownManager = new ShutdownManager();
const twitchPubSubConnection = new TwitchPubSubConnection(twitchPubSubWebSocketUri);
const twitchPubSubManager = new TwitchPubSubManager(twitchPubSubConnection, twitchUserId, twitchUserAccessToken);
const twitchIrcConnection = new TwitchIrcConnection(twitchIrcWebSocketUri, twitchChannelName, twitchUserName, twitchUserAccessToken);
const twitchIrcManager = new TwitchIrcManager(twitchIrcConnection);
const twitchIrcPingManager = new TwitchIrcPingManager(twitchIrcConnection);
Promise.resolve()
.then(() => shutdownManager.start())
.then(() => Promise.all([
twitchPubSubConnection.connect(),
twitchIrcConnection.connect(),
]))
.then(() => {
/* eslint-disable no-console */
console.log("Connected.");
/* eslint-enable no-console */
const disconnect = (incomingError) => Promise.all([
twitchPubSubConnection.disconnect(),
twitchIrcConnection.disconnect(),
])
.then(() => {
if (incomingError) {
/* eslint-disable no-console */
console.error("Disconnected.", incomingError);
/* eslint-enable no-console */
} else {
/* eslint-disable no-console */
console.log("Disconnected.");
/* eslint-enable no-console */
}
return undefined;
});
return Promise.resolve()
.then(() => Promise.all([
twitchPubSubManager.start(),
twitchIrcManager.start(),
twitchIrcPingManager.start(),
]))
.then(() => {
/* eslint-disable no-console */
console.log(`Started listening to events for ${twitchUserName} (${twitchUserId}).`);
/* eslint-enable no-console */
const stop = (incomingError) => Promise.all([
twitchPubSubManager.stop(),
twitchIrcManager.stop(),
twitchIrcPingManager.stop(),
])
.then(() => {
if (incomingError) {
/* eslint-disable no-console */
console.error("Stopped.", incomingError);
/* eslint-enable no-console */
} else {
/* eslint-disable no-console */
console.log("Stopped.");
/* eslint-enable no-console */
}
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) => {
/* eslint-disable no-console */
console.log("Error.", error);
/* eslint-enable no-console */
process.exitCode = 1;
});