forked from pedrojlazevedo/twitch-chatgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitch_bot.js
188 lines (170 loc) · 5.44 KB
/
twitch_bot.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
// Import tmi.js module
import tmi from 'tmi.js';
import OpenAI from 'openai';
import { promises as fsPromises } from 'fs';
export class TwitchBot {
constructor(bot_username, oauth_token, channels, openai_api_key, enable_tts) {
this.channels = channels;
this.client = new tmi.client({
connection: {
reconnect: true,
secure: true
},
identity: {
username: bot_username,
password: oauth_token
},
channels: this.channels
});
this.openai = new OpenAI({apiKey: openai_api_key});
this.enable_tts = enable_tts;
}
addChannel(channel) {
// Check if channel is already in the list
if (!this.channels.includes(channel)) {
this.channels.push(channel);
// Use join method to join a channel instead of modifying the channels property directly
this.client.join(channel);
}
}
connect() {
// Use async/await syntax to handle promises
(async () => {
try {
// Await for the connection to be established
await this.client.connect();
} catch (error) {
// Handle any errors that may occur
console.error(error);
}
})();
}
disconnect() {
// Use async/await syntax to handle promises
(async () => {
try {
// Await for the connection to be closed
await this.client.disconnect();
} catch (error) {
// Handle any errors that may occur
console.error(error);
}
})();
}
onMessage(callback) {
this.client.on('message', callback);
}
onConnected(callback) {
this.client.on('connected', callback);
}
onDisconnected(callback) {
this.client.on('disconnected', callback);
}
say(channel, message) {
// Use async/await syntax to handle promises
(async () => {
try {
// Await for the message to be sent
await this.client.say(channel, message);
} catch (error) {
// Handle any errors that may occur
console.error(error);
}
})();
}
async sayTTS(channel, text, userstate) {
// Check if TTS is enabled
if (this.enable_tts !== 'true') {
return;
}
try {
// Make a call to the OpenAI TTS model
const mp3 = await this.openai.audio.speech.create({
model: 'tts-1',
voice: 'alloy',
input: text,
});
// Convert the mp3 to a buffer
const buffer = Buffer.from(await mp3.arrayBuffer());
// Save the buffer as an MP3 file
const filePath = './public/file.mp3';
await fsPromises.writeFile(filePath, buffer);
// Return the path of the saved audio file
return filePath;
} catch (error) {
console.error('Error in sayTTS:', error);
}
}
whisper(username, message) {
// Use async/await syntax to handle promises
(async () => {
try {
// Await for the message to be sent
await this.client.whisper(username, message);
} catch (error) {
// Handle any errors that may occur
console.error(error);
}
})();
}
ban(channel, username, reason) {
// Use async/await syntax to handle promises
(async () => {
try {
// Await for the user to be banned
await this.client.ban(channel, username, reason);
} catch (error) {
// Handle any errors that may occur
console.error(error);
}
})();
}
unban(channel, username) {
// Use async/await syntax to handle promises
(async () => {
try {
// Await for the user to be unbanned
await this.client.unban(channel, username);
} catch (error) {
// Handle any errors that may occur
console.error(error);
}
})();
}
clear(channel) {
// Use async/await syntax to handle promises
(async () => {
try {
// Await for the chat to be cleared
await this.client.clear(channel);
} catch (error) {
// Handle any errors that may occur
console.error(error);
}
})();
}
color(channel, color) {
// Use async/await syntax to handle promises
(async () => {
try {
// Await for the color to be changed
await this.client.color(channel, color);
} catch (error) {
// Handle any errors that may occur
console.error(error);
}
})();
}
commercial(channel, seconds) {
// Use async/await syntax to handle promises
(async () => {
try {
// Await for the commercial to be played
await this.client.commercial(channel, seconds);
} catch (error) {
// Handle any errors that may occur
console.error(error);
}
})();
}
}