forked from Clad3815/Twitch-Streamer-GPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitchBot.js
419 lines (351 loc) · 15.5 KB
/
twitchBot.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Import required modules
const fs = require('fs');
const path = require('path');
const dotenv = require('dotenv');
const { Bot } = require('@twurple/easy-bot');
const { StaticAuthProvider } = require('@twurple/auth');
const { PubSubClient } = require('@twurple/pubsub');
const { ApiClient } = require('@twurple/api');
const voiceHandler = require("./modules/voiceHandler.js");
const openaiLib = require("./modules/openaiLib.js");
const express = require('express');
const tmi = require('@twurple/auth-tmi');
dotenv.config();
const enableDebug = process.env.DEBUG_MODE === '1';
// Create an Express app
const app = express();
app.use(express.json());
const promptsConfig = JSON.parse(fs.readFileSync('./prompts/prompts.json', 'utf-8'));
process.on('uncaughtException', (err, origin) => {
if (enableDebug) {
console.error('An uncaught exception occurred!');
console.error(err);
console.error('Exception origin:', origin);
}
});
process.on('unhandledRejection', (reason, promise) => {
if (enableDebug) {
console.error('An unhandled rejection occurred!');
console.error('Reason:', reason);
console.error('Promise:', promise);
}
});
const clientId = process.env.TWITCH_BOT_CLIEND_ID;
const accessToken = process.env.TWITCH_BOT_ACCESS_TOKEN;
const refreshToken = process.env.TWITCH_BOT_REFRESH_TOKEN;
const channelName = process.env.TWITCH_CHANNEL_NAME;
const broadcasterClientId = process.env.TWITCH_BROADCASTER_CLIEND_ID;
const broadcasterAccessToken = process.env.TWITCH_BROADCASTER_ACCESS_TOKEN;
const redemptionTrigger = process.env.TWITCH_POINT_REDEMPTIONS_TRIGGER;
const giftCounts = new Map();
const authProvider = new StaticAuthProvider(clientId, accessToken);
const apiClient = new ApiClient({ authProvider });
const broadcasterAuthProvider = new StaticAuthProvider(broadcasterClientId, broadcasterAccessToken);
const broadcasterApiClient = new ApiClient({ authProvider: broadcasterAuthProvider });
const pubSubClient = new PubSubClient({ authProvider: broadcasterAuthProvider });
const bot = new Bot({
authProvider,
channels: [channelName],
});
const tmiClient = new tmi.Client({
options: { debug: enableDebug },
connection: {
reconnect: true,
secure: true
},
authProvider: broadcasterAuthProvider,
channels: [channelName]
});
console.log("Bot started and listening to channel " + channelName);
async function randomDelay(minMs = 1, maxMs = 100) {
const delay = Math.floor(Math.random() * (maxMs - minMs + 1)) + minMs;
return new Promise(resolve => setTimeout(resolve, delay));
}
async function handleTwitchEvent(eventHandler, data) {
// Add the action to the queue
await randomDelay();
await voiceHandler.addActionToQueue(() => eventHandler(data));
}
// Twitch Event Subscriptions
if (process.env.ENABLE_TWITCH_ONSUB === '1') {
bot.onSub((data) => handleTwitchEvent(handleOnSub, data));
}
if (process.env.ENABLE_TWITCH_ONRESUB === '1') {
tmiClient.on("resub", (channel, username, streakMonths, message, userstate, methods) => {
let cumulativeMonths = ~~userstate["msg-param-cumulative-months"];
handleTwitchEvent(handleOnResub, { broadcasterName: channel, userName: username, months: cumulativeMonths });
});
}
if (process.env.ENABLE_TWITCH_ONCOMMUNITYSUB === '1') {
tmiClient.on("submysterygift", (channel, username, numbOfSubs, methods, userstate) => {
let senderCount = ~~userstate["msg-param-sender-count"];
handleTwitchEvent(handleOnCommunitySub, { broadcasterName: channel, gifterName: username, giftSubCount: numbOfSubs, totalGiftSubCount: senderCount });
});
}
if (process.env.ENABLE_TWITCH_ONHYPECHAT === '1') {
tmiClient.on("message", (channel, tags, message, self) => {
if(tags['pinned-chat-paid-amount']) {
const paidAmount = tags['pinned-chat-paid-amount'];
const paidCurrency = tags['pinned-chat-paid-currency'];
const paidExponent = tags['pinned-chat-paid-exponent'];
const paidLevel = tags['pinned-chat-paid-level'];
const isSystemMessage = tags['pinned-chat-paid-is-system-message'] === '1';
handleTwitchEvent(handleHypeChat, {
paidAmount,
paidCurrency,
paidExponent,
paidLevel,
isSystemMessage,
userMessage: message
});
}
});
}
if (process.env.ENABLE_TWITCH_ONPRIMEPAIDUPGRADE === '1') {
bot.onPrimePaidUpgrade((data) => handleTwitchEvent(handleOnPrimePaidUpgrade, data));
}
if (process.env.ENABLE_TWITCH_ONGIFTPAIDUPGRADE === '1') {
bot.onGiftPaidUpgrade((data) => handleTwitchEvent(handleOnGiftPaidUpgrade, data));
}
// Add other event subscriptions as needed
function readRandomWaitMP3() {
const mp3Files = fs.readdirSync(path.join(__dirname, 'wait_mp3'));
const randomMP3 = mp3Files[Math.floor(Math.random() * mp3Files.length)];
console.log("Playing wait mp3: " + path.join(__dirname, 'wait_mp3', randomMP3));
voiceHandler.streamMP3FromFile(path.join(__dirname, 'wait_mp3', randomMP3));
}
// Event Handling Functions
async function handleOnSub({ broadcasterName, userName }) {
const userData = {
name: "system"
};
const prompt = promptsConfig.onSub.replace('{userName}', userName).replace('{broadcasterName}', broadcasterName);
openaiLib.answerToMessage(userData, prompt).then((message) => {
bot.say(channelName, message);
});
}
async function handleOnResub({ broadcasterName, userName, months }) {
const userData = {
name: "system"
};
const prompt = promptsConfig.onResub.replace('{userName}', userName).replace('{broadcasterName}', broadcasterName).replace('{months}', months);
openaiLib.answerToMessage(userData, prompt).then((message) => {
bot.say(channelName, message);
});
}
async function handleOnSubGift({ broadcasterName, gifterName, recipient, totalGiftSubCount }) {
const userData = {
name: "system"
};
const prompt = promptsConfig.onSubGift.replace('{userName}', recipient).replace('{gifterName}', gifterName).replace('{broadcasterName}', broadcasterName);
openaiLib.answerToMessage(userData, prompt).then((message) => {
bot.say(channelName, message);
});
}
async function handleOnCommunitySub({ broadcasterName, gifterName, giftSubCount, totalGiftSubCount }) {
const userData = {
name: "system"
};
const prompt = promptsConfig.onCommunitySub.replace('{gifterName}', gifterName).replace('{broadcasterName}', broadcasterName).replace('{giftSubCount}', giftSubCount).replace('{totalGiftSubCount}', totalGiftSubCount);
openaiLib.answerToMessage(userData, prompt).then((message) => {
bot.say(channelName, message);
});
}
async function handleOnPrimePaidUpgrade({ broadcasterName, userName }) {
const userData = {
name: "system"
};
const prompt = promptsConfig.onPrimePaidUpgrade.replace('{userName}', userName).replace('{broadcasterName}', broadcasterName);
openaiLib.answerToMessage(userData, prompt).then((message) => {
bot.say(channelName, message);
});
}
async function handleOnGiftPaidUpgrade({ broadcasterName, userName, gifterDisplayName }) {
const userData = {
name: "system"
};
const prompt = promptsConfig.onGiftPaidUpgrade.replace('{userName}', userName).replace('{gifterDisplayName}', gifterDisplayName).replace('{broadcasterName}', broadcasterName);
openaiLib.answerToMessage(userData, prompt).then((message) => {
bot.say(channelName, message);
});
}
async function handleHypeChat({ paidAmount, paidCurrency, paidExponent, paidLevel, isSystemMessage, userMessage }) {
const actualAmount = paidAmount / Math.pow(10, paidExponent); // Convert the amount to actual value based on the exponent.
let prompt;
if (isSystemMessage) {
prompt = promptsConfig.onHypeChatSystem
.replace('{amount}', actualAmount)
.replace('{currency}', paidCurrency)
.replace('{level}', paidLevel);
} else {
prompt = promptsConfig.onHypeChatUser
.replace('{amount}', actualAmount)
.replace('{currency}', paidCurrency)
.replace('{level}', paidLevel)
.replace('{message}', userMessage);
}
const userData = {
name: "system"
};
openaiLib.answerToMessage(userData, prompt).then((answerMessage) => {
bot.say(channelName, answerMessage);
});
}
async function main() {
// Check OpenAI model availability
try {
await openaiLib.openai.models.retrieve(process.env.OPENAI_MODEL);
console.log(`Using OpenAI model ${process.env.OPENAI_MODEL}.`);
} catch (error) {
if (process.env.OPENAI_BASEPATH.startsWith('https://api.openai.com')) {
console.log(`The model ${process.env.OPENAI_MODEL} is not available.`);
if (enableDebug) {
console.log(error);
}
process.exit(1);
} else {
console.log(`Using OpenAI model ${process.env.OPENAI_MODEL}.`);
}
}
let streamInfos = {};
const user = await broadcasterApiClient.users.getUserByName(channelName);
const userFollowers = await user.getChannelFollowers();
openaiLib.initVoice();
await openaiLib.initBotFunctions(broadcasterApiClient, user.id);
streamInfos.followers = userFollowers.total;
streamInfos.description = user.description;
if (process.env.ENABLE_TWITCH_ONREDEMPTION === '1') {
pubSubClient.onRedemption(user.id, async (message) => {
const redemptionAction = async () => {
console.log(`${message.userDisplayName} just redeemed ${message.rewardTitle}!`);
if (redemptionTrigger == message.rewardTitle) {
console.log(`Message: ${message.message}`);
if (!await openaiLib.analyseMessage(message.message)) {
bot.say(channelName, promptsConfig.warningMessage.replace('{userName}', message.userDisplayName));
return;
}
const enableGoogleTTS = process.env.READ_CHANNEL_POINT_REDEMPTIONS === '1';
if (enableGoogleTTS) {
console.log("Generating TTS of the message");
const ttsPrompt = promptsConfig.ttsMessage.replace('{userDisplayName}', message.userDisplayName).replace('{message}', message.message);
const audioStream = await voiceHandler.streamMP3FromGoogleTTS(ttsPrompt);
await voiceHandler.playBufferingStream(audioStream);
await new Promise(r => setTimeout(r, 1000));
console.log("Play random wait mp3");
readRandomWaitMP3();
}
const userData = await getViewerInfos(message.userDisplayName);
const answerMessage = await openaiLib.answerToMessage(userData, message.message);
bot.say(channelName, answerMessage);
}
};
// Add the action to the queue
await voiceHandler.addActionToQueue(redemptionAction);
});
}
if (process.env.ENABLE_TWITCH_ONBITS === '1') {
pubSubClient.onBits(user.id, async (message) => {
const bitsAction = async () => {
const minBits = process.env.TWITCH_MIN_BITS ? parseInt(process.env.TWITCH_MIN_BITS) : 0;
if (message.bits >= minBits) {
const prompt = promptsConfig.onBits
.replace('{userName}', message.userName)
.replace('{bits}', message.bits)
.replace('{totalBits}', message.totalBits)
.replace('{broadcasterName}', channelName)
.replace('{message}', message.message);
if (!await openaiLib.analyseMessage(prompt)) {
bot.say(channelName, promptsConfig.warningMessage.replace('{userName}', message.userName));
return;
}
const userData = {
name: "system"
};
const answerMessage = await openaiLib.answerToMessage(userData, prompt);
bot.say(channelName, answerMessage);
}
};
// Add the action to the queue
await voiceHandler.addActionToQueue(bitsAction);
});
}
// Get current game and title
const stream = await broadcasterApiClient.streams.getStreamByUserId(user.id);
if (stream) {
streamInfos.gameName = stream.gameName;
streamInfos.title = stream.title;
streamInfos.viewers = stream.viewers;
}
openaiLib.setStreamInfos(streamInfos);
// Create an interval to update the stream infos
setInterval(async () => {
let streamInfos = {};
const user = await broadcasterApiClient.users.getUserByName(channelName);
const userFollowers = await user.getChannelFollowers();
streamInfos.followers = userFollowers.total;
streamInfos.description = user.description;
const stream = await broadcasterApiClient.streams.getStreamByUserId(user.id);
if (stream) {
streamInfos.gameName = stream.gameName;
streamInfos.title = stream.title;
streamInfos.viewers = stream.viewers;
}
openaiLib.setStreamInfos(streamInfos);
}, 10000);
await tmiClient.connect().catch(console.error);
}
// Endpoint to receive transcriptions from the voice input script
app.post('/transcription', async (req, res) => {
const transcription = req.body.transcription;
const onTranscriptionAction = async () => {
readRandomWaitMP3();
let userData = {
name: channelName,
isBroadcaster: true
};
openaiLib.answerToMessage(userData, transcription).then((answerMessage) => {
bot.say(channelName, answerMessage);
});
};
// Add the action to the queue
voiceHandler.addActionToQueue(onTranscriptionAction);
res.sendStatus(200);
});
async function getViewerInfos(viewer_name) {
const user = await apiClient.users.getUserByName(viewer_name);
const broadcaster = await apiClient.users.getUserByName(channelName);
if (user.id === broadcaster.id) {
return {
name: viewer_name,
isBroadcaster: true,
}
} else {
try {
const channel = await apiClient.channels.getChannelInfoById(broadcaster.id);
const isSub = await user.getSubscriptionTo(broadcaster.id);
const isFollow = await user.getFollowedChannel(broadcaster.id);
const isVip = await broadcasterApiClient.channels.checkVipForUser(channel, user);
const isMod = await broadcasterApiClient.moderation.checkUserMod(channel, user);
return {
name: viewer_name,
isModerator: isMod ? true : false,
isSubscriber: isSub ? true : false,
isVip: (isVip || isMod) ? true : false,
isFollower: isFollow ? true : false,
}
} catch (e) {
if (enableDebug) {
console.log(e);
}
return {
name: viewer_name
}
}
}
}
// Start the Express server
const port = process.env.PORT_NUMBER || 3000;
app.listen(port, () => {
console.log(`Twitch bot listening at http://localhost:${port}`);
});
main();