-
Notifications
You must be signed in to change notification settings - Fork 340
/
Copy pathindex.js
445 lines (403 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
//////////////////////////////////////////
//////////////// LOGGING /////////////////
//////////////////////////////////////////
function getCurrentDateString() {
return (new Date()).toISOString() + ' ::';
};
let __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 { Readable } = require('stream');
const { OpusEncoder } = require('@discordjs/opus');
const https = require('https')
const { Client, IntentsBitField } = require('discord.js')
const { joinVoiceChannel, EndBehaviorType } = require('@discordjs/voice');
const vosk = require('vosk');
const witClient = require('node-witai-speech');
const gspeech = require('@google-cloud/speech');
//////////////////////////////////////////
///////////////// VARIA //////////////////
//////////////////////////////////////////
function necessary_dirs() {
if (!fs.existsSync('./data/')){
fs.mkdirSync('./data/');
}
}
necessary_dirs()
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 = data.filter((el, idx) => idx % 2);
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 WITAI_TOK = null;
let SPEECH_METHOD = 'vosk'; // witai, google, vosk
function loadConfig() {
if (fs.existsSync(SETTINGS_FILE)) {
const CFG_DATA = JSON.parse( fs.readFileSync(SETTINGS_FILE, 'utf8') );
DISCORD_TOK = CFG_DATA.DISCORD_TOK;
WITAI_TOK = CFG_DATA.WITAI_TOK;
SPEECH_METHOD = CFG_DATA.SPEECH_METHOD;
}
DISCORD_TOK = process.env.DISCORD_TOK || DISCORD_TOK;
WITAI_TOK = process.env.WITAI_TOK || WITAI_TOK;
SPEECH_METHOD = process.env.SPEECH_METHOD || SPEECH_METHOD;
if (!['witai', 'google', 'vosk'].includes(SPEECH_METHOD))
throw 'invalid or missing SPEECH_METHOD'
if (!DISCORD_TOK)
throw 'invalid or missing DISCORD_TOK'
if (SPEECH_METHOD === 'witai' && !WITAI_TOK)
throw 'invalid or missing WITAI_TOK'
if (SPEECH_METHOD === 'google' && !fs.existsSync('./gspeech_key.json'))
throw 'missing gspeech_key.json'
}
loadConfig()
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 '+WITAI_TOK,
},
}
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 '+WITAI_TOK,
},
}
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 myIntents = new IntentsBitField();
myIntents.add(
IntentsBitField.Flags.GuildPresences,
IntentsBitField.Flags.GuildVoiceStates,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMessageTyping);
const discordClient = new Client({intents: myIntents})
if (process.env.DEBUG)
discordClient.on('debug', console.debug);
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_DEBUG = PREFIX + 'debug';
const _CMD_TEST = PREFIX + 'hello';
const _CMD_LANG = PREFIX + 'lang';
const guildMap = new Map();
discordClient.on('messageCreate', 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.channel.id) {
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()
guildMap.delete(mapKey)
msg.reply("Disconnected.")
} else {
msg.reply("Cannot leave because not connected.")
}
} 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) {
if (SPEECH_METHOD === 'witai') {
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)
})
}
})
} else if (SPEECH_METHOD === 'vosk') {
let val = guildMap.get(mapKey);
const lang = msg.content.replace(_CMD_LANG, '').trim().toLowerCase()
val.selected_lang = lang;
} else {
msg.reply('Error: this feature is only for Google')
}
}
} 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 = '**COMMANDS:**\n'
out += '```'
out += PREFIX + 'join\n';
out += PREFIX + 'leave\n';
out += PREFIX + 'lang <code>\n';
out += '```'
return out;
}
async function connect(msg, mapKey) {
try {
let voice_Channel = await discordClient.channels.fetch(msg.member.voice.channel.id);
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!");
const voice_Connection = joinVoiceChannel({
channelId: voice_Channel.id,
guildId: voice_Channel.guild.id,
adapterCreator: voice_Channel.guild.voiceAdapterCreator,
selfDeaf: false,
selfMute: true,
});
// voice_Connection.play(new Silence(), { type: 'opus' });
guildMap.set(mapKey, {
'text_Channel': text_Channel,
'voice_Channel': voice_Channel,
'voice_Connection': voice_Connection,
'selected_lang': 'en',
'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;
}
}
let recs = {}
if (SPEECH_METHOD === 'vosk') {
vosk.setLogLevel(-1);
// MODELS: https://alphacephei.com/vosk/models
recs = {
'en': new vosk.Recognizer({model: new vosk.Model('vosk_models/en'), sampleRate: 48000}),
// 'fr': new vosk.Recognizer({model: new vosk.Model('vosk_models/fr'), sampleRate: 48000}),
// 'es': new vosk.Recognizer({model: new vosk.Model('vosk_models/es'), sampleRate: 48000}),
}
// download new models if you need
// dev reference: https://github.com/alphacep/vosk-api/blob/master/nodejs/index.js
}
function speak_impl(voice_Connection, mapKey) {
const receiver = voice_Connection.receiver;
receiver.speaking.on('start', async (userId) => {
const user = discordClient.users.cache.get(userId)
const audioStream = receiver.subscribe(userId, {
end: {
behavior: EndBehaviorType.AfterSilence,
duration: 1000,
},
});
const encoder = new OpusEncoder(48000, 2);
let buffer = [];
audioStream.on("data", chunk => { buffer.push( encoder.decode( chunk ) ) });
audioStream.once("end", async () => {
buffer = Buffer.concat(buffer)
const duration = buffer.length / 48000 / 4;
console.log("duration: " + duration)
if (SPEECH_METHOD === 'witai' || SPEECH_METHOD === 'google') {
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, mapKey);
if (out != null)
process_commands_query(out, mapKey, user);
} catch (e) {
console.log('tmpraw rename: ' + e)
}
});
});
}
function process_commands_query(txt, mapKey, user) {
if (txt && txt.length) {
let val = guildMap.get(mapKey);
val.text_Channel.send(user.username + ': ' + txt)
}
}
//////////////////////////////////////////
//////////////// SPEECH //////////////////
//////////////////////////////////////////
async function transcribe(buffer, mapKey) {
if (SPEECH_METHOD === 'witai') {
return transcribe_witai(buffer)
} else if (SPEECH_METHOD === 'google') {
return transcribe_gspeech(buffer)
} else if (SPEECH_METHOD === 'vosk') {
let val = guildMap.get(mapKey);
recs[val.selected_lang].acceptWaveform(buffer);
let ret = recs[val.selected_lang].result().text;
console.log('vosk:', ret)
return ret;
}
}
// WitAI
let witAI_lastcallTS = null;
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(WITAI_TOK, 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 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) }
}
//////////////////////////////////////////
//////////////////////////////////////////
//////////////////////////////////////////