discord.js-selfv13 13.0.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of discord.js-selfv13 might be problematic. Click here for more details.
- package/.dccache +1 -0
- package/LICENSE +190 -0
- package/README.md +87 -0
- package/browser.js +9 -0
- package/deploy/deploy-key.enc +0 -0
- package/deploy/deploy.sh +90 -0
- package/deploy/test.sh +34 -0
- package/docs/README.md +1 -0
- package/docs/examples/attachments.md +163 -0
- package/docs/examples/avatars.js +29 -0
- package/docs/examples/embed.js +38 -0
- package/docs/examples/greeting.js +30 -0
- package/docs/examples/moderation.md +145 -0
- package/docs/examples/ping.js +29 -0
- package/docs/examples/webhook.js +12 -0
- package/docs/general/faq.md +23 -0
- package/docs/general/updating.md +181 -0
- package/docs/general/welcome.md +95 -0
- package/docs/index.yml +30 -0
- package/docs/logo.svg +19 -0
- package/docs/topics/voice.md +113 -0
- package/docs/topics/web.md +38 -0
- package/package.json +147 -0
- package/src/client/Client.js +564 -0
- package/src/client/ClientDataManager.js +150 -0
- package/src/client/ClientDataResolver.js +376 -0
- package/src/client/ClientManager.js +74 -0
- package/src/client/WebhookClient.js +118 -0
- package/src/client/actions/Action.js +23 -0
- package/src/client/actions/ActionsManager.js +40 -0
- package/src/client/actions/ChannelCreate.js +11 -0
- package/src/client/actions/ChannelDelete.js +30 -0
- package/src/client/actions/ChannelUpdate.js +74 -0
- package/src/client/actions/GuildBanRemove.js +13 -0
- package/src/client/actions/GuildChannelsPositionUpdate.js +19 -0
- package/src/client/actions/GuildDelete.js +57 -0
- package/src/client/actions/GuildEmojiCreate.js +17 -0
- package/src/client/actions/GuildEmojiDelete.js +18 -0
- package/src/client/actions/GuildEmojiUpdate.js +17 -0
- package/src/client/actions/GuildEmojisUpdate.js +38 -0
- package/src/client/actions/GuildMemberGet.js +10 -0
- package/src/client/actions/GuildMemberRemove.js +41 -0
- package/src/client/actions/GuildRoleCreate.js +26 -0
- package/src/client/actions/GuildRoleDelete.js +42 -0
- package/src/client/actions/GuildRoleUpdate.js +41 -0
- package/src/client/actions/GuildRolesPositionUpdate.js +19 -0
- package/src/client/actions/GuildSync.js +29 -0
- package/src/client/actions/GuildUpdate.js +34 -0
- package/src/client/actions/MessageCreate.js +53 -0
- package/src/client/actions/MessageDelete.js +35 -0
- package/src/client/actions/MessageDeleteBulk.js +26 -0
- package/src/client/actions/MessageReactionAdd.js +37 -0
- package/src/client/actions/MessageReactionRemove.js +37 -0
- package/src/client/actions/MessageReactionRemoveAll.js +25 -0
- package/src/client/actions/MessageUpdate.js +40 -0
- package/src/client/actions/Ready.js +1 -0
- package/src/client/actions/Ready.js.bak +65 -0
- package/src/client/actions/UserGet.js +11 -0
- package/src/client/actions/UserNoteUpdate.js +30 -0
- package/src/client/actions/UserUpdate.js +33 -0
- package/src/client/rest/APIRequest.js +56 -0
- package/src/client/rest/DiscordAPIError.js +60 -0
- package/src/client/rest/RESTManager.js +58 -0
- package/src/client/rest/RESTMethods.js +1006 -0
- package/src/client/rest/RequestHandlers/Burst.js +90 -0
- package/src/client/rest/RequestHandlers/RequestHandler.js +54 -0
- package/src/client/rest/RequestHandlers/Sequential.js +132 -0
- package/src/client/rest/UserAgentManager.js +25 -0
- package/src/client/voice/ClientVoiceManager.js +81 -0
- package/src/client/voice/VoiceBroadcast.js +366 -0
- package/src/client/voice/VoiceConnection.js +530 -0
- package/src/client/voice/VoiceUDPClient.js +127 -0
- package/src/client/voice/VoiceWebSocket.js +246 -0
- package/src/client/voice/dispatcher/StreamDispatcher.js +331 -0
- package/src/client/voice/opus/BaseOpusEngine.js +60 -0
- package/src/client/voice/opus/NodeOpusEngine.js +40 -0
- package/src/client/voice/opus/OpusEngineList.js +28 -0
- package/src/client/voice/opus/OpusScriptEngine.js +45 -0
- package/src/client/voice/player/AudioPlayer.js +170 -0
- package/src/client/voice/receiver/VoiceReadable.js +17 -0
- package/src/client/voice/receiver/VoiceReceiver.js +219 -0
- package/src/client/voice/util/SecretKey.js +16 -0
- package/src/client/voice/util/Secretbox.js +33 -0
- package/src/client/voice/util/VolumeInterface.js +86 -0
- package/src/client/websocket/WebSocketConnection.js +506 -0
- package/src/client/websocket/WebSocketManager.js +90 -0
- package/src/client/websocket/packets/WebSocketPacketManager.js +110 -0
- package/src/client/websocket/packets/handlers/AbstractHandler.js +11 -0
- package/src/client/websocket/packets/handlers/ChannelCreate.js +17 -0
- package/src/client/websocket/packets/handlers/ChannelDelete.js +20 -0
- package/src/client/websocket/packets/handlers/ChannelPinsUpdate.js +37 -0
- package/src/client/websocket/packets/handlers/ChannelUpdate.js +11 -0
- package/src/client/websocket/packets/handlers/GuildBanAdd.js +23 -0
- package/src/client/websocket/packets/handlers/GuildBanRemove.js +20 -0
- package/src/client/websocket/packets/handlers/GuildCreate.js +22 -0
- package/src/client/websocket/packets/handlers/GuildDelete.js +19 -0
- package/src/client/websocket/packets/handlers/GuildEmojisUpdate.js +11 -0
- package/src/client/websocket/packets/handlers/GuildIntegrationsUpdate.js +19 -0
- package/src/client/websocket/packets/handlers/GuildMemberAdd.js +17 -0
- package/src/client/websocket/packets/handlers/GuildMemberRemove.js +13 -0
- package/src/client/websocket/packets/handlers/GuildMemberUpdate.js +18 -0
- package/src/client/websocket/packets/handlers/GuildMembersChunk.js +33 -0
- package/src/client/websocket/packets/handlers/GuildRoleCreate.js +11 -0
- package/src/client/websocket/packets/handlers/GuildRoleDelete.js +11 -0
- package/src/client/websocket/packets/handlers/GuildRoleUpdate.js +11 -0
- package/src/client/websocket/packets/handlers/GuildSync.js +11 -0
- package/src/client/websocket/packets/handlers/GuildUpdate.js +11 -0
- package/src/client/websocket/packets/handlers/MessageCreate.js +19 -0
- package/src/client/websocket/packets/handlers/MessageDelete.js +19 -0
- package/src/client/websocket/packets/handlers/MessageDeleteBulk.js +17 -0
- package/src/client/websocket/packets/handlers/MessageReactionAdd.js +11 -0
- package/src/client/websocket/packets/handlers/MessageReactionRemove.js +11 -0
- package/src/client/websocket/packets/handlers/MessageReactionRemoveAll.js +11 -0
- package/src/client/websocket/packets/handlers/MessageUpdate.js +11 -0
- package/src/client/websocket/packets/handlers/PresenceUpdate.js +76 -0
- package/src/client/websocket/packets/handlers/Ready.js +83 -0
- package/src/client/websocket/packets/handlers/RelationshipAdd.js +19 -0
- package/src/client/websocket/packets/handlers/RelationshipRemove.js +19 -0
- package/src/client/websocket/packets/handlers/Resumed.js +28 -0
- package/src/client/websocket/packets/handlers/TypingStart.js +68 -0
- package/src/client/websocket/packets/handlers/UserGuildSettingsUpdate.js +21 -0
- package/src/client/websocket/packets/handlers/UserNoteUpdate.js +12 -0
- package/src/client/websocket/packets/handlers/UserSettingsUpdate.js +18 -0
- package/src/client/websocket/packets/handlers/UserUpdate.js +11 -0
- package/src/client/websocket/packets/handlers/VoiceServerUpdate.js +19 -0
- package/src/client/websocket/packets/handlers/VoiceStateUpdate.js +52 -0
- package/src/client/websocket/packets/handlers/WebhooksUpdate.js +19 -0
- package/src/index.js +66 -0
- package/src/sharding/Shard.js +282 -0
- package/src/sharding/ShardClientUtil.js +146 -0
- package/src/sharding/ShardingManager.js +220 -0
- package/src/structures/Attachment.js +75 -0
- package/src/structures/CategoryChannel.js +22 -0
- package/src/structures/Channel.js +78 -0
- package/src/structures/ClientUser.js +447 -0
- package/src/structures/ClientUserChannelOverride.js +30 -0
- package/src/structures/ClientUserGuildSettings.js +60 -0
- package/src/structures/ClientUserSettings.js +80 -0
- package/src/structures/DMChannel.js +76 -0
- package/src/structures/Emoji.js +256 -0
- package/src/structures/GroupDMChannel.js +246 -0
- package/src/structures/Guild.js +1461 -0
- package/src/structures/GuildAuditLogs.js +371 -0
- package/src/structures/GuildChannel.js +537 -0
- package/src/structures/GuildMember.js +613 -0
- package/src/structures/Invite.js +164 -0
- package/src/structures/Message.js +605 -0
- package/src/structures/MessageAttachment.js +68 -0
- package/src/structures/MessageCollector.js +100 -0
- package/src/structures/MessageEmbed.js +386 -0
- package/src/structures/MessageMentions.js +144 -0
- package/src/structures/MessageReaction.js +96 -0
- package/src/structures/NewsChannel.js +24 -0
- package/src/structures/OAuth2Application.js +148 -0
- package/src/structures/PartialGuild.js +51 -0
- package/src/structures/PartialGuildChannel.js +44 -0
- package/src/structures/PermissionOverwrites.js +69 -0
- package/src/structures/Presence.js +241 -0
- package/src/structures/ReactionCollector.js +85 -0
- package/src/structures/ReactionEmoji.js +49 -0
- package/src/structures/RichEmbed.js +295 -0
- package/src/structures/Role.js +376 -0
- package/src/structures/StoreChannel.js +25 -0
- package/src/structures/TextChannel.js +154 -0
- package/src/structures/User.js +329 -0
- package/src/structures/UserConnection.js +48 -0
- package/src/structures/UserProfile.js +62 -0
- package/src/structures/VoiceChannel.js +146 -0
- package/src/structures/VoiceRegion.js +50 -0
- package/src/structures/Webhook.js +304 -0
- package/src/structures/interfaces/Collector.js +179 -0
- package/src/structures/interfaces/TextBasedChannel.js +635 -0
- package/src/structures/shared/resolvePermissions.js +26 -0
- package/src/util/Collection.js +532 -0
- package/src/util/Constants.js +845 -0
- package/src/util/Permissions.js +306 -0
- package/src/util/Snowflake.js +82 -0
- package/src/util/Util.js +221 -0
- package/test/random.js +207 -0
- package/test/shard.js +31 -0
- package/test/sharder.js +7 -0
- package/test/voice.js +78 -0
- package/test/webpack.html +31 -0
- package/tsconfig.json +13 -0
- package/tslint.json +62 -0
- package/typings/discord.js-test.ts +69 -0
- package/typings/index.d.ts +2190 -0
- package/webpack.config.js +62 -0
@@ -0,0 +1,530 @@
|
|
1
|
+
const VoiceWebSocket = require('./VoiceWebSocket');
|
2
|
+
const VoiceUDP = require('./VoiceUDPClient');
|
3
|
+
const Util = require('../../util/Util');
|
4
|
+
const Constants = require('../../util/Constants');
|
5
|
+
const AudioPlayer = require('./player/AudioPlayer');
|
6
|
+
const VoiceReceiver = require('./receiver/VoiceReceiver');
|
7
|
+
const EventEmitter = require('events').EventEmitter;
|
8
|
+
const Prism = require('prism-media');
|
9
|
+
|
10
|
+
/**
|
11
|
+
* Represents a connection to a guild's voice server.
|
12
|
+
* ```js
|
13
|
+
* // Obtained using:
|
14
|
+
* voiceChannel.join()
|
15
|
+
* .then(connection => {
|
16
|
+
*
|
17
|
+
* });
|
18
|
+
* ```
|
19
|
+
* @extends {EventEmitter}
|
20
|
+
*/
|
21
|
+
class VoiceConnection extends EventEmitter {
|
22
|
+
constructor(voiceManager, channel) {
|
23
|
+
super();
|
24
|
+
|
25
|
+
/**
|
26
|
+
* The voice manager that instantiated this connection
|
27
|
+
* @type {ClientVoiceManager}
|
28
|
+
*/
|
29
|
+
this.voiceManager = voiceManager;
|
30
|
+
|
31
|
+
/**
|
32
|
+
* The client that instantiated this connection
|
33
|
+
* @type {Client}
|
34
|
+
*/
|
35
|
+
this.client = voiceManager.client;
|
36
|
+
|
37
|
+
/**
|
38
|
+
* @external Prism
|
39
|
+
* @see {@link https://github.com/hydrabolt/prism-media}
|
40
|
+
*/
|
41
|
+
|
42
|
+
/**
|
43
|
+
* The audio transcoder for this connection
|
44
|
+
* @type {Prism}
|
45
|
+
*/
|
46
|
+
this.prism = new Prism();
|
47
|
+
|
48
|
+
/**
|
49
|
+
* The voice channel this connection is currently serving
|
50
|
+
* @type {VoiceChannel}
|
51
|
+
*/
|
52
|
+
this.channel = channel;
|
53
|
+
|
54
|
+
/**
|
55
|
+
* The current status of the voice connection
|
56
|
+
* @type {number}
|
57
|
+
*/
|
58
|
+
this.status = Constants.VoiceStatus.AUTHENTICATING;
|
59
|
+
|
60
|
+
/**
|
61
|
+
* Whether we're currently transmitting audio
|
62
|
+
* @type {boolean}
|
63
|
+
*/
|
64
|
+
this.speaking = false;
|
65
|
+
|
66
|
+
/**
|
67
|
+
* An array of Voice Receivers that have been created for this connection
|
68
|
+
* @type {VoiceReceiver[]}
|
69
|
+
*/
|
70
|
+
this.receivers = [];
|
71
|
+
|
72
|
+
/**
|
73
|
+
* The authentication data needed to connect to the voice server
|
74
|
+
* @type {Object}
|
75
|
+
* @private
|
76
|
+
*/
|
77
|
+
this.authentication = {};
|
78
|
+
|
79
|
+
/**
|
80
|
+
* The audio player for this voice connection
|
81
|
+
* @type {AudioPlayer}
|
82
|
+
*/
|
83
|
+
this.player = new AudioPlayer(this);
|
84
|
+
|
85
|
+
this.player.on('debug', m => {
|
86
|
+
/**
|
87
|
+
* Debug info from the connection.
|
88
|
+
* @event VoiceConnection#debug
|
89
|
+
* @param {string} message The debug message
|
90
|
+
*/
|
91
|
+
this.emit('debug', `audio player - ${m}`);
|
92
|
+
});
|
93
|
+
|
94
|
+
this.player.on('error', e => {
|
95
|
+
/**
|
96
|
+
* Warning info from the connection.
|
97
|
+
* @event VoiceConnection#warn
|
98
|
+
* @param {string|Error} warning The warning
|
99
|
+
*/
|
100
|
+
this.emit('warn', e);
|
101
|
+
});
|
102
|
+
|
103
|
+
/**
|
104
|
+
* Map SSRC to speaking values
|
105
|
+
* @type {Map<number, boolean>}
|
106
|
+
* @private
|
107
|
+
*/
|
108
|
+
this.ssrcMap = new Map();
|
109
|
+
|
110
|
+
/**
|
111
|
+
* Object that wraps contains the `ws` and `udp` sockets of this voice connection
|
112
|
+
* @type {Object}
|
113
|
+
* @private
|
114
|
+
*/
|
115
|
+
this.sockets = {};
|
116
|
+
|
117
|
+
this.authenticate();
|
118
|
+
}
|
119
|
+
|
120
|
+
/**
|
121
|
+
* The current stream dispatcher (if any)
|
122
|
+
* @type {?StreamDispatcher}
|
123
|
+
* @readonly
|
124
|
+
*/
|
125
|
+
get dispatcher() {
|
126
|
+
return this.player.dispatcher;
|
127
|
+
}
|
128
|
+
|
129
|
+
/**
|
130
|
+
* Sets whether the voice connection should display as "speaking" or not.
|
131
|
+
* @param {boolean} value Whether or not to speak
|
132
|
+
* @private
|
133
|
+
*/
|
134
|
+
setSpeaking(value) {
|
135
|
+
if (this.speaking === value) return;
|
136
|
+
if (this.status !== Constants.VoiceStatus.CONNECTED) return;
|
137
|
+
this.speaking = value;
|
138
|
+
this.sockets.ws.sendPacket({
|
139
|
+
op: Constants.VoiceOPCodes.SPEAKING,
|
140
|
+
d: {
|
141
|
+
speaking: true,
|
142
|
+
delay: 0,
|
143
|
+
},
|
144
|
+
}).catch(e => {
|
145
|
+
this.emit('debug', e);
|
146
|
+
});
|
147
|
+
}
|
148
|
+
|
149
|
+
/**
|
150
|
+
* Sends a request to the main gateway to join a voice channel.
|
151
|
+
* @param {Object} [options] The options to provide
|
152
|
+
*/
|
153
|
+
sendVoiceStateUpdate(options = {}) {
|
154
|
+
options = Util.mergeDefault({
|
155
|
+
guild_id: this.channel.guild.id,
|
156
|
+
channel_id: this.channel.id,
|
157
|
+
self_mute: false,
|
158
|
+
self_deaf: false,
|
159
|
+
}, options);
|
160
|
+
|
161
|
+
this.client.ws.send({
|
162
|
+
op: Constants.OPCodes.VOICE_STATE_UPDATE,
|
163
|
+
d: options,
|
164
|
+
});
|
165
|
+
}
|
166
|
+
|
167
|
+
/**
|
168
|
+
* Set the token and endpoint required to connect to the voice servers.
|
169
|
+
* @param {string} token The voice token
|
170
|
+
* @param {string} endpoint The voice endpoint
|
171
|
+
* @returns {void}
|
172
|
+
*/
|
173
|
+
setTokenAndEndpoint(token, endpoint) {
|
174
|
+
if (!endpoint) {
|
175
|
+
// Signifies awaiting endpoint stage
|
176
|
+
return;
|
177
|
+
}
|
178
|
+
|
179
|
+
if (!token) {
|
180
|
+
this.authenticateFailed('Token not provided from voice server packet.');
|
181
|
+
return;
|
182
|
+
}
|
183
|
+
|
184
|
+
endpoint = endpoint.match(/([^:]*)/)[0];
|
185
|
+
|
186
|
+
if (!endpoint) {
|
187
|
+
this.authenticateFailed('Invalid endpoint received.');
|
188
|
+
return;
|
189
|
+
}
|
190
|
+
|
191
|
+
if (this.status === Constants.VoiceStatus.AUTHENTICATING) {
|
192
|
+
this.authentication.token = token;
|
193
|
+
this.authentication.endpoint = endpoint;
|
194
|
+
this.checkAuthenticated();
|
195
|
+
} else if (token !== this.authentication.token || endpoint !== this.authentication.endpoint) {
|
196
|
+
this.reconnect(token, endpoint);
|
197
|
+
}
|
198
|
+
}
|
199
|
+
|
200
|
+
/**
|
201
|
+
* Sets the Session ID for the connection.
|
202
|
+
* @param {string} sessionID The voice session ID
|
203
|
+
*/
|
204
|
+
setSessionID(sessionID) {
|
205
|
+
if (!sessionID) {
|
206
|
+
this.authenticateFailed('Session ID not supplied.');
|
207
|
+
return;
|
208
|
+
}
|
209
|
+
|
210
|
+
if (this.status === Constants.VoiceStatus.AUTHENTICATING) {
|
211
|
+
this.authentication.sessionID = sessionID;
|
212
|
+
this.checkAuthenticated();
|
213
|
+
} else if (sessionID !== this.authentication.sessionID) {
|
214
|
+
this.authentication.sessionID = sessionID;
|
215
|
+
/**
|
216
|
+
* Emitted when a new session ID is received.
|
217
|
+
* @event VoiceConnection#newSession
|
218
|
+
* @private
|
219
|
+
*/
|
220
|
+
this.emit('newSession', sessionID);
|
221
|
+
}
|
222
|
+
}
|
223
|
+
|
224
|
+
/**
|
225
|
+
* Checks whether the voice connection is authenticated.
|
226
|
+
* @private
|
227
|
+
*/
|
228
|
+
checkAuthenticated() {
|
229
|
+
const { token, endpoint, sessionID } = this.authentication;
|
230
|
+
|
231
|
+
if (token && endpoint && sessionID) {
|
232
|
+
clearTimeout(this.connectTimeout);
|
233
|
+
this.status = Constants.VoiceStatus.CONNECTING;
|
234
|
+
/**
|
235
|
+
* Emitted when we successfully initiate a voice connection.
|
236
|
+
* @event VoiceConnection#authenticated
|
237
|
+
*/
|
238
|
+
this.emit('authenticated');
|
239
|
+
this.connect();
|
240
|
+
}
|
241
|
+
}
|
242
|
+
|
243
|
+
/**
|
244
|
+
* Invoked when we fail to initiate a voice connection.
|
245
|
+
* @param {string} reason The reason for failure
|
246
|
+
* @private
|
247
|
+
*/
|
248
|
+
authenticateFailed(reason) {
|
249
|
+
clearTimeout(this.connectTimeout);
|
250
|
+
if (this.status === Constants.VoiceStatus.AUTHENTICATING) {
|
251
|
+
/**
|
252
|
+
* Emitted when we fail to initiate a voice connection.
|
253
|
+
* @event VoiceConnection#failed
|
254
|
+
* @param {Error} error The encountered error
|
255
|
+
*/
|
256
|
+
this.emit('failed', new Error(reason));
|
257
|
+
} else {
|
258
|
+
/**
|
259
|
+
* Emitted whenever the connection encounters an error.
|
260
|
+
* @event VoiceConnection#error
|
261
|
+
* @param {Error} error The encountered error
|
262
|
+
*/
|
263
|
+
this.emit('error', new Error(reason));
|
264
|
+
}
|
265
|
+
this.status = Constants.VoiceStatus.DISCONNECTED;
|
266
|
+
}
|
267
|
+
|
268
|
+
/**
|
269
|
+
* Move to a different voice channel in the same guild.
|
270
|
+
* @param {VoiceChannel} channel The channel to move to
|
271
|
+
* @private
|
272
|
+
*/
|
273
|
+
updateChannel(channel) {
|
274
|
+
this.channel = channel;
|
275
|
+
this.sendVoiceStateUpdate();
|
276
|
+
}
|
277
|
+
|
278
|
+
/**
|
279
|
+
* Attempts to authenticate to the voice server.
|
280
|
+
* @private
|
281
|
+
*/
|
282
|
+
authenticate() {
|
283
|
+
this.sendVoiceStateUpdate();
|
284
|
+
this.connectTimeout = this.client.setTimeout(
|
285
|
+
() => this.authenticateFailed(new Error('Connection not established within 15 seconds.')), 15000);
|
286
|
+
}
|
287
|
+
|
288
|
+
/**
|
289
|
+
* Attempts to reconnect to the voice server (typically after a region change).
|
290
|
+
* @param {string} token The voice token
|
291
|
+
* @param {string} endpoint The voice endpoint
|
292
|
+
* @private
|
293
|
+
*/
|
294
|
+
reconnect(token, endpoint) {
|
295
|
+
this.authentication.token = token;
|
296
|
+
this.authentication.endpoint = endpoint;
|
297
|
+
|
298
|
+
this.status = Constants.VoiceStatus.RECONNECTING;
|
299
|
+
/**
|
300
|
+
* Emitted when the voice connection is reconnecting (typically after a region change).
|
301
|
+
* @event VoiceConnection#reconnecting
|
302
|
+
*/
|
303
|
+
this.emit('reconnecting');
|
304
|
+
this.connect();
|
305
|
+
}
|
306
|
+
|
307
|
+
/**
|
308
|
+
* Disconnect the voice connection, causing a disconnect and closing event to be emitted.
|
309
|
+
*/
|
310
|
+
disconnect() {
|
311
|
+
this.emit('closing');
|
312
|
+
this.sendVoiceStateUpdate({
|
313
|
+
channel_id: null,
|
314
|
+
});
|
315
|
+
this.player.destroy();
|
316
|
+
this.cleanup();
|
317
|
+
this.status = Constants.VoiceStatus.DISCONNECTED;
|
318
|
+
/**
|
319
|
+
* Emitted when the voice connection disconnects.
|
320
|
+
* @event VoiceConnection#disconnect
|
321
|
+
*/
|
322
|
+
this.emit('disconnect');
|
323
|
+
}
|
324
|
+
|
325
|
+
/**
|
326
|
+
* Cleans up after disconnect.
|
327
|
+
* @private
|
328
|
+
*/
|
329
|
+
cleanup() {
|
330
|
+
const { ws, udp } = this.sockets;
|
331
|
+
|
332
|
+
if (ws) {
|
333
|
+
ws.removeAllListeners('error');
|
334
|
+
ws.removeAllListeners('ready');
|
335
|
+
ws.removeAllListeners('sessionDescription');
|
336
|
+
ws.removeAllListeners('speaking');
|
337
|
+
}
|
338
|
+
|
339
|
+
if (udp) udp.removeAllListeners('error');
|
340
|
+
|
341
|
+
this.sockets.ws = null;
|
342
|
+
this.sockets.udp = null;
|
343
|
+
}
|
344
|
+
|
345
|
+
/**
|
346
|
+
* Connect the voice connection.
|
347
|
+
* @private
|
348
|
+
*/
|
349
|
+
connect() {
|
350
|
+
if (this.status !== Constants.VoiceStatus.RECONNECTING) {
|
351
|
+
if (this.sockets.ws) throw new Error('There is already an existing WebSocket connection.');
|
352
|
+
if (this.sockets.udp) throw new Error('There is already an existing UDP connection.');
|
353
|
+
}
|
354
|
+
|
355
|
+
if (this.sockets.ws) this.sockets.ws.shutdown();
|
356
|
+
if (this.sockets.udp) this.sockets.udp.shutdown();
|
357
|
+
|
358
|
+
this.sockets.ws = new VoiceWebSocket(this);
|
359
|
+
this.sockets.udp = new VoiceUDP(this);
|
360
|
+
|
361
|
+
const { ws, udp } = this.sockets;
|
362
|
+
|
363
|
+
ws.on('error', err => this.emit('error', err));
|
364
|
+
udp.on('error', err => this.emit('error', err));
|
365
|
+
ws.on('ready', this.onReady.bind(this));
|
366
|
+
ws.on('sessionDescription', this.onSessionDescription.bind(this));
|
367
|
+
ws.on('speaking', this.onSpeaking.bind(this));
|
368
|
+
}
|
369
|
+
|
370
|
+
/**
|
371
|
+
* Invoked when the voice websocket is ready.
|
372
|
+
* @param {Object} data The received data
|
373
|
+
* @private
|
374
|
+
*/
|
375
|
+
onReady({ port, ssrc, ip }) {
|
376
|
+
this.authentication.port = port;
|
377
|
+
this.authentication.ssrc = ssrc;
|
378
|
+
this.sockets.udp.createUDPSocket(ip);
|
379
|
+
}
|
380
|
+
|
381
|
+
/**
|
382
|
+
* Invoked when a session description is received.
|
383
|
+
* @param {string} mode The encryption mode
|
384
|
+
* @param {string} secret The secret key
|
385
|
+
* @private
|
386
|
+
*/
|
387
|
+
onSessionDescription(mode, secret) {
|
388
|
+
this.authentication.encryptionMode = mode;
|
389
|
+
this.authentication.secretKey = secret;
|
390
|
+
|
391
|
+
this.status = Constants.VoiceStatus.CONNECTED;
|
392
|
+
/**
|
393
|
+
* Emitted once the connection is ready, when a promise to join a voice channel resolves,
|
394
|
+
* the connection will already be ready.
|
395
|
+
* @event VoiceConnection#ready
|
396
|
+
*/
|
397
|
+
this.emit('ready');
|
398
|
+
}
|
399
|
+
|
400
|
+
/**
|
401
|
+
* Invoked when a speaking event is received.
|
402
|
+
* @param {Object} data The received data
|
403
|
+
* @private
|
404
|
+
*/
|
405
|
+
onSpeaking({ user_id, ssrc, speaking }) {
|
406
|
+
const guild = this.channel.guild;
|
407
|
+
const user = this.client.users.get(user_id);
|
408
|
+
this.ssrcMap.set(+ssrc, user);
|
409
|
+
if (!speaking) {
|
410
|
+
for (const receiver of this.receivers) {
|
411
|
+
receiver.stoppedSpeaking(user);
|
412
|
+
}
|
413
|
+
}
|
414
|
+
/**
|
415
|
+
* Emitted whenever a user starts/stops speaking.
|
416
|
+
* @event VoiceConnection#speaking
|
417
|
+
* @param {User} user The user that has started/stopped speaking
|
418
|
+
* @param {boolean} speaking Whether or not the user is speaking
|
419
|
+
*/
|
420
|
+
if (this.status === Constants.VoiceStatus.CONNECTED) this.emit('speaking', user, speaking);
|
421
|
+
guild._memberSpeakUpdate(user_id, speaking);
|
422
|
+
}
|
423
|
+
|
424
|
+
/**
|
425
|
+
* Options that can be passed to stream-playing methods:
|
426
|
+
* @typedef {Object} StreamOptions
|
427
|
+
* @property {number} [seek=0] The time to seek to
|
428
|
+
* @property {number} [volume=1] The volume to play at
|
429
|
+
* @property {number} [passes=1] How many times to send the voice packet to reduce packet loss
|
430
|
+
* @property {number|string} [bitrate=48000] The bitrate (quality) of the audio.
|
431
|
+
* If set to 'auto', the voice channel's bitrate will be used
|
432
|
+
*/
|
433
|
+
|
434
|
+
/**
|
435
|
+
* Play the given file in the voice connection.
|
436
|
+
* @param {string} file The absolute path to the file
|
437
|
+
* @param {StreamOptions} [options] Options for playing the stream
|
438
|
+
* @returns {StreamDispatcher}
|
439
|
+
* @example
|
440
|
+
* // Play files natively
|
441
|
+
* voiceChannel.join()
|
442
|
+
* .then(connection => {
|
443
|
+
* const dispatcher = connection.playFile('C:/Users/Discord/Desktop/music.mp3');
|
444
|
+
* })
|
445
|
+
* .catch(console.error);
|
446
|
+
*/
|
447
|
+
playFile(file, options) {
|
448
|
+
return this.player.playUnknownStream(`file:${file}`, options);
|
449
|
+
}
|
450
|
+
|
451
|
+
/**
|
452
|
+
* Play an arbitrary input that can be [handled by ffmpeg](https://ffmpeg.org/ffmpeg-protocols.html#Description)
|
453
|
+
* @param {string} input the arbitrary input
|
454
|
+
* @param {StreamOptions} [options] Options for playing the stream
|
455
|
+
* @returns {StreamDispatcher}
|
456
|
+
*/
|
457
|
+
playArbitraryInput(input, options) {
|
458
|
+
return this.player.playUnknownStream(input, options);
|
459
|
+
}
|
460
|
+
|
461
|
+
/**
|
462
|
+
* Plays and converts an audio stream in the voice connection.
|
463
|
+
* @param {ReadableStream} stream The audio stream to play
|
464
|
+
* @param {StreamOptions} [options] Options for playing the stream
|
465
|
+
* @returns {StreamDispatcher}
|
466
|
+
* @example
|
467
|
+
* // Play streams using ytdl-core
|
468
|
+
* const ytdl = require('ytdl-core');
|
469
|
+
* const streamOptions = { seek: 0, volume: 1 };
|
470
|
+
* voiceChannel.join()
|
471
|
+
* .then(connection => {
|
472
|
+
* const stream = ytdl('https://www.youtube.com/watch?v=XAWgeLF9EVQ', { filter : 'audioonly' });
|
473
|
+
* const dispatcher = connection.playStream(stream, streamOptions);
|
474
|
+
* })
|
475
|
+
* .catch(console.error);
|
476
|
+
*/
|
477
|
+
playStream(stream, options) {
|
478
|
+
return this.player.playUnknownStream(stream, options);
|
479
|
+
}
|
480
|
+
|
481
|
+
/**
|
482
|
+
* Plays a stream of 16-bit signed stereo PCM.
|
483
|
+
* @param {ReadableStream} stream The audio stream to play
|
484
|
+
* @param {StreamOptions} [options] Options for playing the stream
|
485
|
+
* @returns {StreamDispatcher}
|
486
|
+
*/
|
487
|
+
playConvertedStream(stream, options) {
|
488
|
+
return this.player.playPCMStream(stream, options);
|
489
|
+
}
|
490
|
+
|
491
|
+
/**
|
492
|
+
* Plays an Opus encoded stream.
|
493
|
+
* <warn>Note that inline volume is not compatible with this method.</warn>
|
494
|
+
* @param {ReadableStream} stream The Opus audio stream to play
|
495
|
+
* @param {StreamOptions} [options] Options for playing the stream
|
496
|
+
* @returns {StreamDispatcher}
|
497
|
+
*/
|
498
|
+
playOpusStream(stream, options) {
|
499
|
+
return this.player.playOpusStream(stream, options);
|
500
|
+
}
|
501
|
+
|
502
|
+
/**
|
503
|
+
* Plays a voice broadcast.
|
504
|
+
* @param {VoiceBroadcast} broadcast The broadcast to play
|
505
|
+
* @param {StreamOptions} [options] Options for playing the stream
|
506
|
+
* @returns {StreamDispatcher}
|
507
|
+
* @example
|
508
|
+
* // Play a broadcast
|
509
|
+
* const broadcast = client
|
510
|
+
* .createVoiceBroadcast()
|
511
|
+
* .playFile('./test.mp3');
|
512
|
+
* const dispatcher = voiceConnection.playBroadcast(broadcast);
|
513
|
+
*/
|
514
|
+
playBroadcast(broadcast, options) {
|
515
|
+
return this.player.playBroadcast(broadcast, options);
|
516
|
+
}
|
517
|
+
|
518
|
+
/**
|
519
|
+
* Creates a VoiceReceiver so you can start listening to voice data.
|
520
|
+
* It's recommended to only create one of these.
|
521
|
+
* @returns {VoiceReceiver}
|
522
|
+
*/
|
523
|
+
createReceiver() {
|
524
|
+
const receiver = new VoiceReceiver(this);
|
525
|
+
this.receivers.push(receiver);
|
526
|
+
return receiver;
|
527
|
+
}
|
528
|
+
}
|
529
|
+
|
530
|
+
module.exports = VoiceConnection;
|
@@ -0,0 +1,127 @@
|
|
1
|
+
const udp = require('dgram');
|
2
|
+
const Constants = require('../../util/Constants');
|
3
|
+
const EventEmitter = require('events').EventEmitter;
|
4
|
+
|
5
|
+
/**
|
6
|
+
* Represents a UDP client for a Voice Connection.
|
7
|
+
* @extends {EventEmitter}
|
8
|
+
* @private
|
9
|
+
*/
|
10
|
+
class VoiceConnectionUDPClient extends EventEmitter {
|
11
|
+
constructor(voiceConnection) {
|
12
|
+
super();
|
13
|
+
|
14
|
+
/**
|
15
|
+
* The voice connection that this UDP client serves
|
16
|
+
* @type {VoiceConnection}
|
17
|
+
*/
|
18
|
+
this.voiceConnection = voiceConnection;
|
19
|
+
|
20
|
+
/**
|
21
|
+
* The UDP socket
|
22
|
+
* @type {?Socket}
|
23
|
+
*/
|
24
|
+
this.socket = null;
|
25
|
+
|
26
|
+
/**
|
27
|
+
* The address of the Discord voice server
|
28
|
+
* @type {?string}
|
29
|
+
*/
|
30
|
+
this.discordAddress = null;
|
31
|
+
|
32
|
+
/**
|
33
|
+
* The local IP address
|
34
|
+
* @type {?string}
|
35
|
+
*/
|
36
|
+
this.localAddress = null;
|
37
|
+
|
38
|
+
/**
|
39
|
+
* The local port
|
40
|
+
* @type {?string}
|
41
|
+
*/
|
42
|
+
this.localPort = null;
|
43
|
+
|
44
|
+
this.voiceConnection.on('closing', this.shutdown.bind(this));
|
45
|
+
}
|
46
|
+
|
47
|
+
shutdown() {
|
48
|
+
if (this.socket) {
|
49
|
+
this.socket.removeAllListeners('message');
|
50
|
+
try {
|
51
|
+
this.socket.close();
|
52
|
+
} finally {
|
53
|
+
this.socket = null;
|
54
|
+
}
|
55
|
+
}
|
56
|
+
}
|
57
|
+
|
58
|
+
/**
|
59
|
+
* The port of the Discord voice server
|
60
|
+
* @type {number}
|
61
|
+
* @readonly
|
62
|
+
*/
|
63
|
+
get discordPort() {
|
64
|
+
return this.voiceConnection.authentication.port;
|
65
|
+
}
|
66
|
+
|
67
|
+
/**
|
68
|
+
* Send a packet to the UDP client.
|
69
|
+
* @param {Object} packet The packet to send
|
70
|
+
* @returns {Promise<Object>}
|
71
|
+
*/
|
72
|
+
send(packet) {
|
73
|
+
return new Promise((resolve, reject) => {
|
74
|
+
if (!this.socket) throw new Error('Tried to send a UDP packet, but there is no socket available.');
|
75
|
+
if (!this.discordAddress || !this.discordPort) throw new Error('Malformed UDP address or port.');
|
76
|
+
this.socket.send(packet, 0, packet.length, this.discordPort, this.discordAddress, error => {
|
77
|
+
if (error) reject(error); else resolve(packet);
|
78
|
+
});
|
79
|
+
});
|
80
|
+
}
|
81
|
+
|
82
|
+
createUDPSocket(address) {
|
83
|
+
this.discordAddress = address;
|
84
|
+
const socket = this.socket = udp.createSocket('udp4');
|
85
|
+
|
86
|
+
socket.once('message', message => {
|
87
|
+
const packet = parseLocalPacket(message);
|
88
|
+
if (packet.error) {
|
89
|
+
this.emit('error', packet.error);
|
90
|
+
return;
|
91
|
+
}
|
92
|
+
|
93
|
+
this.localAddress = packet.address;
|
94
|
+
this.localPort = packet.port;
|
95
|
+
|
96
|
+
this.voiceConnection.sockets.ws.sendPacket({
|
97
|
+
op: Constants.VoiceOPCodes.SELECT_PROTOCOL,
|
98
|
+
d: {
|
99
|
+
protocol: 'udp',
|
100
|
+
data: {
|
101
|
+
address: packet.address,
|
102
|
+
port: packet.port,
|
103
|
+
mode: 'xsalsa20_poly1305',
|
104
|
+
},
|
105
|
+
},
|
106
|
+
});
|
107
|
+
});
|
108
|
+
|
109
|
+
const blankMessage = Buffer.alloc(70);
|
110
|
+
blankMessage.writeUIntBE(this.voiceConnection.authentication.ssrc, 0, 4);
|
111
|
+
this.send(blankMessage);
|
112
|
+
}
|
113
|
+
}
|
114
|
+
|
115
|
+
function parseLocalPacket(message) {
|
116
|
+
try {
|
117
|
+
const packet = Buffer.from(message);
|
118
|
+
let address = '';
|
119
|
+
for (let i = 4; i < packet.indexOf(0, i); i++) address += String.fromCharCode(packet[i]);
|
120
|
+
const port = parseInt(packet.readUIntLE(packet.length - 2, 2).toString(10), 10);
|
121
|
+
return { address, port };
|
122
|
+
} catch (error) {
|
123
|
+
return { error };
|
124
|
+
}
|
125
|
+
}
|
126
|
+
|
127
|
+
module.exports = VoiceConnectionUDPClient;
|