-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice.js
346 lines (312 loc) · 11.9 KB
/
voice.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
const { Client: DiscordClient, GatewayIntentBits, Partials } = require('discord.js');
const {
createAudioPlayer,
entersState,
joinVoiceChannel,
VoiceConnectionStatus
} = require('@discordjs/voice');
const { Client: TwitchClient } = require("tmi.js");
const cors = require('cors');
const express = require("express");
const app = express();
const corsOptions = {
origin: [
process.env.AB_OVERLAY_ORIGIN,
// /\.glitch\.me$/,
// "https://codepen.io",
// "https://cdpn.io"
],
methods: "GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS",
}
app.use(cors(corsOptions));
const sseExpress = require('sse-express');
const KoboldAIClient = require("./kobold");
const koboldai = new KoboldAIClient();
const { matchVoiceAndPlay } = require("./tts");
const { wait } = require("./utils");
const TTSQueue = require("./tts-queue");
const queue = new TTSQueue();
koboldai.queue = queue;
queue.processQueue();
const discord = new DiscordClient({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates
],
partials: [ Partials.Channel ]
});
const twitchChannel = process.env.TWITCH_CHANNEL;
const twitch = new TwitchClient({
// options: { debug: true },
connection: {
secure: true,
reconnect: true,
},
identity: {
username: process.env.TWITCH_BOT_USERNAME,
password: process.env.TWITCH_BOT_TOKEN,
},
channels: [ twitchChannel ],
});
koboldai.twitch = twitch;
const consolere = require("console-remote-client");
consolere.connect({
server: 'https://console.re', // optional, default: https://console.re
channel: process.env.CONSOLERE_CHANNEL, // required
redirectDefaultConsoleToRemote: true, // optional, default: false
disableDefaultConsoleOutput: true, // optional, default: false
});
const voiceChannelId = process.env.DISCORD_VOICE_CHANNEL_ID
var botVoice = "en-US-Wavenet-C"; //"glados-p2";
var channel;
/**
* DISCORD
*/
// discord.once('ready', () => {
// console.re.log('Ready!');
// });
discord.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === 'k9pause') {
queue.pause();
await interaction.reply('TTS queue paused');
} else if (interaction.commandName === 'k9resume') {
queue.resume();
await interaction.reply('TTS queue resumed');
} else if (interaction.commandName === 'k9tts') {
const message = interaction.options.getString('message');
matchVoiceAndPlay(queue, message, botVoice);
await tmpReply(interaction, "Message added to TTS queue.");
} else if (interaction.commandName === 'ab') {
if (interaction.options.getSubcommand() === 'prompt' && koboldai.round === "PROMPT") {
const prompt = interaction.options.getString('prompt');
koboldai.addPrompt(interaction.user.username, prompt.trim());
await tmpReply(interaction, "Prompt submitted!");
} else if (interaction.options.getSubcommand() === 'skip') {
queue.vstop();
await tmpReply(interaction, "Skipping TTS...");
} else if (interaction.options.getSubcommand() === 'redo' && koboldai.round === "VOTE") {
koboldai.redo();
await tmpReply(interaction, "Regenerated response(s)");
} else if (interaction.options.getSubcommand() === 'retry' && koboldai.round === "VOTE") {
koboldai.retry();
await tmpReply(interaction, "Retrying last action");
} else if (interaction.options.getSubcommand() === 'continue' && koboldai.round === "PROMPT") {
koboldai.addPrompt(interaction.user.username, "");
await tmpReply(interaction, "Continuing...");
} else if (interaction.options.getSubcommand() === 'next') {
koboldai.nextRound();
await tmpReply(interaction, "Advancing to the next round");
} else if (interaction.options.getSubcommand() === 'new') {
koboldai.newStory();
await tmpReply(interaction, "Starting a new story");
} else if (interaction.options.getSubcommand() === 'end' && koboldai.round === "PROMPT") {
await interaction.deferReply();
await koboldai.endStory();
await interaction.editReply("Ended the current story");
await wait(1000);
await interaction.deleteReply();
} else if (interaction.options.getSubcommand() === 'start') {
koboldai.startAdventureBot();
await tmpReply(interaction, "Starting game loop");
} else if (interaction.options.getSubcommand() === 'stop') {
koboldai.stopAdvetnureBot();
await tmpReply(interaction, "Stopping game loop");
} else if (interaction.options.getSubcommand() === 'model') {
const model = await koboldai.getCurrentModel();
await interaction.reply(`Current KoboldAI Base URL: ${model}`);
} else if (interaction.options.getSubcommand() === 'join') {
await setupVoice(queue);
await tmpReply(interaction, "Joining voice channel...");
} else if (interaction.options.getSubcommand() === 'leave') {
queue.vdisconnect();
await tmpReply(interaction, "Leaving voice channel...");
} else if (interaction.options.getSubcommand() === 'url') {
const baseUrl = interaction.options.getString('url');
if (baseUrl) {
koboldai.saveBaseUrl(baseUrl);
await tmpReply(interaction, "KoboldAI Base URL updated!");
} else {
await tmpReply(interaction, `Current KoboldAI Base URL: ${koboldai.baseUrl}`, 3000);
}
} else if (interaction.options.getSubcommand() === 'generate') {
const prompt = interaction.options.getString('prompt');
const genOptions = {
temperature: 0.6,
top_p: 1.0,
max_length: 80, // tokens to generate
};
await interaction.deferReply();
const responses = await koboldai.generate(prompt, genOptions);
if (responses.length > 0)
await interaction.editReply(responses[0].text);
else {
await interaction.editReply("Bot response was empty");
}
} else {
console.re.warn(interaction.commandName);
console.re.warn(interaction.options.getSubcommand());
await tmpReply(interaction, "Not valid round for that command");
}
}
});
async function tmpReply(interaction, message, waitTimeInMs=1000) {
await interaction.reply(message);
await wait(waitTimeInMs);
await interaction.deleteReply();
}
async function setupVoice(queue) {
if(!discord) return console.re.error("Please connect to client first!");
channel = await discord.channels.fetch(voiceChannelId);
if (!channel) return console.re.error("The channel does not exist!");
// console.log(`joining voice channel> channelName = ${channel.name}, channelId = ${channel.id}, guildId = ${channel.guild.id}`);
const connection = joinVoiceChannel({
channelId: `${channel.id}`,
guildId: `${channel.guild.id}`,
adapterCreator: channel.guild.voiceAdapterCreator,
});
connection.on(VoiceConnectionStatus.Disconnected, async (oldState, newState) => {
console.re.log('Connection is disconnected...');
try {
await Promise.race([
entersState(connection, VoiceConnectionStatus.Signalling, 5),
entersState(connection, VoiceConnectionStatus.Connecting, 5),
]);
// Seems to be reconnecting to a new channel - ignore disconnect
} catch (error) {
// Seems to be a real disconnect which SHOULDN'T be recovered from
connection.destroy();
}
});
connection.on('stateChange', (oldState, newState) => {
console.re.log(`! Discord voice connection: ${oldState.status} -> ${newState.status}`);
});
const player = createAudioPlayer();
player.on('error', error => {
console.re.error(error);
});
queue._connection = connection;
queue._player = player;
queue._subscription = connection.subscribe(player);
}
const IGNORED_USERS = ["nightbot", "streamelements"];
const cmdRegex = new RegExp(/^!([a-zA-Z0-9]+)(?:\W+)?(.*)?/i);
/**
* TWITCH
*/
twitch.on("message", async (channel, userstate, message, self) => {
// ignore echoed messages & commands
if (self) return;
const user = userstate.username;
const isOwner = channel === `#${user}`;
const isMod = userstate.mod;
const isVip = userstate.badges != null && userstate.badges.vip;
const isSubscriber = userstate.subscriber;
const isReward = userstate["custom-reward-id"] != null;
if (IGNORED_USERS.indexOf(user) > -1) return;
const cmdFound = message.match(cmdRegex);
if (!cmdFound) return;
var [_, command, argument] = cmdFound;
command = command.toLowerCase();
// ADVENTURE BOT USER COMMANDS
if (koboldai.running && command === "vote" && (koboldai.round === "VOTE" || koboldai.round === "GENERATE")) {
const voteIndex = Math.abs(parseInt(argument));
if (isNaN(voteIndex) || voteIndex < 1 || voteIndex > koboldai.botResponses.length) return;
const success = koboldai.addVote(user, voteIndex - 1);
if (success) twitch.say(channel, `@${user} vote added for bot response #${voteIndex}!`);
}
if (!isOwner && !isMod) return;
// TTS MOD COMMANDS
if (command === "aplay") {
queue.vunpause();
} else if (command === "apause") {
queue.vpause();
} else if (command === "askip") {
queue.vstop();
} else if (command === "qplay") {
queue.resume();
} else if (command === "qpause") {
queue.pause();
} else if (command === "qstop") {
queue.stop();
} else if (command === "setbotvoice") {
botVoice = argument;
koboldai.voice = argument;
}
// ADVENTURE BOT MOD COMMANDS
else if (command === "abstart") {
koboldai.startAdventureBot();
} else if (command === "abstop") {
koboldai.stopAdvetnureBot();
} else if (command === "abnewstory") {
koboldai.newStory();
} else if (command === "absave") {
koboldai.saveStory();
} else if (command === "abremove" && koboldai.round === "PROMPT") {
const promptIndex = parseInt(argument);
if (isNaN(promptIndex) || promptIndex < 1 || promptIndex > koboldai.prompts.length) return;
koboldai.removePrompt(promptIndex - 1);
} else if (command === "abredo" && koboldai.round === "VOTE") {
koboldai.redo();
} else if (command === "abretry" && koboldai.round !== "PROMPT") {
koboldai.retry();
} else if (command === "abnext") {
koboldai.nextRound();
} else if (command === "abaddtime") {
koboldai.resetRoundTime();
} else if (command === "abmodel") {
const model = await koboldai.getCurrentModel();
twitch.say(channel, `Current KoboldAI model: ${model}`);
} else if (command === "prompt" && koboldai.round === "PROMPT") {
if (!argument || argument === "") return;
const prompt = argument.trim();
if (prompt === "") return;
koboldai.addPrompt(user, prompt);
} else if (command === "continue" && koboldai.round === "PROMPT") {
koboldai.addPrompt(user, "");
}
});
/**
* EXPRESS
*/
const AB_TOKEN = process.env.AB_TOKEN;
app.get("/adventurebot/events", sseExpress(), (request, response) => {
if (!request.query.token || request.query.token !== AB_TOKEN) {
response.status(403).send({ error: 'Forbidden' });
return;
}
console.re.log("AdventureBot> event source client opened");
const intervalId = setInterval(() => {
const eventData = {
round: koboldai.round,
roundStartTime: koboldai.roundStartTime,
story: koboldai.story,
currentPrompt: koboldai.currentPrompt,
botResponses: koboldai.botResponses,
votes: koboldai.votes,
winningResponse: koboldai.winningResponse,
endPrompt: koboldai.endPrompt,
endResponse: koboldai.endResponse
};
response.sse({
event: 'heartbeat',
data: eventData
});
}, 200);
response.on('close', () => {
console.re.log("AdventureBot> event source client closed");
clearInterval(intervalId);
});
});
/**
* Starts everything need to run Hular Hoops Bot!
*/
function start() {
console.re.log("Starting hular hoops bot...");
discord.login();
twitch.connect();
const listener = app.listen(process.env.PORT, () => {
console.re.log("Your app is listening on port " + listener.address().port);
});
}
start();