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,366 @@
|
|
1
|
+
const VolumeInterface = require('./util/VolumeInterface');
|
2
|
+
const Prism = require('prism-media');
|
3
|
+
const OpusEncoders = require('./opus/OpusEngineList');
|
4
|
+
const Collection = require('../../util/Collection');
|
5
|
+
|
6
|
+
const ffmpegArguments = [
|
7
|
+
'-analyzeduration', '0',
|
8
|
+
'-loglevel', '0',
|
9
|
+
'-f', 's16le',
|
10
|
+
'-ar', '48000',
|
11
|
+
'-ac', '2',
|
12
|
+
];
|
13
|
+
|
14
|
+
/**
|
15
|
+
* A voice broadcast can be played across multiple voice connections for improved shared-stream efficiency.
|
16
|
+
*
|
17
|
+
* Example usage:
|
18
|
+
* ```js
|
19
|
+
* const broadcast = client.createVoiceBroadcast();
|
20
|
+
* broadcast.playFile('./music.mp3');
|
21
|
+
* // Play "music.mp3" in all voice connections that the client is in
|
22
|
+
* for (const connection of client.voiceConnections.values()) {
|
23
|
+
* connection.playBroadcast(broadcast);
|
24
|
+
* }
|
25
|
+
* ```
|
26
|
+
* @implements {VolumeInterface}
|
27
|
+
*/
|
28
|
+
class VoiceBroadcast extends VolumeInterface {
|
29
|
+
constructor(client) {
|
30
|
+
super();
|
31
|
+
/**
|
32
|
+
* The client that created the broadcast
|
33
|
+
* @type {Client}
|
34
|
+
*/
|
35
|
+
this.client = client;
|
36
|
+
this._dispatchers = new Collection();
|
37
|
+
this._encoders = new Collection();
|
38
|
+
/**
|
39
|
+
* The audio transcoder that this broadcast uses
|
40
|
+
* @type {Prism}
|
41
|
+
*/
|
42
|
+
this.prism = new Prism();
|
43
|
+
/**
|
44
|
+
* The current audio transcoder that is being used
|
45
|
+
* @type {Object}
|
46
|
+
*/
|
47
|
+
this.currentTranscoder = null;
|
48
|
+
this.tickInterval = null;
|
49
|
+
this._volume = 1;
|
50
|
+
}
|
51
|
+
|
52
|
+
/**
|
53
|
+
* An array of subscribed dispatchers
|
54
|
+
* @type {StreamDispatcher[]}
|
55
|
+
* @readonly
|
56
|
+
*/
|
57
|
+
get dispatchers() {
|
58
|
+
let d = [];
|
59
|
+
for (const container of this._dispatchers.values()) {
|
60
|
+
d = d.concat(Array.from(container.values()));
|
61
|
+
}
|
62
|
+
return d;
|
63
|
+
}
|
64
|
+
|
65
|
+
get _playableStream() {
|
66
|
+
const currentTranscoder = this.currentTranscoder;
|
67
|
+
if (!currentTranscoder) return null;
|
68
|
+
const transcoder = currentTranscoder.transcoder;
|
69
|
+
const options = currentTranscoder.options;
|
70
|
+
return (transcoder && transcoder.output) || options.stream;
|
71
|
+
}
|
72
|
+
|
73
|
+
unregisterDispatcher(dispatcher, old) {
|
74
|
+
const volume = old || dispatcher.volume;
|
75
|
+
|
76
|
+
/**
|
77
|
+
* Emitted whenever a stream dispatcher unsubscribes from the broadcast.
|
78
|
+
* @event VoiceBroadcast#unsubscribe
|
79
|
+
* @param {StreamDispatcher} dispatcher The unsubscribed dispatcher
|
80
|
+
*/
|
81
|
+
this.emit('unsubscribe', dispatcher);
|
82
|
+
for (const container of this._dispatchers.values()) {
|
83
|
+
container.delete(dispatcher);
|
84
|
+
|
85
|
+
if (!container.size) {
|
86
|
+
this._encoders.get(volume).destroy();
|
87
|
+
this._dispatchers.delete(volume);
|
88
|
+
this._encoders.delete(volume);
|
89
|
+
}
|
90
|
+
}
|
91
|
+
}
|
92
|
+
|
93
|
+
registerDispatcher(dispatcher) {
|
94
|
+
if (!this._dispatchers.has(dispatcher.volume)) {
|
95
|
+
this._dispatchers.set(dispatcher.volume, new Set());
|
96
|
+
this._encoders.set(dispatcher.volume, OpusEncoders.fetch());
|
97
|
+
}
|
98
|
+
const container = this._dispatchers.get(dispatcher.volume);
|
99
|
+
if (!container.has(dispatcher)) {
|
100
|
+
container.add(dispatcher);
|
101
|
+
dispatcher.once('end', () => this.unregisterDispatcher(dispatcher));
|
102
|
+
dispatcher.on('volumeChange', (o, n) => {
|
103
|
+
this.unregisterDispatcher(dispatcher, o);
|
104
|
+
if (!this._dispatchers.has(n)) {
|
105
|
+
this._dispatchers.set(n, new Set());
|
106
|
+
this._encoders.set(n, OpusEncoders.fetch());
|
107
|
+
}
|
108
|
+
this._dispatchers.get(n).add(dispatcher);
|
109
|
+
});
|
110
|
+
/**
|
111
|
+
* Emitted whenever a stream dispatcher subscribes to the broadcast.
|
112
|
+
* @event VoiceBroadcast#subscribe
|
113
|
+
* @param {StreamDispatcher} dispatcher The subscribed dispatcher
|
114
|
+
*/
|
115
|
+
this.emit('subscribe', dispatcher);
|
116
|
+
}
|
117
|
+
}
|
118
|
+
|
119
|
+
killCurrentTranscoder() {
|
120
|
+
if (this.currentTranscoder) {
|
121
|
+
if (this.currentTranscoder.transcoder) this.currentTranscoder.transcoder.kill();
|
122
|
+
this.currentTranscoder = null;
|
123
|
+
this.emit('end');
|
124
|
+
}
|
125
|
+
}
|
126
|
+
|
127
|
+
/**
|
128
|
+
* Plays any audio stream across the broadcast.
|
129
|
+
* @param {ReadableStream} stream The audio stream to play
|
130
|
+
* @param {StreamOptions} [options] Options for playing the stream
|
131
|
+
* @returns {VoiceBroadcast}
|
132
|
+
* @example
|
133
|
+
* // Play streams using ytdl-core
|
134
|
+
* const ytdl = require('ytdl-core');
|
135
|
+
* const streamOptions = { seek: 0, volume: 1 };
|
136
|
+
* const broadcast = client.createVoiceBroadcast();
|
137
|
+
*
|
138
|
+
* voiceChannel.join()
|
139
|
+
* .then(connection => {
|
140
|
+
* const stream = ytdl('https://www.youtube.com/watch?v=XAWgeLF9EVQ', { filter : 'audioonly' });
|
141
|
+
* broadcast.playStream(stream);
|
142
|
+
* const dispatcher = connection.playBroadcast(broadcast);
|
143
|
+
* })
|
144
|
+
* .catch(console.error);
|
145
|
+
*/
|
146
|
+
playStream(stream, options = {}) {
|
147
|
+
this.setVolume(options.volume || 1);
|
148
|
+
return this._playTranscodable(stream, options);
|
149
|
+
}
|
150
|
+
|
151
|
+
/**
|
152
|
+
* Play the given file in the voice connection.
|
153
|
+
* @param {string} file The absolute path to the file
|
154
|
+
* @param {StreamOptions} [options] Options for playing the stream
|
155
|
+
* @returns {StreamDispatcher}
|
156
|
+
* @example
|
157
|
+
* // Play files natively
|
158
|
+
* const broadcast = client.createVoiceBroadcast();
|
159
|
+
*
|
160
|
+
* voiceChannel.join()
|
161
|
+
* .then(connection => {
|
162
|
+
* broadcast.playFile('C:/Users/Discord/Desktop/music.mp3');
|
163
|
+
* const dispatcher = connection.playBroadcast(broadcast);
|
164
|
+
* })
|
165
|
+
* .catch(console.error);
|
166
|
+
*/
|
167
|
+
playFile(file, options = {}) {
|
168
|
+
this.setVolume(options.volume || 1);
|
169
|
+
return this._playTranscodable(`file:${file}`, options);
|
170
|
+
}
|
171
|
+
|
172
|
+
_playTranscodable(media, options) {
|
173
|
+
this.killCurrentTranscoder();
|
174
|
+
const transcoder = this.prism.transcode({
|
175
|
+
type: 'ffmpeg',
|
176
|
+
media,
|
177
|
+
ffmpegArguments: ffmpegArguments.concat(['-ss', String(options.seek || 0)]),
|
178
|
+
});
|
179
|
+
/**
|
180
|
+
* Emitted whenever an error occurs.
|
181
|
+
* @event VoiceBroadcast#error
|
182
|
+
* @param {Error} error The error that occurred
|
183
|
+
*/
|
184
|
+
transcoder.once('error', e => {
|
185
|
+
if (this.listenerCount('error') > 0) this.emit('error', e);
|
186
|
+
/**
|
187
|
+
* Emitted whenever the VoiceBroadcast has any warnings.
|
188
|
+
* @event VoiceBroadcast#warn
|
189
|
+
* @param {string|Error} warning The warning that was raised
|
190
|
+
*/
|
191
|
+
else this.emit('warn', e);
|
192
|
+
});
|
193
|
+
/**
|
194
|
+
* Emitted once the broadcast (the audio stream) ends.
|
195
|
+
* @event VoiceBroadcast#end
|
196
|
+
*/
|
197
|
+
transcoder.once('end', () => this.killCurrentTranscoder());
|
198
|
+
this.currentTranscoder = {
|
199
|
+
transcoder,
|
200
|
+
options,
|
201
|
+
};
|
202
|
+
transcoder.output.once('readable', () => this._startPlaying());
|
203
|
+
return this;
|
204
|
+
}
|
205
|
+
|
206
|
+
/**
|
207
|
+
* Plays a stream of 16-bit signed stereo PCM.
|
208
|
+
* @param {ReadableStream} stream The audio stream to play
|
209
|
+
* @param {StreamOptions} [options] Options for playing the stream
|
210
|
+
* @returns {VoiceBroadcast}
|
211
|
+
*/
|
212
|
+
playConvertedStream(stream, options = {}) {
|
213
|
+
this.killCurrentTranscoder();
|
214
|
+
this.setVolume(options.volume || 1);
|
215
|
+
this.currentTranscoder = { options: { stream } };
|
216
|
+
stream.once('readable', () => this._startPlaying());
|
217
|
+
return this;
|
218
|
+
}
|
219
|
+
|
220
|
+
/**
|
221
|
+
* Plays an Opus encoded stream.
|
222
|
+
* <warn>Note that inline volume is not compatible with this method.</warn>
|
223
|
+
* @param {ReadableStream} stream The Opus audio stream to play
|
224
|
+
* @param {StreamOptions} [options] Options for playing the stream
|
225
|
+
* @returns {StreamDispatcher}
|
226
|
+
*/
|
227
|
+
playOpusStream(stream) {
|
228
|
+
this.currentTranscoder = { options: { stream }, opus: true };
|
229
|
+
stream.once('readable', () => this._startPlaying());
|
230
|
+
return this;
|
231
|
+
}
|
232
|
+
|
233
|
+
/**
|
234
|
+
* Play an arbitrary input that can be [handled by ffmpeg](https://ffmpeg.org/ffmpeg-protocols.html#Description)
|
235
|
+
* @param {string} input The arbitrary input
|
236
|
+
* @param {StreamOptions} [options] Options for playing the stream
|
237
|
+
* @returns {VoiceBroadcast}
|
238
|
+
*/
|
239
|
+
playArbitraryInput(input, options = {}) {
|
240
|
+
this.setVolume(options.volume || 1);
|
241
|
+
options.input = input;
|
242
|
+
return this._playTranscodable(input, options);
|
243
|
+
}
|
244
|
+
|
245
|
+
/**
|
246
|
+
* Pauses the entire broadcast - all dispatchers also pause.
|
247
|
+
*/
|
248
|
+
pause() {
|
249
|
+
this.paused = true;
|
250
|
+
for (const container of this._dispatchers.values()) {
|
251
|
+
for (const dispatcher of container.values()) {
|
252
|
+
dispatcher.pause();
|
253
|
+
}
|
254
|
+
}
|
255
|
+
}
|
256
|
+
|
257
|
+
/**
|
258
|
+
* Resumes the entire broadcast - all dispatchers also resume.
|
259
|
+
*/
|
260
|
+
resume() {
|
261
|
+
this.paused = false;
|
262
|
+
for (const container of this._dispatchers.values()) {
|
263
|
+
for (const dispatcher of container.values()) {
|
264
|
+
dispatcher.resume();
|
265
|
+
}
|
266
|
+
}
|
267
|
+
}
|
268
|
+
|
269
|
+
_startPlaying() {
|
270
|
+
if (this.tickInterval) clearInterval(this.tickInterval);
|
271
|
+
// Old code?
|
272
|
+
// this.tickInterval = this.client.setInterval(this.tick.bind(this), 20);
|
273
|
+
this._startTime = Date.now();
|
274
|
+
this._count = 0;
|
275
|
+
this._pausedTime = 0;
|
276
|
+
this._missed = 0;
|
277
|
+
this.tick();
|
278
|
+
}
|
279
|
+
|
280
|
+
tick() {
|
281
|
+
if (!this._playableStream) return;
|
282
|
+
if (this.paused) {
|
283
|
+
this._pausedTime += 20;
|
284
|
+
setTimeout(() => this.tick(), 20);
|
285
|
+
return;
|
286
|
+
}
|
287
|
+
|
288
|
+
const opus = this.currentTranscoder.opus;
|
289
|
+
const buffer = this.readStreamBuffer();
|
290
|
+
|
291
|
+
if (!buffer) {
|
292
|
+
this._missed++;
|
293
|
+
if (this._missed < 5) {
|
294
|
+
this._pausedTime += 200;
|
295
|
+
setTimeout(() => this.tick(), 200);
|
296
|
+
} else {
|
297
|
+
this.killCurrentTranscoder();
|
298
|
+
}
|
299
|
+
return;
|
300
|
+
}
|
301
|
+
|
302
|
+
this._missed = 0;
|
303
|
+
|
304
|
+
let packetMatrix = {};
|
305
|
+
|
306
|
+
const getOpusPacket = volume => {
|
307
|
+
if (packetMatrix[volume]) return packetMatrix[volume];
|
308
|
+
|
309
|
+
const opusEncoder = this._encoders.get(volume);
|
310
|
+
const opusPacket = opusEncoder.encode(this.applyVolume(buffer, this._volume * volume));
|
311
|
+
packetMatrix[volume] = opusPacket;
|
312
|
+
return opusPacket;
|
313
|
+
};
|
314
|
+
|
315
|
+
for (const dispatcher of this.dispatchers) {
|
316
|
+
if (opus) {
|
317
|
+
dispatcher.processPacket(buffer);
|
318
|
+
continue;
|
319
|
+
}
|
320
|
+
|
321
|
+
const volume = dispatcher.volume;
|
322
|
+
dispatcher.processPacket(getOpusPacket(volume));
|
323
|
+
}
|
324
|
+
|
325
|
+
const next = 20 + (this._startTime + this._pausedTime + (this._count * 20) - Date.now());
|
326
|
+
this._count++;
|
327
|
+
setTimeout(() => this.tick(), next);
|
328
|
+
}
|
329
|
+
|
330
|
+
readStreamBuffer() {
|
331
|
+
const opus = this.currentTranscoder.opus;
|
332
|
+
const bufferLength = (opus ? 80 : 1920) * 2;
|
333
|
+
let buffer = this._playableStream.read(bufferLength);
|
334
|
+
if (opus) return buffer;
|
335
|
+
if (!buffer) return null;
|
336
|
+
|
337
|
+
if (buffer.length !== bufferLength) {
|
338
|
+
const newBuffer = Buffer.alloc(bufferLength).fill(0);
|
339
|
+
buffer.copy(newBuffer);
|
340
|
+
buffer = newBuffer;
|
341
|
+
}
|
342
|
+
|
343
|
+
return buffer;
|
344
|
+
}
|
345
|
+
|
346
|
+
/**
|
347
|
+
* Stop the current stream from playing without unsubscribing dispatchers.
|
348
|
+
*/
|
349
|
+
end() {
|
350
|
+
this.killCurrentTranscoder();
|
351
|
+
}
|
352
|
+
|
353
|
+
/**
|
354
|
+
* End the current broadcast, all subscribed dispatchers will also end.
|
355
|
+
*/
|
356
|
+
destroy() {
|
357
|
+
this.end();
|
358
|
+
for (const container of this._dispatchers.values()) {
|
359
|
+
for (const dispatcher of container.values()) {
|
360
|
+
dispatcher.destroy('end', 'broadcast ended');
|
361
|
+
}
|
362
|
+
}
|
363
|
+
}
|
364
|
+
}
|
365
|
+
|
366
|
+
module.exports = VoiceBroadcast;
|