forked from healzer/DiscordEarsBot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
406 lines (362 loc) · 12.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
//////////////////////////////////////////
//////////////// 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 { Readable } = require('stream');
//////////////////////////////////////////
///////////////// 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 = 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;
} else {
DISCORD_TOK = process.env.DISCORD_TOK;
WITAPIKEY = process.env.WITAPIKEY;
}
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()
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('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()
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) {
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 = '**COMMANDS:**\n'
out += '```'
out += PREFIX + 'join\n';
out += PREFIX + 'leave\n';
out += '```'
return out;
}
const SILENCE_FRAME = Buffer.from([0xF8, 0xFF, 0xFE]);
class Silence extends Readable {
_read() {
this.push(SILENCE_FRAME);
this.destroy();
}
}
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(new Silence(), { type: 'opus' });
guildMap.set(mapKey, {
'text_Channel': text_Channel,
'voice_Channel': voice_Channel,
'voice_Connection': voice_Connection,
'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);
} 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) {
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) }
}
//////////////////////////////////////////
//////////////////////////////////////////
//////////////////////////////////////////