-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathecho.js
194 lines (156 loc) · 5.3 KB
/
echo.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
"use strict";
// voice echo bot
// records and sends audio using `proxy` mode (without decoding/encoding/saving anything)
// variable `var recordTime = 10;` controls duration of recording in seconds
// commands:
// ~!~echo - joins voice channel and records audio, then plays recorded audio back
// ~!~stop - stops recording/playing
var fs = require('fs');
var Discordie;
try { Discordie = require("../"); } catch(e) {}
try { Discordie = require("discordie"); } catch(e) {}
var client = new Discordie({autoReconnect: true});
var auth = { token: "<BOT-TOKEN>" };
try { auth = require("./auth"); } catch(e) {}
client.connect(auth);
var recordTime = 10;
var recordTimer = null;
var recordReply = null;
var recordStartTime = null;
var recordedData = [];
var recordingUser = null;
client.Dispatcher.on("MESSAGE_CREATE", e => {
console.log("new message: ");
console.log(JSON.stringify(e.message, null, " "));
console.log("e.message.content: " + e.message.content);
const content = e.message.content;
const guild = e.message.channel.guild;
if (content == "~!~echo") {
if (recordingUser)
return e.message.reply("Already recording");
const channelToJoin = e.message.author.getVoiceChannel(guild);
if (!channelToJoin)
return e.message.reply("Join a voice channel first");
channelToJoin.join().then(info => {
recordReply = e.message.reply.bind(e.message);
e.message.reply("Recording audio for " + recordTime + " seconds");
recordedData = [];
recordingUser = e.message.author.id;
recordStartTime = Date.now();
recordTimer = setTimeout(() => {
play(info);
e.message.reply("Playing");
}, recordTime * 1000);
});
}
if (content == "~!~stop") {
e.message.reply("Stopped");
stopPlaying = true;
recordingUser = false;
if (recordTimer) {
clearTimeout(recordTimer);
recordTimer = null;
}
}
});
client.Dispatcher.on("VOICE_CONNECTED", e => {
e.voiceConnection.getDecoder()
.onPacket = (packet) => {
const user = e.voiceConnection.ssrcToMember(packet.ssrc);
if (!user) return;
if (user.id != recordingUser) return;
packet.playbackTime = Date.now() - recordStartTime;
recordedData.push(packet);
const name = user ? user.username : "<unknown>";
console.log(
"recording " + packet.chunk.length +
" bytes from " + name +
" @ " + packet.timestamp
);
};
// set callback on `.onPacketDecoded` to enable decoding and
// have decoded audio in `packet.chunk`
});
var stopPlaying = false;
function play(info) {
stopPlaying = false;
var playbackStartTime = Date.now();
if (!client.VoiceConnections.length)
return console.log("Voice not connected");
if (!info) info = client.VoiceConnections[0];
var voiceConnection = info.voiceConnection;
var encoder = voiceConnection.getEncoder({ frameDuration: 20, proxy: true });
function sendPacket() {
var packet = recordedData[0];
if (!packet && recordReply) {
recordReply("Finished playing");
}
if (!packet || stopPlaying) {
recordingUser = null;
return;
}
var currentTime = (Date.now() - playbackStartTime);
var nextTime = packet.playbackTime - currentTime;
setTimeout(sendPacket, nextTime);
if (currentTime < nextTime) return;
recordedData.shift(packet);
var numSamples = opus_packet_get_samples_per_frame(packet.chunk);
console.log("playing ", packet.chunk.length, numSamples);
encoder.enqueue(packet.chunk, numSamples);
}
sendPacket();
}
client.Dispatcher.onAny((type, args) => {
console.log("\nevent "+type);
if (args.type == "READY" || args.type == "READY" ||
type == "GATEWAY_READY" || type == "ANY_GATEWAY_READY" ||
type == "GATEWAY_DISPATCH") {
return console.log("e " + (args.type || type));
}
console.log("args " + JSON.stringify(args));
});
// ===========================================================================
// Opus helper functions
// ===========================================================================
const Constants = {
OPUS_BAD_ARG: -1,
OPUS_INVALID_PACKET: -4
};
function opus_packet_get_samples_per_frame(packet, sampleRate) {
sampleRate = sampleRate || 48000;
let audiosize;
if (packet[0] & 0x80) {
audiosize = ((packet[0] >> 3) & 0x3);
audiosize = (sampleRate << audiosize) / 400;
} else if ((packet[0] & 0x60) == 0x60) {
audiosize = (packet[0] & 0x08) ? sampleRate / 50 : sampleRate / 100;
} else {
audiosize = ((packet[0] >> 3) & 0x3);
if (audiosize == 3) {
audiosize = sampleRate * 60 / 1000;
} else {
audiosize = (sampleRate << audiosize) / 100;
}
}
return audiosize;
}
function opus_packet_get_nb_frames(packet) {
var count;
if (packet.length < 1) return Constants.OPUS_BAD_ARG;
count = packet[0] & 0x3;
if (count == 0) return 1;
else if (count != 3) return 2;
else if (packet.length < 2) return Constants.OPUS_INVALID_PACKET;
else return packet[1] & 0x3F;
}
function opus_packet_get_nb_samples(packet, sampleRate)
{
sampleRate = sampleRate || 48000;
var count = opus_packet_get_nb_frames(packet);
if (count < 0) return count;
var samples = count * opus_packet_get_samples_per_frame(packet, sampleRate);
/* Can't have more than 120 ms */
if (samples * 25 > sampleRate * 3)
return Constants.OPUS_INVALID_PACKET;
return samples;
}