-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
476 lines (440 loc) · 14.6 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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/**
*
* FEATURE LIST:
* - Horse Racing with TTS
* - Sopranos
* - Chess
*/
const Discord = require("discord.js");
const config = require("./config.json");
const gTTS = require('gtts');
const fs = require('fs');
const ffmpeg = require('fluent-ffmpeg');
const websocket = require('ws');
const { exec } = require("child_process");
require('log-timestamp');
const prefix = "~";
var client = undefined;
const TTS_LANGUAGES = {
'af': 'Afrikaans',
'sq': 'Albanian',
'ar': 'Arabic',
'hy': 'Armenian',
'ca': 'Catalan',
'zh': 'Chinese',
'zh-cn': 'Chinese (Mandarin/China)',
'zh-tw': 'Chinese (Mandarin/Taiwan)',
'zh-yue': 'Chinese (Cantonese)',
'hr': 'Croatian',
'cs': 'Czech',
'da': 'Danish',
'nl': 'Dutch',
'en': 'English',
'en-au': 'English (Australia)',
'en-uk': 'English (United Kingdom)',
'en-us': 'English (United States)',
'eo': 'Esperanto',
'fi': 'Finnish',
'fr': 'French',
'de': 'German',
'el': 'Greek',
'ht': 'Haitian Creole',
'hi': 'Hindi',
'hu': 'Hungarian',
'is': 'Icelandic',
'id': 'Indonesian',
'it': 'Italian',
'ja': 'Japanese',
'ko': 'Korean',
'la': 'Latin',
'lv': 'Latvian',
'mk': 'Macedonian',
'no': 'Norwegian',
'pl': 'Polish',
'pt': 'Portuguese',
'pt-br': 'Portuguese (Brazil)',
'ro': 'Romanian',
'ru': 'Russian',
'sr': 'Serbian',
'sk': 'Slovak',
'es': 'Spanish',
'es-es': 'Spanish (Spain)',
'es-us': 'Spanish (United States)',
'sw': 'Swahili',
'sv': 'Swedish',
'ta': 'Tamil',
'th': 'Thai',
'tr': 'Turkish',
'vi': 'Vietnamese',
'cy': 'Welsh'
}
var isBusy = false;
var isConnected = false;
var isBeta = false;
process.on('unhandledRejection', function(err) {
console.log(err);
});
async function handleVoiceCommand(voiceChannel, voiceCommand) {
if(isBusy) {
return;
}
if(voiceCommand.includes("sing me a song")) {
console.log('singing song');
var files = [];
var entries = fs.readdirSync(`./songs`, { withFileTypes: true });
for(var entry of entries) {
if(entry.isFile()) {
files.push(`./songs/${entry.name}`);
}
}
if(files.length > 0) {
var fileToPlay = files[Math.floor(Math.random() * files.length)];
exec(`text2wave -mode singing -o song.wav ${fileToPlay}`, (error, stdout, stderr) => {
console.log('Generated song file.');
if(error) {
console.log(error);
} else {
sayTTS(voiceChannel, 'here goes nothing')
.then(async () => {
console.log('here');
playAudio(voiceChannel, './song.wav')
.then(() => sayTTS(voiceChannel, 'thank you'))
.catch(error => console.error(error));
})
.catch(error => {
console.log(error);
})
}
});
}
}
}
function convertSpeechToWav(inputFile, outputFile) {
return new Promise((resolve, reject) => {
ffmpeg(inputFile)
.inputOptions(['-f s16le', '-ar 48k', '-ac 2'])
.output(outputFile)
.outputOptions(['-filter:a loudnorm', '-ar 8k', '-ac 1'])
.on('end', function() {
resolve(outputFile);
})
.on('error', err => {
reject(err);
})
.run();
});
}
function speechToText(inputFile) {
return new Promise((resolve, reject) => {
var results = [];
vosk_ws = new websocket('ws://vosk:2700');
vosk_ws.on('open', function open() {
var readStream = fs.createReadStream(inputFile);
readStream.on('data', function (chunk) {
vosk_ws.send(chunk);
});
readStream.on('end', function () {
vosk_ws.send('{"eof" : 1}');
});
});
vosk_ws.on('message', function incoming(data) {
results.push(JSON.parse(data));
});
vosk_ws.on('error', error => reject(error));
vosk_ws.on('close', () => resolve(results));
});
}
/**
* Joins the specified voice channel if not already in it.
*/
async function joinChannel(voiceChannel) {
var connection = client.voice.connections.find(conn => conn.channel.id == voiceChannel.id);
if(connection == undefined) {
connection = await voiceChannel.join();
connection.on('error', error => {
if(isConnected) {
isConnected = false;
console.log("Voice channel disconnected.");
connect();
}
});
connection.on('speaking', async(user, speaking) => {
if (speaking.bitfield == 0) {
return;
}
console.log(`Beta = ${isBeta}`);
if (isBeta == false) {
return;
}
var recordFile = `record/voice-${Date.now()}.raw`;
var waveFile = recordFile + '.wav';
let ws = fs.createWriteStream(recordFile);
const audioStream = connection.receiver.createStream(user, { mode: 'pcm'});
audioStream.pipe(ws);
audioStream.on('error', e => console.error(e));
audioStream.on('end', () => {
convertSpeechToWav(recordFile, waveFile)
.then(waveFile => {
speechToText(waveFile)
.then(results => {
for(var result of results) {
if(result.text != undefined) {
var speechText = result.text.trim();
console.log(`Got speech: ${speechText}`);
if(speechText.startsWith('hey reginald')) {
handleVoiceCommand(voiceChannel, speechText);
}
}
}
fs.unlinkSync(recordFile);
fs.unlinkSync(waveFile);
})
.catch(err => {
console.log("Transcribe failed.");
console.log(err);
})
})
.catch(err => {
console.log("File conversion failed.");
console.log(err);
});
});
});
}
return connection;
}
/**
* Plays the specified audio file over the discord voice channel. Connects to voice channel if needed.
*
* @param {VoiceChannel} voiceChannel
* @param {string} file
*/
function playAudio(voiceChannel, file) {
return new Promise((resolve, reject) => {
joinChannel(voiceChannel)
.then(connection => {
if(!isConnected) {
return;
}
const dispatcher = connection.play(file);
dispatcher.on("finish", end => {
isBusy = false;
resolve();
});
})
.catch(error => {
console.error(`Error joining voice channel ${voiceChannel.name}.`);
console.error(error);
reject();
});
});
}
function sayTTS(voiceChannel, text, language = 'en') {
return new Promise((resolve, reject) => {
isBusy = true;
var fileToPlay = undefined;
if(language == 'en') {
text = text.replace(/'/g, "\\'");
text = text.replace(/"/g, `\\"`);
exec(`echo ${text} | text2wave -o tts.wav`, (error, stdout, stderr) => {
if(error) {
console.log(error);
}
fileToPlay = './tts.wav';
playAudio(voiceChannel, fileToPlay)
.then(() => resolve())
.catch(error => reject(error));
});
} else {
var gtts = new gTTS(text, language);
gtts.save('./tts.mp3', function (error, result) {
if(error) {
console.log(error);
}
fileToPlay = './tts.mp3';
});
}
});
}
function handleTTS(message, command, subCommand, args, messageText) {
if(isBusy) {
return message.reply("I'm busy.");
}
if(!message.member.voice.channel)
{
return message.reply("You have to be in a voice channel.");
}
if(messageText.trim() == '')
{
return message.reply("Usage: ~tts some text here.");
}
if(subCommand == '') {
subCommand = 'say';
}
if(subCommand == 'say') {
var language = 'en';
if(args.length > 0) {
var languageArg = args[0].trim();
if(languageArg in TTS_LANGUAGES) {
language = languageArg;
}
}
sayTTS(message.member.voice.channel, messageText.trim(), language);
}
else if(subCommand == 'help') {
var languageList = "";
for(var languageCode in TTS_LANGUAGES) {
languageList += languageCode + ": " + TTS_LANGUAGES[languageCode] + "\r\n";
}
const helpEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Reginald.Bot TTS Help')
.setDescription('Speaks text into the voice channel you are in. Default language / accent is US English.')
.addFields(
{ name: 'Usage', value: '~tts [optional-language-code] text to speak'},
{ name: 'Help', value: '~tts !help' },
{ name: 'TTS Languages', value: languageList }
);
message.channel.send(helpEmbed);
}
}
/**
* Given a soundboard name and the category, returns a file to be played.
* @param {*} soundboard
* @param {*} category
*/
function getSoundboardFile(soundboard, category) {
// Figure out categories
var categories = [];
var dirEntries = fs.readdirSync(`./${soundboard}`, { withFileTypes: true });
for(var dirEnt of dirEntries) {
if(dirEnt.isDirectory()) {
categories.push(dirEnt.name);
}
}
// Specific category or all categories.
var categoriesToInclude = [];
if(categories.indexOf(category) >= 0) {
categoriesToInclude.push(category);
} else {
categoriesToInclude = categories;
}
// Find candidate files.
var files = [];
for(var cat of categoriesToInclude) {
var entries = fs.readdirSync(`./${soundboard}/${cat}`, { withFileTypes: true });
for(var entry of entries) {
if(entry.isFile()) {
files.push(`${cat}/${entry.name}`);
}
}
}
if(files.length > 0) {
var fileToPlay = files[Math.floor(Math.random() * files.length)];
return `./${soundboard}/${fileToPlay}`;
}
return undefined;
}
function handleRoll(message, command, subCommand, args, messageText) {
if(messageText.trim() == '') {
messageText = '6';
}
var rollSideString = messageText.trim();
if(!isNaN(Number(rollSideString))) {
numSides = parseInt(Number(rollSideString));
message.channel.send("Rolled a " + numSides + " sided die and got " + Math.floor((Math.random() * numSides) + 1));
}
else {
message.reply("That's not a number.");
}
}
function handleSoprano(message, command, subCommand, args, messageText) {
if(!message.member.voice.channel)
{
return message.reply("You have to be in a voice channel.");
}
var fileToPlay = getSoundboardFile('sop');
if(fileToPlay != undefined) {
playAudio(message.member.voice.channel, fileToPlay);
}
}
function handleSara(message, command, subCommand, args, messageText) {
if(!message.member.voice.channel)
{
return message.reply("You have to be in a voice channel.");
}
var fileToPlay = getSoundboardFile('sara');
if(fileToPlay != undefined) {
playAudio(message.member.voice.channel, fileToPlay);
}
}
function handleClientMessage(message) {
// Ignore all bots
if(message.author.bot) {
return;
}
// Only respond to messages with the bot prefix
if(!message.content.startsWith(prefix)) {
return;
}
// Parse out command info
const commandBody = message.content.slice(prefix.length);
const tokens = commandBody.split(' ');
const command = tokens.shift().toLowerCase();
var subCommand = '';
var args = [];
// See if any subcommand was provided. This has to follow the main command
if(tokens.length > 0 && tokens[0].startsWith('!')) {
subCommand = tokens.shift().substring(1).trim();
}
var messageText = tokens.join(' ');
// See if any arguments were provided. They have to be provided right
// after any subcommands.
if(tokens.length > 0 && tokens[0].startsWith('[')) {
var restOfMessageBody = tokens.join(' ');
var endOfArgs = restOfMessageBody.indexOf(']');
if(endOfArgs != -1) {
args = restOfMessageBody.substring(1, endOfArgs).split(',');
messageText = restOfMessageBody.substring(endOfArgs + 1);
}
}
// Handle commands
if(command == "tts") {
handleTTS(message, command, subCommand, args, messageText);
}
else if(command == "roll") {
handleRoll(message, command, subCommand, args, messageText);
}
else if(command == "sop" || command == "soprano") {
handleSoprano(message, command, subCommand, args, messageText);
}
else if(command == "sara" || command == "jamie") {
handleSara(message, command, subCommand, args, messageText);
}
else if(command == "beta") {
isBeta = ~isBeta;
}
}
function handleClientError(error) {
isConnected = false;
client.destroy();
console.error("Connection error. Going to try again in 2 seconds...");
setTimeout(connect, 2000);
}
function connect() {
console.log("Attempting to connect...");
if(client != undefined) {
client.destroy();
}
client = new Discord.Client();
client.on("message", handleClientMessage);
client.on("ready", () => {
console.log("Client connected.");
isConnected = true;
});
client.login(config.BOT_TOKEN).catch(error => {
console.error("Could not login.");
handleClientError(error);
});
}
connect();