diff --git a/README.md b/README.md index dfb90ca..3c11832 100644 --- a/README.md +++ b/README.md @@ -202,19 +202,18 @@ Here's an example on how you can use **[play-dl](https://npmjs.com/package/play- const playdl = require("play-dl"); // other code - -const queue = player.createQueue(...); -if (!queue.createStream) { - queue.createStream = async (track, source, _queue) => { +const queue = player.createQueue(..., { + ..., + async onBeforeCreateStream(track, source, _queue) { // only trap youtube source if (source === "youtube") { // track here would be youtube track return (await playdl.stream(track.url)).stream; // we must return readable stream or void (returning void means telling discord-player to look for default extractor) } - }; -} + } +}); ``` -`.createStream` is called before actually downloading the stream. It is a different concept from extractors, where you are **just** downloading -streams. `source` here will be a video source. Streams from `createStream` are then piped to `FFmpeg` and finally sent to Discord voice servers. \ No newline at end of file +`.onBeforeCreateStream` is called before actually downloading the stream. It is a different concept from extractors, where you are **just** downloading +streams. `source` here will be a video source. Streams from `onBeforeCreateStream` are then piped to `FFmpeg` and finally sent to Discord voice servers. \ No newline at end of file diff --git a/docs/extractors/create_stream.md b/docs/extractors/create_stream.md index dba70a2..ea211d8 100644 --- a/docs/extractors/create_stream.md +++ b/docs/extractors/create_stream.md @@ -12,28 +12,27 @@ Here's an example on how you can use **[play-dl](https://npmjs.com/package/play- const playdl = require("play-dl"); // other code - -const queue = player.createQueue(...); -if (!queue.createStream) { - queue.createStream = async (track, source, _queue) => { +const queue = player.createQueue(..., { + ..., + async onBeforeCreateStream(track, source, _queue) { // only trap youtube source if (source === "youtube") { // track here would be youtube track return (await playdl.stream(track.url)).stream; // we must return readable stream or void (returning void means telling discord-player to look for default extractor) } - }; -} + } +}); ``` -`.createStream` is called before actually downloading the stream. It is a different concept from extractors, where you are **just** downloading -streams. `source` here will be a video source. Streams from `createStream` are then piped to `FFmpeg` and finally sent to Discord voice servers. +`.onBeforeCreateStream` is called before actually downloading the stream. It is a different concept from extractors, where you are **just** downloading +streams. `source` here will be a video source. Streams from `onBeforeCreateStream` are then piped to `FFmpeg` and finally sent to Discord voice servers. # FAQ ## How can I remove this? > If you already made this change and want to switch to default mode in runtime, -> you can set `queue.createStream` to `null` which will make `discord-player` use default config. +> you can set `queue.onBeforeCreateStream` to `null` which will make `discord-player` use default config. ## Which stream format should I return? diff --git a/src/Structures/Queue.ts b/src/Structures/Queue.ts index 8d4740d..cca75c4 100644 --- a/src/Structures/Queue.ts +++ b/src/Structures/Queue.ts @@ -28,7 +28,7 @@ class Queue { private _filtersUpdate = false; #lastVolume = 0; #destroyed = false; - public createStream: (track: Track, source: TrackSource, queue: Queue) => Promise | Readable = null; + public onBeforeCreateStream: (track: Track, source: TrackSource, queue: Queue) => Promise = null; /** * Queue constructor @@ -110,6 +110,8 @@ class Queue { options ); + if ("onBeforeCreateStream" in this.options) this.onBeforeCreateStream = this.options.onBeforeCreateStream; + this.player.emit("debug", this, `Queue initialized:\n\n${this.player.scanDeps()}`); } @@ -633,7 +635,7 @@ class Queue { } let stream = null; - const customDownloader = typeof this.createStream === "function"; + const customDownloader = typeof this.onBeforeCreateStream === "function"; if (["youtube", "spotify"].includes(track.raw.source)) { if (track.raw.source === "spotify" && !track.raw.engine) { @@ -645,7 +647,7 @@ class Queue { if (!link) return void this.play(this.tracks.shift(), { immediate: true }); if (customDownloader) { - stream = (await this.createStream(track, "youtube", this)) ?? null; + stream = (await this.onBeforeCreateStream(track, "youtube", this)) ?? null; if (stream) stream = ytdl .arbitraryStream(stream, { @@ -670,7 +672,7 @@ class Queue { }); } } else { - const tryArb = (customDownloader && (await this.createStream(track, track.raw.source || track.raw.engine, this))) || null; + const tryArb = (customDownloader && (await this.onBeforeCreateStream(track, track.raw.source || track.raw.engine, this))) || null; const arbitrarySource = tryArb ? tryArb : track.raw.source === "soundcloud" diff --git a/src/types/types.ts b/src/types/types.ts index 4b7f807..694b0ee 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -128,6 +128,7 @@ export interface PlayerProgressbarOptions { * @property {YTDLDownloadOptions} [ytdlOptions={}] The youtube download options * @property {number} [initialVolume=100] The initial player volume * @property {number} [bufferingTimeout=3000] Buffering timeout for the stream + * @property {Function} [onBeforeCreateStream] Runs before creating stream */ export interface PlayerOptions { leaveOnEnd?: boolean; @@ -138,6 +139,7 @@ export interface PlayerOptions { ytdlOptions?: downloadOptions; initialVolume?: number; bufferingTimeout?: number; + onBeforeCreateStream?: (track: Track, source: TrackSource, queue: Queue) => Promise; } /**