This repository has been archived by the owner on Aug 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
308 lines (288 loc) · 11 KB
/
extension.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
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
const fs = require('fs');
const net = require('net');
const process = require('process');
const api = require('@twurple/api');
const auth = require('@twurple/auth');
const chat = require('@twurple/chat');
const eventSub = require('@twurple/eventsub');
const aedes = require('aedes');
const express = require('express');
const iconv = require('iconv-lite');
const modernAsync = require('modern-async');
const autoShDelay = 5000;
const autoMessageInterval = 10*60000;
function displayUser(name, login, anonymous) {
if (anonymous) return '???';
return /[\p{ASCII}]+/u.test(name) ? name : login;
}
function pluralize(amount, singular, plural) {
return amount === 1 ? `${amount} ${singular}` : `${amount} ${plural}`;
}
/*
* Our Metro font only supports Latin-1 characters, thus some UTF-8 characters
* (i.e. U+2212 Minus Sign, which appears in some game names - thanks Twitch!)
* need to get coerced accordingly
*/
function latinize(text) {
const buffer = iconv.encode(text, 'iso-8859-1');
return iconv.decode(buffer, 'iso-8859-1');
}
module.exports = nodecg => {
const config = nodecg.bundleConfig;
const tokens = nodecg.Replicant('tokens', { defaultValue: {} });
const counters = nodecg.Replicant('counters', { defaultValue: {} });
const secretCount = nodecg.Replicant('secret-counters', { defaultValue: {} });
const follower = nodecg.Replicant('follower', { defaultValue: '' });
const subscriber = nodecg.Replicant('subscriber', { defaultValue: '' });
const cheer = nodecg.Replicant('cheer', { defaultValue: '' });
const mediaFiles = nodecg.Replicant('media-files', { defaultValue: [] });
const elite = nodecg.Replicant('elite', { defaultValue: {} });
Object.keys(config.secret).forEach(key => {
if (config.secret[key].counter && !Number.isInteger(secretCount.value[key])) {
secretCount.value[key] = 0;
}
});
fs.readdir('bundles/comboio-overlay/graphics/media', (err, files) => {
mediaFiles.value = files;
});
const userAuthProvider = new auth.RefreshingAuthProvider(
{
clientId: config.twitchApp.id,
clientSecret: config.twitchApp.secret,
onRefresh: data => tokens.value = data
},
tokens.value
);
const userApiClient = new api.ApiClient({ authProvider: userAuthProvider });
userApiClient.users.getFollows({ followedUser: config.channel.id, limit: 1 })
.then(response => {
nodecg.log.info('Initializing follow via Twitch Helix API');
follower.value = displayUser(response.data[0].userDisplayName, response.data[0].userName);
}).catch(err => {
nodecg.log.error('Error on calling Twitch Helix API:', err.body);
});
const webhook = express();
webhook.use(express.urlencoded());
webhook.use((req, res, next) => {
res.set('X-Clacks-Overhead', 'GNU Terry Pratchett');
next();
});
webhook.post('/ko-fi', (req, res) => {
const data = JSON.parse(req.body.data);
if (!data.is_public) {
data.from_name = 'anônimo';
}
data.amount = new Intl.NumberFormat('pt-BR', { style: 'currency', currency: data.currency }).format(data.amount);
nodecg.sendMessage('alert', {
tee: true,
user_name: data.from_name,
title: `Doação de ${data.amount} enviada para o Comboio`,
message: data.message
});
//donate.value = `${data.from_name} (${data.amount})`;
res.status(200).end();
});
webhook.post('/tipa', express.json(), (req, res) => {
if (req.get('X-Tipa-Webhook-Secret-Token') !== config.webhook.tipa) {
res.status(404).end();
return;
}
const tip = req.body.payload.tip;
const amount = new Intl.NumberFormat('pt-BR', { style: 'currency', currency: 'BRL' }).format(tip.amount);
nodecg.sendMessage('alert', {
tee: true,
user_name: tip.name,
title: `Pix de ${amount} enviado para o Comboio`,
message: tip.message
});
//donate.value = `${tip.name} (${amount})`;
res.status(200).end();
});
const appAuthProvider = new auth.ClientCredentialsAuthProvider(config.twitchApp.id, config.twitchApp.secret);
const appApiClient = new api.ApiClient({ authProvider: appAuthProvider });
const listener = new eventSub.EventSubMiddleware({
apiClient: appApiClient,
hostName: config.eventSub.hostname,
pathPrefix: '/twitch',
secret: config.eventSub.secret
});
listener.apply(webhook).then(() => {
webhook.listen(config.eventSub.port, async () => {
await listener.markAsReady();
nodecg.log.info('Twitch EventSub ready for follows and redemptions');
listener.subscribeToStreamOnlineEvents(config.channel.id, event => {
autoMessages.start();
});
listener.subscribeToStreamOfflineEvents(config.channel.id, event => {
autoMessages.stop();
});
listener.subscribeToChannelFollowEvents(config.channel.id, event => {
follower.value = displayUser(event.userDisplayName, event.userName);
nodecg.sendMessage('alert', {
tee: true,
user_name: follower.value,
title: 'Novo passageiro no Comboio'
});
});
listener.subscribeToChannelRedemptionAddEvents(config.channel.id, event => {
if (event.rewardTitle === config.tts.reward) {
nodecg.sendMessage('alert', { message: event.input });
} else if (config.rewards[event.rewardTitle]) {
const reward = config.rewards[event.rewardTitle];
if (reward.source) {
nodecg.sendMessage('play', reward.source);
}
if (reward.countdown) {
nodecg.sendMessage('countdown', [event.rewardTitle, reward]);
}
}
});
});
});
const chatClient = new chat.ChatClient({
channels: [config.channel.name],
authProvider: userAuthProvider
});
chatClient.connect();
let autoMessageIdx = 0;
const autoMessages = new modernAsync.Scheduler(async () => {
const [username, message] = config.sh[autoMessageIdx++ % config.sh.length];
chatClient.say(config.channel.name, `!sh ${username} - ${message} #ComboioRecomenda`);
sh(config.channel.name, username);
}, autoMessageInterval);
function handleCommand(channel, message, privmsg, name, command) {
if (command.alias) {
command = config.commands[command.alias];
}
if (command.enabled === false) {
return;
}
if (command.play) {
nodecg.sendMessage('play', command.play);
}
if (command.counter) {
// we assume the counter key only applies to secret commands
chatClient.say(channel, command.counter.replace('####', ++secretCount.value[name]));
}
if (command.input) {
chatClient.say(channel, command.input.replace('####', message.split(' ')[1]));
}
if (command.reply) {
chatClient.say(channel, command.reply, { replyTo: privmsg });
}
if (command.message) {
chatClient.say(channel, command.message);
}
if (command.thread) {
command.thread.forEach(message => {
chatClient.say(channel, message, { replyTo: privmsg });
});
}
}
async function sh(channel, username) {
const user = await userApiClient.users.getUserByName(username);
const userChannel = await userApiClient.channels.getChannelInfo(user);
nodecg.sendMessage('alert', {
title: 'O Comboio do Saico recomenda este canal',
user_name: displayUser(userChannel.displayName, userChannel.name),
game: latinize(userChannel.gameName)
});
chatClient.say(channel, `Recomendação do Comboio: https://twitch.tv/${userChannel.name}, que estava em ${userChannel.gameName} - siga você também!`);
}
chatClient.onMessage(async (channel, user, message, privmsg) => {
if (privmsg.isCheer) {
const username = displayUser(privmsg.userInfo.displayName, user);
nodecg.sendMessage('alert', {
tee: true,
user_name: username,
title: `${pluralize(privmsg.bits, 'bit enviado', 'bits enviados')} para o Comboio`,
message: message
});
cheer.value = `${username} (${privmsg.bits})`;
} else if (message.startsWith('!')) {
const args = message.slice(1).split(' ');
let command = args.shift().toLowerCase();
if (command === 'comandos') {
chatClient.say(channel, `Comandos disponíveis: !sh !contadores ${Object.keys(config.commands).map(command => '!'+command).join(' ')}`);
} else if (command === 'contadores') {
counters.value
.filter(counter => counter.show)
.forEach(counter => {
chatClient.say(channel, `!${counter.command} - ${counter.description}`, { replyTo: privmsg });
});
} else if (command === 'sh') {
if (privmsg.userInfo.isBroadcaster || privmsg.userInfo.isMod) {
sh(channel, args[0]);
}
} else if (config.secret[command]) {
handleCommand(channel, message, privmsg, command, config.secret[command]);
} else if (config.commands[command]) {
handleCommand(channel, message, privmsg, command, config.commands[command]);
} else {
const counter = counters.value.find(counter => counter.command === command);
if (counter && counter.show) {
counter.count++;
if (counter.message) {
chatClient.say(channel, counter.message.replace('####', counter.count));
}
if (counter.play) {
nodecg.sendMessage('play', counter.play);
}
}
}
}
});
function onSub(channel, user, info, notice) {
subscriber.value = displayUser(info.displayName, user);
nodecg.sendMessage('alert', {
tee: true,
user_name: subscriber.value,
title: `Passe adquirido, somando ${pluralize(info.months, 'mês', 'meses')}`,
message: info.message
});
}
chatClient.onSub(onSub);
chatClient.onResub(onSub);
chatClient.onBan((channel, user) => {
nodecg.sendMessage('play', 'banido.mp4');
});
chatClient.onRaid((channel, user, info, notice) => {
nodecg.sendMessage('alert', {
tee: true,
user_name: displayUser(info.displayName, user),
title: `Embarque de uma raid com ${pluralize(info.viewerCount, 'pessoa', 'pessoas')}`
});
});
nodecg.listenFor('chat', message => {
chatClient.say(config.channel.name, message);
});
const mqtt = aedes();
mqtt.authenticate = (client, username, password, cb) => {
cb(null, username === config.mqtt.secret);
}
mqtt.on('clientReady', client => {
nodecg.log.info(`MQTT client connected: ${client.id}`);
});
const mqttServer = net.createServer(mqtt.handle);
mqttServer.listen(config.mqtt.port, () => {
nodecg.log.info('MQTT server ready');
});
mqtt.subscribe(`${config.mqtt.rootTopics.elite}/#`, (packet, cb) => {
const root = config.mqtt.rootTopics.elite;
const payload = packet.payload.toString();
switch (packet.topic) {
case `${root}/FeedActive`:
case `${root}/GameRunning`:
break;
case `${root}/Location/System`:
elite.system = payload;
break;
case `${root}/Location/Station`:
elite.station = payload;
break;
default:
nodecg.log.debug(`MQTT topic unprocessed: ${packet.topic}`);
}
cb();
});
};