-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
136 lines (116 loc) · 3.22 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
const tmi = require('tmi.js');
const { Worker } = require('worker_threads');
const filterText = require('./wordFilterSystem');
require('dotenv').config();
let metaphone;
const twitchUsername = process.env.TWITCH_USERNAME;
const twitchOAuthToken = process.env.TWITCH_OAUTH_TOKEN;
const channelName = process.env.TWITCH_CHANNEL;
const MODE_SKIP = 1;
const MODE_CHAOS = 2;
const MODE_QUEUE = 3;
let currentMode = MODE_QUEUE;
let messageQueue = [];
let isPlaying = false;
const playNextInQueue = () => {
if (currentMode === MODE_QUEUE && messageQueue.length > 0) {
playText(messageQueue.shift());
} else {
isPlaying = false;
}
};
const worker = new Worker('./worker.js');
worker.on('message', () => {
isPlaying = false;
if (currentMode === MODE_SKIP) {
setTimeout(() => {
playNextInQueue();
}, 30000); // 30-second delay between skipped messages so its tollerable.
} else {
playNextInQueue();
}
});
const playText = (textObj) => {
const filteredText = filterText(textObj.message);
if (filteredText.trim() === '') {
if (currentMode !== MODE_CHAOS) {
playNextInQueue(); // Process the next message in the queue when the current message is empty after filtering.
}
return;
}
if (currentMode === MODE_CHAOS) {
const chaosWorker = new Worker('./worker.js');
chaosWorker.postMessage({ text: filteredText, username: textObj.username });
} else {
isPlaying = true;
worker.postMessage({ text: filteredText, username: textObj.username });
}
};
const handleModeChange = (command, username) => {
const mode = command.split(' ')[1];
if (username === channelName) {
switch (mode) {
case 'skip':
currentMode = MODE_SKIP;
console.log(`Mode changed to: SKIP`);
break;
case 'chaos':
currentMode = MODE_CHAOS;
console.log(`Mode changed to: CHAOS`);
break;
case 'queue':
currentMode = MODE_QUEUE;
console.log(`Mode changed to: QUEUE`);
break;
default:
console.log(`Invalid mode: ${mode}`);
}
} else {
console.log(`User ${username} is not authorized to change the mode.`);
}
};
const startTwitchClient = async () => {
const client = new tmi.Client({
options: { debug: true },
connection: {
reconnect: true,
secure: true,
},
identity: {
username: twitchUsername,
password: twitchOAuthToken,
},
channels: [channelName],
});
client.connect();
client.on('message', (channel, userstate, message, self) => {
if (self) return;
const username = userstate.username;
if (message.startsWith('!mode')) {
handleModeChange(message, username);
return;
}
if (currentMode === MODE_CHAOS) {
playText({ username, message });
} else {
switch (currentMode) {
case MODE_SKIP:
if (!isPlaying) {
isPlaying = true;
playText({ username, message });
}
break;
case MODE_QUEUE:
default:
if (!isPlaying) {
isPlaying = true;
playText({ username, message });
} else {
messageQueue.push({ username, message });
}
break;
}
}
});
};
startTwitchClient();