forked from healzer/DiscordSpeechBot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
1153 lines (1032 loc) · 36 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////
//////////////// LOGGING /////////////////
//////////////////////////////////////////
function getCurrentDateString() {
return (new Date()).toISOString() + ' ::';
};
__originalLog = console.log;
console.log = function () {
var args = [].slice.call(arguments);
__originalLog.apply(console.log, [getCurrentDateString()].concat(args));
};
//////////////////////////////////////////
//////////////////////////////////////////
const fs = require('fs');
const util = require('util');
const path = require('path');
const request = require('request');
const { Readable } = require('stream');
//////////////////////////////////////////
///////////////// VARIA //////////////////
//////////////////////////////////////////
function necessary_dirs() {
if (!fs.existsSync('./data/')){
fs.mkdirSync('./data/');
}
}
necessary_dirs()
function shuffle(a) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function convert_audio(input) {
try {
// stereo to mono channel
const data = new Int16Array(input)
const ndata = new Int16Array(data.length/2)
for (let i = 0, j = 0; i < data.length; i+=4) {
ndata[j++] = data[i]
ndata[j++] = data[i+1]
}
return Buffer.from(ndata);
} catch (e) {
console.log(e)
console.log('convert_audio: ' + e)
throw e;
}
}
//////////////////////////////////////////
//////////////////////////////////////////
//////////////////////////////////////////
//////////////////////////////////////////
//////////////// CONFIG //////////////////
//////////////////////////////////////////
const SETTINGS_FILE = 'settings.json';
let DISCORD_TOK = null;
let WITAPIKEY = null;
let SPOTIFY_TOKEN_ID = null;
let SPOTIFY_TOKEN_SECRET = null;
function loadConfig() {
if (fs.existsSync(SETTINGS_FILE)) {
const CFG_DATA = JSON.parse( fs.readFileSync(SETTINGS_FILE, 'utf8') );
DISCORD_TOK = CFG_DATA.discord_token;
WITAPIKEY = CFG_DATA.wit_ai_token;
SPOTIFY_TOKEN_ID = CFG_DATA.spotify_token_id;
SPOTIFY_TOKEN_SECRET = CFG_DATA.spotify_token_secret;
} else {
DISCORD_TOK = process.env.DISCORD_TOK;
WITAPIKEY = process.env.WITAPIKEY;
SPOTIFY_TOKEN_ID = process.env.SPOTIFY_TOKEN_ID;
SPOTIFY_TOKEN_SECRET = process.env.SPOTIFY_TOKEN_SECRET;
}
if (!DISCORD_TOK || !WITAPIKEY)
throw 'failed loading config #113 missing keys!'
}
loadConfig()
const https = require('https')
function listWitAIApps(cb) {
const options = {
hostname: 'api.wit.ai',
port: 443,
path: '/apps?offset=0&limit=100',
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer '+WITAPIKEY,
},
}
const req = https.request(options, (res) => {
res.setEncoding('utf8');
let body = ''
res.on('data', (chunk) => {
body += chunk
});
res.on('end',function() {
cb(JSON.parse(body))
})
})
req.on('error', (error) => {
console.error(error)
cb(null)
})
req.end()
}
function updateWitAIAppLang(appID, lang, cb) {
const options = {
hostname: 'api.wit.ai',
port: 443,
path: '/apps/' + appID,
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer '+WITAPIKEY,
},
}
const data = JSON.stringify({
lang
})
const req = https.request(options, (res) => {
res.setEncoding('utf8');
let body = ''
res.on('data', (chunk) => {
body += chunk
});
res.on('end',function() {
cb(JSON.parse(body))
})
})
req.on('error', (error) => {
console.error(error)
cb(null)
})
req.write(data)
req.end()
}
//////////////////////////////////////////
//////////////////////////////////////////
//////////////////////////////////////////
const Discord = require('discord.js')
const DISCORD_MSG_LIMIT = 2000;
const discordClient = new Discord.Client()
discordClient.on('ready', () => {
console.log(`Logged in as ${discordClient.user.tag}!`)
})
discordClient.login(DISCORD_TOK)
const PREFIX = '!';
const _CMD_HELP = PREFIX + 'help';
const _CMD_JOIN = PREFIX + 'join';
const _CMD_LEAVE = PREFIX + 'leave';
const _CMD_PLAY = PREFIX + 'play';
const _CMD_PAUSE = PREFIX + 'pause';
const _CMD_RESUME = PREFIX + 'resume';
const _CMD_SHUFFLE = PREFIX + 'shuffle';
const _CMD_FAVORITE = PREFIX + 'favorite';
const _CMD_UNFAVORITE = PREFIX + 'unfavorite';
const _CMD_FAVORITES = PREFIX + 'favorites';
const _CMD_GENRE = PREFIX + 'genre';
const _CMD_GENRES = PREFIX + 'genres';
const _CMD_CLEAR = PREFIX + 'clear';
const _CMD_RANDOM = PREFIX + 'random';
const _CMD_SKIP = PREFIX + 'skip';
const _CMD_QUEUE = PREFIX + 'list';
const _CMD_DEBUG = PREFIX + 'debug';
const _CMD_TEST = PREFIX + 'hello';
const _CMD_LANG = PREFIX + 'lang';
const PLAY_CMDS = [_CMD_PLAY, _CMD_PAUSE, _CMD_RESUME, _CMD_SHUFFLE, _CMD_SKIP, _CMD_GENRE, _CMD_GENRES, _CMD_RANDOM, _CMD_CLEAR, _CMD_QUEUE, _CMD_FAVORITE, _CMD_FAVORITES, _CMD_UNFAVORITE];
const EMOJI_GREEN_CIRCLE = '🟢'
const EMOJI_RED_CIRCLE = '🔴'
const GENRES = {
'hip-hop': ['hip-hop', 'hip hop', 'hiphop', 'rap'],
'rock': ['rock'],
'dance': ['dance'],
'trance': ['techno'],
'trance': ['trance'],
'groove': ['groove'],
'classical': ['classical'],
'techno': ['techno'],
}
const guildMap = new Map();
discordClient.on('message', async (msg) => {
try {
if (!('guild' in msg) || !msg.guild) return; // prevent private messages to bot
const mapKey = msg.guild.id;
if (msg.content.trim().toLowerCase() == _CMD_JOIN) {
if (!msg.member.voice.channelID) {
msg.reply('Error: please join a voice channel first.')
} else {
if (!guildMap.has(mapKey))
await connect(msg, mapKey)
else
msg.reply('Already connected')
}
} else if (msg.content.trim().toLowerCase() == _CMD_LEAVE) {
if (guildMap.has(mapKey)) {
let val = guildMap.get(mapKey);
if (val.voice_Channel) val.voice_Channel.leave()
if (val.voice_Connection) val.voice_Connection.disconnect()
if (val.musicYTStream) val.musicYTStream.destroy()
guildMap.delete(mapKey)
msg.reply("Disconnected.")
} else {
msg.reply("Cannot leave because not connected.")
}
}
else if ( PLAY_CMDS.indexOf( msg.content.trim().toLowerCase().split('\n')[0].split(' ')[0] ) >= 0 ) {
if (!msg.member.voice.channelID) {
msg.reply('Error: please join a voice channel first.')
} else {
if (!guildMap.has(mapKey))
await connect(msg, mapKey)
music_message(msg, mapKey);
}
} else if (msg.content.trim().toLowerCase() == _CMD_HELP) {
msg.reply(getHelpString());
}
else if (msg.content.trim().toLowerCase() == _CMD_DEBUG) {
console.log('toggling debug mode')
let val = guildMap.get(mapKey);
if (val.debug)
val.debug = false;
else
val.debug = true;
}
else if (msg.content.trim().toLowerCase() == _CMD_TEST) {
msg.reply('hello back =)')
}
else if (msg.content.split('\n')[0].split(' ')[0].trim().toLowerCase() == _CMD_LANG) {
const lang = msg.content.replace(_CMD_LANG, '').trim().toLowerCase()
listWitAIApps(data => {
if (!data.length)
return msg.reply('no apps found! :(')
for (const x of data) {
updateWitAIAppLang(x.id, lang, data => {
if ('success' in data)
msg.reply('succes!')
else if ('error' in data && data.error !== 'Access token does not match')
msg.reply('Error: ' + data.error)
})
}
})
}
} catch (e) {
console.log('discordClient message: ' + e)
msg.reply('Error#180: Something went wrong, try again or contact the developers if this keeps happening.');
}
})
function getHelpString() {
let out = '**VOICE COMMANDS:**\n'
out += '```'
out += 'music help\n'
out += 'music play [random, favorites, <genre> or query]\n'
out += 'music skip\n'
out += 'music pause/resume\n'
out += 'music shuffle\n'
out += 'music genres\n'
out += 'music set favorite\n'
out += 'music favorites\n'
out += 'music list\n'
out += 'music clear list\n';
out += '```'
out += '**TEXT COMMANDS:**\n'
out += '```'
out += _CMD_HELP + '\n'
out += _CMD_JOIN + '/' + _CMD_LEAVE + '\n'
out += _CMD_PLAY + ' [query]\n'
out += _CMD_GENRE + ' [name]\n'
out += _CMD_RANDOM + '\n'
out += _CMD_PAUSE + '/' + _CMD_RESUME + '\n'
out += _CMD_SKIP + '\n'
out += _CMD_SHUFFLE + '\n'
out += _CMD_FAVORITE + '\n'
out += _CMD_UNFAVORITE + ' [name]\n'
out += _CMD_FAVORITES + '\n'
out += _CMD_GENRES + '\n'
out += _CMD_QUEUE + '\n';
out += _CMD_CLEAR + '\n';
out += '```'
return out;
}
async function connect(msg, mapKey) {
try {
let voice_Channel = await discordClient.channels.fetch(msg.member.voice.channelID);
if (!voice_Channel) return msg.reply("Error: The voice channel does not exist!");
let text_Channel = await discordClient.channels.fetch(msg.channel.id);
if (!text_Channel) return msg.reply("Error: The text channel does not exist!");
let voice_Connection = await voice_Channel.join();
voice_Connection.play('sound.mp3', { volume: 0.5 });
guildMap.set(mapKey, {
'text_Channel': text_Channel,
'voice_Channel': voice_Channel,
'voice_Connection': voice_Connection,
'musicQueue': [],
'musicDispatcher': null,
'musicYTStream': null,
'currentPlayingTitle': null,
'currentPlayingQuery': null,
'debug': false,
});
speak_impl(voice_Connection, mapKey)
voice_Connection.on('disconnect', async(e) => {
if (e) console.log(e);
guildMap.delete(mapKey);
})
msg.reply('connected!')
} catch (e) {
console.log('connect: ' + e)
msg.reply('Error: unable to join your voice channel.');
throw e;
}
}
function speak_impl(voice_Connection, mapKey) {
voice_Connection.on('speaking', async (user, speaking) => {
if (speaking.bitfield == 0 || user.bot) {
return
}
console.log(`I'm listening to ${user.username}`)
// this creates a 16-bit signed PCM, stereo 48KHz stream
const audioStream = voice_Connection.receiver.createStream(user, { mode: 'pcm' })
audioStream.on('error', (e) => {
console.log('audioStream: ' + e)
});
let buffer = [];
audioStream.on('data', (data) => {
buffer.push(data)
})
audioStream.on('end', async () => {
buffer = Buffer.concat(buffer)
const duration = buffer.length / 48000 / 4;
console.log("duration: " + duration)
if (duration < 1.0 || duration > 19) { // 20 seconds max dur
console.log("TOO SHORT / TOO LONG; SKPPING")
return;
}
try {
let new_buffer = await convert_audio(buffer)
let out = await transcribe(new_buffer);
if (out != null)
process_commands_query(out, mapKey, user.id);
} catch (e) {
console.log('tmpraw rename: ' + e)
}
})
})
}
function process_commands_query(query, mapKey, userid) {
if (!query || !query.length)
return;
let out = null;
const regex = /^music ([a-zA-Z]+)(.+?)?$/;
const m = query.toLowerCase().match(regex);
if (m && m.length) {
const cmd = (m[1]||'').trim();
const args = (m[2]||'').trim();
switch(cmd) {
case 'help':
out = _CMD_HELP;
break;
case 'skip':
out = _CMD_SKIP;
break;
case 'shuffle':
out = _CMD_SHUFFLE;
break;
case 'genres':
out = _CMD_GENRES;
break;
case 'pause':
out = _CMD_PAUSE;
break;
case 'resume':
out = _CMD_RESUME;
break;
case 'clear':
if (args == 'list')
out = _CMD_CLEAR;
break;
case 'list':
out = _CMD_QUEUE;
break;
case 'hello':
out = 'hello back =)'
break;
case 'favorites':
out = _CMD_FAVORITES;
break;
case 'set':
switch (args) {
case 'favorite':
case 'favorites':
out = _CMD_FAVORITE;
break;
}
break;
case 'play':
case 'player':
switch(args) {
case 'random':
out = _CMD_RANDOM;
break;
case 'favorite':
case 'favorites':
out = _CMD_PLAY + ' ' + 'favorites';
break;
default:
for (let k of Object.keys(GENRES)) {
if (GENRES[k].includes(args)) {
out = _CMD_GENRE + ' ' + k;
}
}
if (out == null) {
out = _CMD_PLAY + ' ' + args;
}
}
break;
}
if (out == null)
out = '<bad command: ' + query + '>';
}
if (out != null && out.length) {
// out = '<@' + userid + '>, ' + out;
console.log('text_Channel out: ' + out)
const val = guildMap.get(mapKey);
val.text_Channel.send(out)
}
}
async function music_message(message, mapKey) {
let replymsgs = [];
const messes = message.content.split('\n');
for (let mess of messes) {
const args = mess.split(' ');
if (args[0] == _CMD_PLAY && args.length) {
const qry = args.slice(1).join(' ');
if (qry == 'favorites') {
// play guild's favorites
if (mapKey in GUILD_FAVORITES) {
let arr = GUILD_FAVORITES[mapKey];
if (arr.length) {
for (let item of arr) {
addToQueue(item, mapKey)
}
message.react(EMOJI_GREEN_CIRCLE)
} else {
message.channel.send('No favorites yet.')
}
} else {
message.channel.send('No favorites yet.')
}
}
else if (isSpotify(qry)) {
try {
const arr = await spotify_tracks_from_playlist(qry);
console.log(arr.length + ' spotify items from playlist')
for (let item of arr)
addToQueue(item, mapKey);
message.react(EMOJI_GREEN_CIRCLE)
} catch(e) {
console.log('music_message 464:' + e)
message.channel.send('Failed processing spotify link: ' + qry);
}
} else {
if (isYoutube(qry) && isYoutubePlaylist(qry)) {
try {
const arr = await youtube_tracks_from_playlist(qry);
for (let item of arr)
addToQueue(item, mapKey)
message.react(EMOJI_GREEN_CIRCLE)
} catch (e) {
console.log('music_message 476:' + e)
message.channel.send('Failed to process playlist: ' + qry);
}
} else {
try {
addToQueue(qry, mapKey);
message.react(EMOJI_GREEN_CIRCLE)
} catch (e) {
console.log('music_message 484:' + e)
message.channel.send('Failed to find video for (try again): ' + qry);
}
}
}
} else if (args[0] == _CMD_SKIP) {
skipMusic(mapKey, ()=>{
message.react(EMOJI_GREEN_CIRCLE)
}, (msg)=>{
if (msg && msg.length) message.channel.send(msg);
})
} else if (args[0] == _CMD_PAUSE) {
pauseMusic(mapKey, ()=>{
message.react(EMOJI_GREEN_CIRCLE)
}, (msg)=>{
if (msg && msg.length) message.channel.send(msg);
})
} else if (args[0] == _CMD_RESUME) {
resumeMusic(mapKey, ()=>{
message.react(EMOJI_GREEN_CIRCLE)
}, (msg)=>{
if (msg && msg.length) message.channel.send(msg);
})
} else if (args[0] == _CMD_SHUFFLE) {
shuffleMusic(mapKey, ()=>{
message.react(EMOJI_GREEN_CIRCLE)
}, (msg)=>{
if (msg && msg.length) message.channel.send(msg);
})
} else if (args[0] == _CMD_CLEAR) {
clearQueue(mapKey, ()=>{
message.react(EMOJI_GREEN_CIRCLE)
}, (msg)=>{
if (msg && msg.length) message.channel.send(msg);
})
} else if (args[0] == _CMD_QUEUE) {
const chunks = message_chunking(getQueueString(mapKey), DISCORD_MSG_LIMIT);
for (let chunk of chunks) {
console.log(chunk.length)
message.channel.send(chunk);
}
message.react(EMOJI_GREEN_CIRCLE)
} else if (args[0] == _CMD_RANDOM) {
let arr = await spotify_new_releases();
if (arr.length) {
arr = shuffle(arr);
// let item = arr[Math.floor(Math.random() * arr.length)];
for (let item of arr)
addToQueue(item, mapKey);
message.react(EMOJI_GREEN_CIRCLE)
} else {
message.channel.send('no results for random');
}
} else if (args[0] == _CMD_GENRES) {
let out = "------------ genres ------------\n";
for (let g of Object.keys(GENRES)) {
out += g + '\n'
}
out += "--------------------------------\n";
const chunks = message_chunking(out, DISCORD_MSG_LIMIT);
for (let chunk of chunks)
message.channel.send(chunk);
} else if (args[0] == _CMD_GENRE) {
const genre = args.slice(1).join(' ').trim();
let arr = await spotify_recommended(genre);
if (arr.length) {
arr = shuffle(arr);
// let item = arr[Math.floor(Math.random() * arr.length)];
for (let item of arr)
addToQueue(item, mapKey);
message.react(EMOJI_GREEN_CIRCLE)
} else {
message.channel.send('no results for genre: ' + genre);
}
} else if (args[0] == _CMD_FAVORITES) {
const favs = getFavoritesString(mapKey);
if (!(mapKey in GUILD_FAVORITES) || !GUILD_FAVORITES[mapKey].length)
message.channel.send('No favorites to play.')
else {
const chunks = message_chunking(favs, DISCORD_MSG_LIMIT);
for (let chunk of chunks)
message.channel.send(chunk);
message.react(EMOJI_GREEN_CIRCLE)
}
} else if (args[0] == _CMD_FAVORITE) {
setAsFavorite(mapKey, ()=>{
message.react(EMOJI_GREEN_CIRCLE)
}, (msg)=> {
if (msg && msg.length) message.channel.send(msg);
})
} else if (args[0] == _CMD_UNFAVORITE) {
const qry = args.slice(1).join(' ');
unFavorite(qry, mapKey, ()=>{
message.react(EMOJI_GREEN_CIRCLE)
}, (msg)=>{
if (msg && msg.length) message.channel.send(msg);
})
}
}
queueTryPlayNext(mapKey, (title)=>{
message.react(EMOJI_GREEN_CIRCLE);
message.channel.send('Now playing: **' + title + '**')
}, (msg)=>{
if (msg && msg.length) message.channel.send(msg);
});
}
let GUILD_FAVORITES = {};
const GUILD_FAVORITES_FILE = './data/guild_favorites.json';
setInterval(()=>{
var json = JSON.stringify(GUILD_FAVORITES);
fs.writeFile(GUILD_FAVORITES_FILE, json, 'utf8', (err)=>{
if (err) return console.log('GUILD_FAVORITES_FILE:' + err);
});
},1000);
function load_guild_favorites() {
if (fs.existsSync(GUILD_FAVORITES_FILE)) {
const data = fs.readFileSync(GUILD_FAVORITES_FILE, 'utf8');
GUILD_FAVORITES = JSON.parse(data);
}
}
load_guild_favorites();
function setAsFavorite(mapKey, cbok, cberr) {
let val = guildMap.get(mapKey);
if (!val.currentPlayingTitle || !val.currentPlayingQuery)
cberr('Nothing playing at the moment.')
else {
if (!(mapKey in GUILD_FAVORITES)) {
GUILD_FAVORITES[mapKey] = [];
}
if (!GUILD_FAVORITES[mapKey].includes(val.currentPlayingQuery))
GUILD_FAVORITES[mapKey].push( val.currentPlayingQuery )
cbok()
}
}
function unFavorite(qry, mapKey, cbok, cberr) {
let val = guildMap.get(mapKey);
if (!qry || !qry.length)
cberr('Invalid query.');
else {
if (!(mapKey in GUILD_FAVORITES)) {
cberr('No favorites.');
} else {
if (GUILD_FAVORITES[mapKey].includes(qry)) {
GUILD_FAVORITES[mapKey] = GUILD_FAVORITES[mapKey].filter(e => e !== qry);
cbok()
} else {
cberr('Favorite not found.');
}
}
}
}
function getFavoritesString(mapKey) {
let out = "------------ favorites ------------\n";
if (mapKey in GUILD_FAVORITES) {
let arr = GUILD_FAVORITES[mapKey];
if (arr.length) {
for (let item of arr) {
out += item + '\n';
}
} else {
out += '(empty)\n'
}
} else {
out += '(empty)\n'
}
out += "-----------------------------------\n";
return out;
}
function message_chunking(msg, MAXL) {
const msgs = msg.split('\n');
const chunks = [];
let outmsg = '';
while (msgs.length) {
let a = msgs.shift() + '\n';
if (a.length > MAXL) {
console.log(a)
throw new Error('error#418: max single msg limit');
}
if ((outmsg + a + 6).length <= MAXL) {
outmsg += a;
} else {
chunks.push('```' + outmsg + '```')
outmsg = ''
}
}
if (outmsg.length) {
chunks.push('```' + outmsg + '```')
}
return chunks;
}
function getQueueString(mapKey) {
let val = guildMap.get(mapKey);
let _message = "------------ queue ------------\n";
if (val.currentPlayingTitle != null)
_message += '[X] ' + val.currentPlayingTitle + '\n';
for (let i = 0; i < val.musicQueue.length; i++) {
_message += '['+i+'] ' + val.musicQueue[i] + '\n';
}
if (val.currentPlayingTitle == null && val.musicQueue.length == 0)
_message += '(empty)\n'
_message += "---------------------------------\n";
return _message;
}
async function queueTryPlayNext(mapKey, cbok, cberr) {
try {
let val = guildMap.get(mapKey);
if (!val) {
console.log('mapKey: ' + mapKey + ' no longer in guildMap')
return
}
if (val.musicQueue.length == 0)
return;
if (val.currentPlayingTitle)
return;
const qry = val.musicQueue.shift();
const data = await getYoutubeVideoData(qry)
const ytid = data.id;
const title = data.title;
// lag or stuttering? try this first!
// https://groovy.zendesk.com/hc/en-us/articles/360023031772-Laggy-Glitchy-Distorted-No-Audio
val.currentPlayingTitle = title;
val.currentPlayingQuery = qry;
val.musicYTStream = ytdl('https://www.youtube.com/watch?v=' + ytid, {
filter: 'audioonly',
quality: 'highestaudio',
highWaterMark: 1024*1024*10, // 10mb
}, {highWaterMark: 1})
val.musicDispatcher = val.voice_Connection.play(val.musicYTStream);
val.musicDispatcher.on('finish', () => {
val.currentPlayingTitle = val.currentPlayingQuery = null;
queueTryPlayNext(mapKey, cbok, cberr);
});
val.musicDispatcher.on('error', (err) => {
if (err) console.log('musicDispatcher error: ' + err);
console.log(err)
cberr('Error playing <'+title+'>, try again?')
val.currentPlayingTitle = val.currentPlayingQuery = null;
queueTryPlayNext(mapKey, cbok, cberr);
});
val.musicDispatcher.on('start', () => {
cbok(title)
});
} catch (e) {
console.log('queueTryPlayNext: ' + e)
cberr('Error playing, try again?')
if (typeof val !== 'undefined') {
val.currentPlayingTitle = val.currentPlayingQuery = null;
if (val.musicDispatcher) val.musicDispatcher.end();
}
}
}
function addToQueue(title, mapKey) {
let val = guildMap.get(mapKey);
if (val.currentPlayingTitle == title || val.currentPlayingQuery == title || val.musicQueue.includes(title)) {
console.log('duplicate prevented: ' + title)
} else {
val.musicQueue.push(title);
}
}
function skipMusic(mapKey, cbok, cberr) {
let val = guildMap.get(mapKey);
if (!val.currentPlayingTitle) {
cberr('Nothing to skip');
} else {
if (val.musicDispatcher) val.musicDispatcher.end();
cbok()
}
}
function pauseMusic(mapKey, cbok, cberr) {
let val = guildMap.get(mapKey);
if (!val.currentPlayingTitle) {
cberr('Nothing to pause');
} else {
if (val.musicDispatcher) val.musicDispatcher.pause();
cbok()
}
}
function resumeMusic(mapKey, cbok, cberr) {
let val = guildMap.get(mapKey);
if (!val.currentPlayingTitle) {
cberr('Nothing to resume');
} else {
if (val.musicDispatcher) val.musicDispatcher.resume();
cbok()
}
}
function clearQueue(mapKey, cbok, cberr) {
let val = guildMap.get(mapKey);
val.musicQueue = [];
if (val.musicDispatcher) val.musicDispatcher.end();
cbok()
}
function shuffleMusic(mapKey, cbok, cberr) {
let val = guildMap.get(mapKey);
val.musicQueue = shuffle(val.musicQueue);
cbok()
}
//////////////////////////////////////////
//////////////// SPEECH //////////////////
//////////////////////////////////////////
async function transcribe(buffer) {
return transcribe_witai(buffer)
// return transcribe_gspeech(buffer)
}
// WitAI
let witAI_lastcallTS = null;
const witClient = require('node-witai-speech');
async function transcribe_witai(buffer) {
try {
// ensure we do not send more than one request per second
if (witAI_lastcallTS != null) {
let now = Math.floor(new Date());
while (now - witAI_lastcallTS < 1000) {
console.log('sleep')
await sleep(100);
now = Math.floor(new Date());
}
}
} catch (e) {
console.log('transcribe_witai 837:' + e)
}
try {
console.log('transcribe_witai')
const extractSpeechIntent = util.promisify(witClient.extractSpeechIntent);
var stream = Readable.from(buffer);
const contenttype = "audio/raw;encoding=signed-integer;bits=16;rate=48k;endian=little"
const output = await extractSpeechIntent(WITAPIKEY, stream, contenttype)
witAI_lastcallTS = Math.floor(new Date());
console.log(output)
stream.destroy()
if (output && '_text' in output && output._text.length)
return output._text
if (output && 'text' in output && output.text.length)
return output.text
return output;
} catch (e) { console.log('transcribe_witai 851:' + e); console.log(e) }
}
// Google Speech API
// https://cloud.google.com/docs/authentication/production
const gspeech = require('@google-cloud/speech');
const gspeechclient = new gspeech.SpeechClient({
projectId: 'discordbot',
keyFilename: 'gspeech_key.json'
});
async function transcribe_gspeech(buffer) {
try {
console.log('transcribe_gspeech')
const bytes = buffer.toString('base64');
const audio = {
content: bytes,
};
const config = {
encoding: 'LINEAR16',
sampleRateHertz: 48000,
languageCode: 'en-US', // https://cloud.google.com/speech-to-text/docs/languages
};
const request = {
audio: audio,
config: config,
};
const [response] = await gspeechclient.recognize(request);
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`gspeech: ${transcription}`);
return transcription;
} catch (e) { console.log('transcribe_gspeech 368:' + e) }
}
//////////////////////////////////////////
//////////////////////////////////////////
//////////////////////////////////////////
//////////////////////////////////////////
//////////////// YOUTUBE /////////////////
//////////////////////////////////////////
let YT_CACHE = {};
const ytdl = require('ytdl-core');
const getYoutubeID = require('get-youtube-id');
const ytlist = require('youtube-playlist');
const yts = util.promisify(require('yt-search'))
async function searchYoutubeVideo(query) {
const r = await yts(query);
try {
const videos = r.videos
if (!videos.length) {
console.log(query)
throw new Error('videos empty array')
}
const playlists = r.playlists || r.lists
const channels = r.channels || r.accounts
return {id:videos[0].videoId, title:videos[0].title};
} catch (e) {
console.log(r)
console.log('searchYoutubeVideo: ' + e)
throw e;
}
}
function isYoutube(str) {
return str.toLowerCase().indexOf('youtube.com') > -1;
}
function isYoutubePlaylist(str) {
return str.toLowerCase().indexOf('?list=') > -1 || str.toLowerCase().indexOf('&list=') > -1;
}
async function youtube_tracks_from_playlist(url, isretry=false) {
const data = await ytlist(url, 'url');
if (data && 'data' in data && 'playlist' in data.data && data.data.playlist && data.data.playlist.length) {
return data.data.playlist
} else {
if (!isretry) {
console.log('retrying yt playlist processing')
return await youtube_tracks_from_playlist(url, true);
} else {
return null;
}
}
}