discord-player 5.3.2-dev.3 → 5.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,226 +1,226 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StreamDispatcher = void 0;
4
- const voice_1 = require("@discordjs/voice");
5
- const tiny_typed_emitter_1 = require("tiny-typed-emitter");
6
- const Util_1 = require("../utils/Util");
7
- const PlayerError_1 = require("../Structures/PlayerError");
8
- class StreamDispatcher extends tiny_typed_emitter_1.TypedEmitter {
9
- /**
10
- * Creates new connection object
11
- * @param {VoiceConnection} connection The connection
12
- * @param {VoiceChannel|StageChannel} channel The connected channel
13
- * @private
14
- */
15
- constructor(connection, channel, connectionTimeout = 20000) {
16
- super();
17
- this.connectionTimeout = connectionTimeout;
18
- this.readyLock = false;
19
- /**
20
- * The voice connection
21
- * @type {VoiceConnection}
22
- */
23
- this.voiceConnection = connection;
24
- /**
25
- * The audio player
26
- * @type {AudioPlayer}
27
- */
28
- this.audioPlayer = (0, voice_1.createAudioPlayer)();
29
- /**
30
- * The voice channel
31
- * @type {VoiceChannel|StageChannel}
32
- */
33
- this.channel = channel;
34
- /**
35
- * The paused state
36
- * @type {boolean}
37
- */
38
- this.paused = false;
39
- this.voiceConnection.on("stateChange", async (_, newState) => {
40
- if (newState.status === voice_1.VoiceConnectionStatus.Disconnected) {
41
- if (newState.reason === voice_1.VoiceConnectionDisconnectReason.WebSocketClose && newState.closeCode === 4014) {
42
- try {
43
- await (0, voice_1.entersState)(this.voiceConnection, voice_1.VoiceConnectionStatus.Connecting, this.connectionTimeout);
44
- }
45
- catch {
46
- try {
47
- this.voiceConnection.destroy();
48
- }
49
- catch (err) {
50
- this.emit("error", err);
51
- }
52
- }
53
- }
54
- else if (this.voiceConnection.rejoinAttempts < 5) {
55
- await Util_1.Util.wait((this.voiceConnection.rejoinAttempts + 1) * 5000);
56
- this.voiceConnection.rejoin();
57
- }
58
- else {
59
- try {
60
- this.voiceConnection.destroy();
61
- }
62
- catch (err) {
63
- this.emit("error", err);
64
- }
65
- }
66
- }
67
- else if (newState.status === voice_1.VoiceConnectionStatus.Destroyed) {
68
- this.end();
69
- }
70
- else if (!this.readyLock && (newState.status === voice_1.VoiceConnectionStatus.Connecting || newState.status === voice_1.VoiceConnectionStatus.Signalling)) {
71
- this.readyLock = true;
72
- try {
73
- await (0, voice_1.entersState)(this.voiceConnection, voice_1.VoiceConnectionStatus.Ready, this.connectionTimeout);
74
- }
75
- catch {
76
- if (this.voiceConnection.state.status !== voice_1.VoiceConnectionStatus.Destroyed) {
77
- try {
78
- this.voiceConnection.destroy();
79
- }
80
- catch (err) {
81
- this.emit("error", err);
82
- }
83
- }
84
- }
85
- finally {
86
- this.readyLock = false;
87
- }
88
- }
89
- });
90
- this.audioPlayer.on("stateChange", (oldState, newState) => {
91
- if (newState.status === voice_1.AudioPlayerStatus.Playing) {
92
- if (!this.paused)
93
- return void this.emit("start", this.audioResource);
94
- }
95
- else if (newState.status === voice_1.AudioPlayerStatus.Idle && oldState.status !== voice_1.AudioPlayerStatus.Idle) {
96
- if (!this.paused) {
97
- void this.emit("finish", this.audioResource);
98
- this.audioResource = null;
99
- }
100
- }
101
- });
102
- this.audioPlayer.on("debug", (m) => void this.emit("debug", m));
103
- this.audioPlayer.on("error", (error) => void this.emit("error", error));
104
- this.voiceConnection.subscribe(this.audioPlayer);
105
- }
106
- /**
107
- * Creates stream
108
- * @param {Readable|Duplex|string} src The stream source
109
- * @param {object} [ops] Options
110
- * @returns {AudioResource}
111
- */
112
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
113
- createStream(src, ops) {
114
- this.audioResource = (0, voice_1.createAudioResource)(src, {
115
- inputType: ops?.type ?? voice_1.StreamType.Arbitrary,
116
- metadata: ops?.data,
117
- // eslint-disable-next-line no-extra-boolean-cast
118
- inlineVolume: !Boolean(ops?.disableVolume)
119
- });
120
- return this.audioResource;
121
- }
122
- /**
123
- * The player status
124
- * @type {AudioPlayerStatus}
125
- */
126
- get status() {
127
- return this.audioPlayer.state.status;
128
- }
129
- /**
130
- * Disconnects from voice
131
- * @returns {void}
132
- */
133
- disconnect() {
134
- try {
135
- this.audioPlayer.stop(true);
136
- this.voiceConnection.destroy();
137
- }
138
- catch { } // eslint-disable-line no-empty
139
- }
140
- /**
141
- * Stops the player
142
- * @returns {void}
143
- */
144
- end() {
145
- this.audioPlayer.stop();
146
- }
147
- /**
148
- * Pauses the stream playback
149
- * @param {boolean} [interpolateSilence=false] If true, the player will play 5 packets of silence after pausing to prevent audio glitches.
150
- * @returns {boolean}
151
- */
152
- pause(interpolateSilence) {
153
- const success = this.audioPlayer.pause(interpolateSilence);
154
- this.paused = success;
155
- return success;
156
- }
157
- /**
158
- * Resumes the stream playback
159
- * @returns {boolean}
160
- */
161
- resume() {
162
- const success = this.audioPlayer.unpause();
163
- this.paused = !success;
164
- return success;
165
- }
166
- /**
167
- * Play stream
168
- * @param {AudioResource<Track>} [resource=this.audioResource] The audio resource to play
169
- * @returns {Promise<StreamDispatcher>}
170
- */
171
- async playStream(resource = this.audioResource) {
172
- if (!resource)
173
- throw new PlayerError_1.PlayerError("Audio resource is not available!", PlayerError_1.ErrorStatusCode.NO_AUDIO_RESOURCE);
174
- if (resource.ended) {
175
- return void this.emit("finish", resource);
176
- }
177
- if (!this.audioResource)
178
- this.audioResource = resource;
179
- if (this.voiceConnection.state.status !== voice_1.VoiceConnectionStatus.Ready) {
180
- try {
181
- await (0, voice_1.entersState)(this.voiceConnection, voice_1.VoiceConnectionStatus.Ready, this.connectionTimeout);
182
- }
183
- catch (err) {
184
- return void this.emit("error", err);
185
- }
186
- }
187
- try {
188
- this.audioPlayer.play(resource);
189
- }
190
- catch (e) {
191
- this.emit("error", e);
192
- }
193
- return this;
194
- }
195
- /**
196
- * Sets playback volume
197
- * @param {number} value The volume amount
198
- * @returns {boolean}
199
- */
200
- setVolume(value) {
201
- if (!this.audioResource?.volume || isNaN(value) || value < 0 || value > Infinity)
202
- return false;
203
- this.audioResource.volume.setVolumeLogarithmic(value / 100);
204
- return true;
205
- }
206
- /**
207
- * The current volume
208
- * @type {number}
209
- */
210
- get volume() {
211
- if (!this.audioResource?.volume)
212
- return 100;
213
- const currentVol = this.audioResource.volume.volume;
214
- return Math.round(Math.pow(currentVol, 1 / 1.660964) * 100);
215
- }
216
- /**
217
- * The playback time
218
- * @type {number}
219
- */
220
- get streamTime() {
221
- if (!this.audioResource)
222
- return 0;
223
- return this.audioResource.playbackDuration;
224
- }
225
- }
226
- exports.StreamDispatcher = StreamDispatcher;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StreamDispatcher = void 0;
4
+ const voice_1 = require("@discordjs/voice");
5
+ const tiny_typed_emitter_1 = require("tiny-typed-emitter");
6
+ const Util_1 = require("../utils/Util");
7
+ const PlayerError_1 = require("../Structures/PlayerError");
8
+ class StreamDispatcher extends tiny_typed_emitter_1.TypedEmitter {
9
+ /**
10
+ * Creates new connection object
11
+ * @param {VoiceConnection} connection The connection
12
+ * @param {VoiceChannel|StageChannel} channel The connected channel
13
+ * @private
14
+ */
15
+ constructor(connection, channel, connectionTimeout = 20000) {
16
+ super();
17
+ this.connectionTimeout = connectionTimeout;
18
+ this.readyLock = false;
19
+ /**
20
+ * The voice connection
21
+ * @type {VoiceConnection}
22
+ */
23
+ this.voiceConnection = connection;
24
+ /**
25
+ * The audio player
26
+ * @type {AudioPlayer}
27
+ */
28
+ this.audioPlayer = (0, voice_1.createAudioPlayer)();
29
+ /**
30
+ * The voice channel
31
+ * @type {VoiceChannel|StageChannel}
32
+ */
33
+ this.channel = channel;
34
+ /**
35
+ * The paused state
36
+ * @type {boolean}
37
+ */
38
+ this.paused = false;
39
+ this.voiceConnection.on("stateChange", async (_, newState) => {
40
+ if (newState.status === voice_1.VoiceConnectionStatus.Disconnected) {
41
+ if (newState.reason === voice_1.VoiceConnectionDisconnectReason.WebSocketClose && newState.closeCode === 4014) {
42
+ try {
43
+ await (0, voice_1.entersState)(this.voiceConnection, voice_1.VoiceConnectionStatus.Connecting, this.connectionTimeout);
44
+ }
45
+ catch {
46
+ try {
47
+ this.voiceConnection.destroy();
48
+ }
49
+ catch (err) {
50
+ this.emit("error", err);
51
+ }
52
+ }
53
+ }
54
+ else if (this.voiceConnection.rejoinAttempts < 5) {
55
+ await Util_1.Util.wait((this.voiceConnection.rejoinAttempts + 1) * 5000);
56
+ this.voiceConnection.rejoin();
57
+ }
58
+ else {
59
+ try {
60
+ this.voiceConnection.destroy();
61
+ }
62
+ catch (err) {
63
+ this.emit("error", err);
64
+ }
65
+ }
66
+ }
67
+ else if (newState.status === voice_1.VoiceConnectionStatus.Destroyed) {
68
+ this.end();
69
+ }
70
+ else if (!this.readyLock && (newState.status === voice_1.VoiceConnectionStatus.Connecting || newState.status === voice_1.VoiceConnectionStatus.Signalling)) {
71
+ this.readyLock = true;
72
+ try {
73
+ await (0, voice_1.entersState)(this.voiceConnection, voice_1.VoiceConnectionStatus.Ready, this.connectionTimeout);
74
+ }
75
+ catch {
76
+ if (this.voiceConnection.state.status !== voice_1.VoiceConnectionStatus.Destroyed) {
77
+ try {
78
+ this.voiceConnection.destroy();
79
+ }
80
+ catch (err) {
81
+ this.emit("error", err);
82
+ }
83
+ }
84
+ }
85
+ finally {
86
+ this.readyLock = false;
87
+ }
88
+ }
89
+ });
90
+ this.audioPlayer.on("stateChange", (oldState, newState) => {
91
+ if (newState.status === voice_1.AudioPlayerStatus.Playing) {
92
+ if (!this.paused)
93
+ return void this.emit("start", this.audioResource);
94
+ }
95
+ else if (newState.status === voice_1.AudioPlayerStatus.Idle && oldState.status !== voice_1.AudioPlayerStatus.Idle) {
96
+ if (!this.paused) {
97
+ void this.emit("finish", this.audioResource);
98
+ this.audioResource = null;
99
+ }
100
+ }
101
+ });
102
+ this.audioPlayer.on("debug", (m) => void this.emit("debug", m));
103
+ this.audioPlayer.on("error", (error) => void this.emit("error", error));
104
+ this.voiceConnection.subscribe(this.audioPlayer);
105
+ }
106
+ /**
107
+ * Creates stream
108
+ * @param {Readable|Duplex|string} src The stream source
109
+ * @param {object} [ops] Options
110
+ * @returns {AudioResource}
111
+ */
112
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
113
+ createStream(src, ops) {
114
+ this.audioResource = (0, voice_1.createAudioResource)(src, {
115
+ inputType: ops?.type ?? voice_1.StreamType.Arbitrary,
116
+ metadata: ops?.data,
117
+ // eslint-disable-next-line no-extra-boolean-cast
118
+ inlineVolume: !Boolean(ops?.disableVolume)
119
+ });
120
+ return this.audioResource;
121
+ }
122
+ /**
123
+ * The player status
124
+ * @type {AudioPlayerStatus}
125
+ */
126
+ get status() {
127
+ return this.audioPlayer.state.status;
128
+ }
129
+ /**
130
+ * Disconnects from voice
131
+ * @returns {void}
132
+ */
133
+ disconnect() {
134
+ try {
135
+ this.audioPlayer.stop(true);
136
+ this.voiceConnection.destroy();
137
+ }
138
+ catch { } // eslint-disable-line no-empty
139
+ }
140
+ /**
141
+ * Stops the player
142
+ * @returns {void}
143
+ */
144
+ end() {
145
+ this.audioPlayer.stop();
146
+ }
147
+ /**
148
+ * Pauses the stream playback
149
+ * @param {boolean} [interpolateSilence=false] If true, the player will play 5 packets of silence after pausing to prevent audio glitches.
150
+ * @returns {boolean}
151
+ */
152
+ pause(interpolateSilence) {
153
+ const success = this.audioPlayer.pause(interpolateSilence);
154
+ this.paused = success;
155
+ return success;
156
+ }
157
+ /**
158
+ * Resumes the stream playback
159
+ * @returns {boolean}
160
+ */
161
+ resume() {
162
+ const success = this.audioPlayer.unpause();
163
+ this.paused = !success;
164
+ return success;
165
+ }
166
+ /**
167
+ * Play stream
168
+ * @param {AudioResource<Track>} [resource=this.audioResource] The audio resource to play
169
+ * @returns {Promise<StreamDispatcher>}
170
+ */
171
+ async playStream(resource = this.audioResource) {
172
+ if (!resource)
173
+ throw new PlayerError_1.PlayerError("Audio resource is not available!", PlayerError_1.ErrorStatusCode.NO_AUDIO_RESOURCE);
174
+ if (resource.ended) {
175
+ return void this.emit("finish", resource);
176
+ }
177
+ if (!this.audioResource)
178
+ this.audioResource = resource;
179
+ if (this.voiceConnection.state.status !== voice_1.VoiceConnectionStatus.Ready) {
180
+ try {
181
+ await (0, voice_1.entersState)(this.voiceConnection, voice_1.VoiceConnectionStatus.Ready, this.connectionTimeout);
182
+ }
183
+ catch (err) {
184
+ return void this.emit("error", err);
185
+ }
186
+ }
187
+ try {
188
+ this.audioPlayer.play(resource);
189
+ }
190
+ catch (e) {
191
+ this.emit("error", e);
192
+ }
193
+ return this;
194
+ }
195
+ /**
196
+ * Sets playback volume
197
+ * @param {number} value The volume amount
198
+ * @returns {boolean}
199
+ */
200
+ setVolume(value) {
201
+ if (!this.audioResource?.volume || isNaN(value) || value < 0 || value > Infinity)
202
+ return false;
203
+ this.audioResource.volume.setVolumeLogarithmic(value / 100);
204
+ return true;
205
+ }
206
+ /**
207
+ * The current volume
208
+ * @type {number}
209
+ */
210
+ get volume() {
211
+ if (!this.audioResource?.volume)
212
+ return 100;
213
+ const currentVol = this.audioResource.volume.volume;
214
+ return Math.round(Math.pow(currentVol, 1 / 1.660964) * 100);
215
+ }
216
+ /**
217
+ * The playback time
218
+ * @type {number}
219
+ */
220
+ get streamTime() {
221
+ if (!this.audioResource)
222
+ return 0;
223
+ return this.audioResource.playbackDuration;
224
+ }
225
+ }
226
+ exports.StreamDispatcher = StreamDispatcher;
@@ -1,65 +1,65 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.VoiceUtils = void 0;
4
- const discord_js_1 = require("discord.js");
5
- const voice_1 = require("@discordjs/voice");
6
- const StreamDispatcher_1 = require("./StreamDispatcher");
7
- class VoiceUtils {
8
- /**
9
- * The voice utils
10
- * @private
11
- */
12
- constructor() {
13
- /**
14
- * The cache where voice utils stores stream managers
15
- * @type {Collection<Snowflake, StreamDispatcher>}
16
- */
17
- this.cache = new discord_js_1.Collection();
18
- }
19
- /**
20
- * Joins a voice channel, creating basic stream dispatch manager
21
- * @param {StageChannel|VoiceChannel} channel The voice channel
22
- * @param {object} [options] Join options
23
- * @returns {Promise<StreamDispatcher>}
24
- */
25
- async connect(channel, options) {
26
- const conn = await this.join(channel, options);
27
- const sub = new StreamDispatcher_1.StreamDispatcher(conn, channel, options.maxTime);
28
- this.cache.set(channel.guild.id, sub);
29
- return sub;
30
- }
31
- /**
32
- * Joins a voice channel
33
- * @param {StageChannel|VoiceChannel} [channel] The voice/stage channel to join
34
- * @param {object} [options] Join options
35
- * @returns {VoiceConnection}
36
- */
37
- async join(channel, options) {
38
- const conn = (0, voice_1.joinVoiceChannel)({
39
- guildId: channel.guild.id,
40
- channelId: channel.id,
41
- adapterCreator: channel.guild.voiceAdapterCreator,
42
- selfDeaf: Boolean(options.deaf)
43
- });
44
- return conn;
45
- }
46
- /**
47
- * Disconnects voice connection
48
- * @param {VoiceConnection} connection The voice connection
49
- * @returns {void}
50
- */
51
- disconnect(connection) {
52
- if (connection instanceof StreamDispatcher_1.StreamDispatcher)
53
- return connection.voiceConnection.destroy();
54
- return connection.destroy();
55
- }
56
- /**
57
- * Returns Discord Player voice connection
58
- * @param {Snowflake} guild The guild id
59
- * @returns {StreamDispatcher}
60
- */
61
- getConnection(guild) {
62
- return this.cache.get(guild);
63
- }
64
- }
65
- exports.VoiceUtils = VoiceUtils;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VoiceUtils = void 0;
4
+ const discord_js_1 = require("discord.js");
5
+ const voice_1 = require("@discordjs/voice");
6
+ const StreamDispatcher_1 = require("./StreamDispatcher");
7
+ class VoiceUtils {
8
+ /**
9
+ * The voice utils
10
+ * @private
11
+ */
12
+ constructor() {
13
+ /**
14
+ * The cache where voice utils stores stream managers
15
+ * @type {Collection<Snowflake, StreamDispatcher>}
16
+ */
17
+ this.cache = new discord_js_1.Collection();
18
+ }
19
+ /**
20
+ * Joins a voice channel, creating basic stream dispatch manager
21
+ * @param {StageChannel|VoiceChannel} channel The voice channel
22
+ * @param {object} [options] Join options
23
+ * @returns {Promise<StreamDispatcher>}
24
+ */
25
+ async connect(channel, options) {
26
+ const conn = await this.join(channel, options);
27
+ const sub = new StreamDispatcher_1.StreamDispatcher(conn, channel, options.maxTime);
28
+ this.cache.set(channel.guild.id, sub);
29
+ return sub;
30
+ }
31
+ /**
32
+ * Joins a voice channel
33
+ * @param {StageChannel|VoiceChannel} [channel] The voice/stage channel to join
34
+ * @param {object} [options] Join options
35
+ * @returns {VoiceConnection}
36
+ */
37
+ async join(channel, options) {
38
+ const conn = (0, voice_1.joinVoiceChannel)({
39
+ guildId: channel.guild.id,
40
+ channelId: channel.id,
41
+ adapterCreator: channel.guild.voiceAdapterCreator,
42
+ selfDeaf: Boolean(options.deaf)
43
+ });
44
+ return conn;
45
+ }
46
+ /**
47
+ * Disconnects voice connection
48
+ * @param {VoiceConnection} connection The voice connection
49
+ * @returns {void}
50
+ */
51
+ disconnect(connection) {
52
+ if (connection instanceof StreamDispatcher_1.StreamDispatcher)
53
+ return connection.voiceConnection.destroy();
54
+ return connection.destroy();
55
+ }
56
+ /**
57
+ * Returns Discord Player voice connection
58
+ * @param {Snowflake} guild The guild id
59
+ * @returns {StreamDispatcher}
60
+ */
61
+ getConnection(guild) {
62
+ return this.cache.get(guild);
63
+ }
64
+ }
65
+ exports.VoiceUtils = VoiceUtils;