-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.js
179 lines (149 loc) · 5.11 KB
/
main.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
'use strict';
const fs = require('fs');
const path = require('path');
const Discord = require('discord.js');
const opus = require('node-opus');
const datapath = 'voicedata';
const rate = 48000;
const frame_size = 1920;
const channels = 2;
const pythonapp = 'bot_conversation.py'
const token = '';
const child = require('child_process');
let voiceConnections = new Map();
let voiceReceivers = new Map();
let writeStreams = new Map();
let client = new Discord.Client()
let textChannel;
client.on('ready', () => {
console.log("Started!");
});
client.on('message', (msg) => {
if (msg.content.charAt(0) === '!') {
switch (msg.content.slice(1)) {
case 'on':
textChannel = msg.channel;
start(msg.member);
break;
case 'off':
stop(msg.member);
break;
}
}
});
client.on('guildMemberSpeaking', (member, speaking) => {
if (!speaking && member.voiceChannel) {
let receiver = voiceReceivers.get(member.voiceChannelID);
if (receiver) {
let writeStream = writeStreams.get(member.id);
if (writeStream) {
writeStreams.delete(member.id);
writeStream.end((err) => {
if (err) {
console.error(err);
} else {
let pcmpath = writeStream.path.replace(".opus_hex", ".pcm_raw");
save(writeStream.path, pcmpath, member);
}
});
}
}
}
});
let start = (member) => {
if (!member || !member.voiceChannel) {
return;
}
member.voiceChannel.join().then((voiceConnection) => {
console.log("Recording...");
voiceConnections.set(member.voiceChannelID, voiceConnection);
let voiceReceiver = voiceConnection.createReceiver();
voiceReceiver.on('opus', (user, data) => {
let hexString = data.toString('hex');
let writeStream = writeStreams.get(user.id);
if (!writeStream) {
if (hexString === 'f8fffe') {
return;
}
let outputPath = path.join(datapath, `${Date.now()}.opus_hex`);
writeStream = fs.createWriteStream(outputPath);
writeStreams.set(user.id, writeStream);
}
writeStream.write(`,${hexString}`);
});
voiceReceivers.set(member.voiceChannelID, voiceReceiver);
}).catch(console.error);
}
let stop = (member) => {
if (!member || !member.voiceChannel) {
return;
}
console.log("Stopping...");
if (voiceReceivers.get(member.voiceChannelID)) {
voiceReceivers.get(member.voiceChannelID).destroy();
voiceReceivers.delete(member.voiceChannelID);
voiceConnections.get(member.voiceChannelID).disconnect();
voiceConnections.delete(member.voiceChannelID);
}
}
let save = (inputPath, filename, member) => {
let encoder = new opus.OpusEncoder(rate, channels);
const inputStream = fs.createReadStream(inputPath);
const outputStream = fs.createWriteStream(filename);
let data = '';
inputStream.on('data', chunk => {
data += chunk.toString();
const frames = data.split(',');
if (frames.length) {
data = frames.pop();
}
for (let frame of frames) {
if (frame !== '') {
const decodedBuffer = getDecodedFrame(frame, encoder, filename);
if (decodedBuffer) {
outputStream.write(decodedBuffer);
}
}
}
});
inputStream.on('end', () => {
outputStream.end((err) => {
if (err) {
console.error(err);
} else {
let py = child.spawn('python3', [pythonapp, 'open', filename, ""+member.id, ""+Date.now()]);
py.stdout.on('data', function(data) {
let content = data.toString().trim();
console.log(content);
if (content.startsWith("msg")) {
console.log(">> sending message");
textChannel.send(content.slice(4));
} else if (content.startsWith("play")) {
console.log(">> playing audio");
playFile(member, "./" + datapath + "/" + content.slice(5));
}
});
}
});
});
};
let getDecodedFrame = (frameString, encoder, filename) => {
let buffer = Buffer.from(frameString, 'hex');
try {
buffer = encoder.decode(buffer, frame_size);
} catch (err) {
try {
buffer = encoder.decode(buffer.slice(8), frame_size);
} catch (err) {
console.log(`${filename} was unable to be decoded`);
return null;
}
}
return buffer;
};
let playFile = (member, filename) => {
let voiceConnection = voiceConnections.get(member.voiceChannelID)
let disp = voiceConnection.playFile(filename);
disp.on("end", (end) => {});
}
client.login(token).catch(console.error);