-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
127 lines (108 loc) · 2.87 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
// @flow
require('dotenv').config();
import { nanoid } from 'nanoid';
import process from 'process';
import tmi from 'tmi.js';
import { App as WebSocketServer } from 'uWebSockets.js';
import type { Emote } from './types';
import {
getChannelBTTVEmotes,
getChannelFFZEmotes,
getGlobalBTTVEmotes
} from './util';
type UserStateEmotes = {
[string]: string[]
};
const validEmotes: Map<string, Emote> = new Map();
function parseEmotes(message: string, twitchEmotes: UserStateEmotes): Emote[] {
if (!twitchEmotes) {
twitchEmotes = {};
}
Object.keys(twitchEmotes).forEach((twitchEmoteID) => {
for (const range of twitchEmotes[twitchEmoteID]) {
const r = range.split('-');
const start = parseInt(r[0], 10);
const end = parseInt(r[1], 10) + 1;
validEmotes.set(message.substring(start, end), {
id: twitchEmoteID,
name: message.substring(start, end),
type: 'twitch',
size: {
width: 28,
height: 28
},
isGIF: false
});
}
});
const words = message.split(' ');
const emotes = [];
for (const word of words) {
// See <https://github.com/facebook/flow/issues/2751> for why
// `Map#has(word)` cannot be used here.
const validEmote = validEmotes.get(word);
if (validEmote) {
emotes.push(validEmote);
}
}
return emotes;
}
const tmiOptions = {
options: {
debug: process.env.NODE_ENV !== 'production'
},
connection: {
reconnect: true
},
channels: [`#${process.env.TWITCH_CHANNEL_NAME}`]
};
async function init() {
const globalBTTVEmotes = await getGlobalBTTVEmotes();
for (const emote of globalBTTVEmotes) {
validEmotes.set(emote.name, emote);
}
const { twitchId, emotes: channelFFZEmotes } = await getChannelFFZEmotes(
process.env.TWITCH_CHANNEL_NAME
);
for (const emote of channelFFZEmotes) {
validEmotes.set(emote.name, emote);
}
const channelBTTVEmotes = await getChannelBTTVEmotes(twitchId);
for (const emote of channelBTTVEmotes) {
validEmotes.set(emote.name, emote);
}
const sockets: { [id: string]: WebSocket } = {};
WebSocketServer()
.ws('/*', {
close: (socket) => {
delete sockets[socket.id];
},
open: (socket) => {
socket.id = nanoid();
sockets[socket.id] = socket;
}
})
.listen(3000, (listenSocket) => {
if (listenSocket) {
console.log('listening on port 3000');
}
});
const tmiClient = new tmi.Client(tmiOptions);
tmiClient.on('chat', (channel, userState, message, self) => {
if (self) {
return;
}
const emotes = parseEmotes(message, userState.emotes);
if (emotes.length) {
for (const socket in sockets) {
try {
sockets[socket].send(JSON.stringify(emotes));
} catch (_error) {
// ignore error
}
}
}
});
tmiClient.connect();
}
init();