refactor(Queue): rename createStream to onBeforeCreateStream
This commit is contained in:
parent
ddf279e0cb
commit
ef239229d4
4 changed files with 23 additions and 21 deletions
13
README.md
13
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");
|
const playdl = require("play-dl");
|
||||||
|
|
||||||
// other code
|
// other code
|
||||||
|
const queue = player.createQueue(..., {
|
||||||
const queue = player.createQueue(...);
|
...,
|
||||||
if (!queue.createStream) {
|
async onBeforeCreateStream(track, source, _queue) {
|
||||||
queue.createStream = async (track, source, _queue) => {
|
|
||||||
// only trap youtube source
|
// only trap youtube source
|
||||||
if (source === "youtube") {
|
if (source === "youtube") {
|
||||||
// track here would be youtube track
|
// track here would be youtube track
|
||||||
return (await playdl.stream(track.url)).stream;
|
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)
|
// we must return readable stream or void (returning void means telling discord-player to look for default extractor)
|
||||||
}
|
}
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
`<Queue>.createStream` is called before actually downloading the stream. It is a different concept from extractors, where you are **just** downloading
|
`<Queue>.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 `createStream` are then piped to `FFmpeg` and finally sent to Discord voice servers.
|
streams. `source` here will be a video source. Streams from `onBeforeCreateStream` are then piped to `FFmpeg` and finally sent to Discord voice servers.
|
|
@ -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");
|
const playdl = require("play-dl");
|
||||||
|
|
||||||
// other code
|
// other code
|
||||||
|
const queue = player.createQueue(..., {
|
||||||
const queue = player.createQueue(...);
|
...,
|
||||||
if (!queue.createStream) {
|
async onBeforeCreateStream(track, source, _queue) {
|
||||||
queue.createStream = async (track, source, _queue) => {
|
|
||||||
// only trap youtube source
|
// only trap youtube source
|
||||||
if (source === "youtube") {
|
if (source === "youtube") {
|
||||||
// track here would be youtube track
|
// track here would be youtube track
|
||||||
return (await playdl.stream(track.url)).stream;
|
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)
|
// we must return readable stream or void (returning void means telling discord-player to look for default extractor)
|
||||||
}
|
}
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
`<Queue>.createStream` is called before actually downloading the stream. It is a different concept from extractors, where you are **just** downloading
|
`<Queue>.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 `createStream` are then piped to `FFmpeg` and finally sent to Discord voice servers.
|
streams. `source` here will be a video source. Streams from `onBeforeCreateStream` are then piped to `FFmpeg` and finally sent to Discord voice servers.
|
||||||
|
|
||||||
# FAQ
|
# FAQ
|
||||||
## How can I remove this?
|
## How can I remove this?
|
||||||
|
|
||||||
> If you already made this change and want to switch to default mode in runtime,
|
> 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?
|
## Which stream format should I return?
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@ class Queue<T = unknown> {
|
||||||
private _filtersUpdate = false;
|
private _filtersUpdate = false;
|
||||||
#lastVolume = 0;
|
#lastVolume = 0;
|
||||||
#destroyed = false;
|
#destroyed = false;
|
||||||
public createStream: (track: Track, source: TrackSource, queue: Queue) => Promise<Readable> | Readable = null;
|
public onBeforeCreateStream: (track: Track, source: TrackSource, queue: Queue) => Promise<Readable> = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queue constructor
|
* Queue constructor
|
||||||
|
@ -110,6 +110,8 @@ class Queue<T = unknown> {
|
||||||
options
|
options
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if ("onBeforeCreateStream" in this.options) this.onBeforeCreateStream = this.options.onBeforeCreateStream;
|
||||||
|
|
||||||
this.player.emit("debug", this, `Queue initialized:\n\n${this.player.scanDeps()}`);
|
this.player.emit("debug", this, `Queue initialized:\n\n${this.player.scanDeps()}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -633,7 +635,7 @@ class Queue<T = unknown> {
|
||||||
}
|
}
|
||||||
|
|
||||||
let stream = null;
|
let stream = null;
|
||||||
const customDownloader = typeof this.createStream === "function";
|
const customDownloader = typeof this.onBeforeCreateStream === "function";
|
||||||
|
|
||||||
if (["youtube", "spotify"].includes(track.raw.source)) {
|
if (["youtube", "spotify"].includes(track.raw.source)) {
|
||||||
if (track.raw.source === "spotify" && !track.raw.engine) {
|
if (track.raw.source === "spotify" && !track.raw.engine) {
|
||||||
|
@ -645,7 +647,7 @@ class Queue<T = unknown> {
|
||||||
if (!link) return void this.play(this.tracks.shift(), { immediate: true });
|
if (!link) return void this.play(this.tracks.shift(), { immediate: true });
|
||||||
|
|
||||||
if (customDownloader) {
|
if (customDownloader) {
|
||||||
stream = (await this.createStream(track, "youtube", this)) ?? null;
|
stream = (await this.onBeforeCreateStream(track, "youtube", this)) ?? null;
|
||||||
if (stream)
|
if (stream)
|
||||||
stream = ytdl
|
stream = ytdl
|
||||||
.arbitraryStream(stream, {
|
.arbitraryStream(stream, {
|
||||||
|
@ -670,7 +672,7 @@ class Queue<T = unknown> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} 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
|
const arbitrarySource = tryArb
|
||||||
? tryArb
|
? tryArb
|
||||||
: track.raw.source === "soundcloud"
|
: track.raw.source === "soundcloud"
|
||||||
|
|
|
@ -128,6 +128,7 @@ export interface PlayerProgressbarOptions {
|
||||||
* @property {YTDLDownloadOptions} [ytdlOptions={}] The youtube download options
|
* @property {YTDLDownloadOptions} [ytdlOptions={}] The youtube download options
|
||||||
* @property {number} [initialVolume=100] The initial player volume
|
* @property {number} [initialVolume=100] The initial player volume
|
||||||
* @property {number} [bufferingTimeout=3000] Buffering timeout for the stream
|
* @property {number} [bufferingTimeout=3000] Buffering timeout for the stream
|
||||||
|
* @property {Function} [onBeforeCreateStream] Runs before creating stream
|
||||||
*/
|
*/
|
||||||
export interface PlayerOptions {
|
export interface PlayerOptions {
|
||||||
leaveOnEnd?: boolean;
|
leaveOnEnd?: boolean;
|
||||||
|
@ -138,6 +139,7 @@ export interface PlayerOptions {
|
||||||
ytdlOptions?: downloadOptions;
|
ytdlOptions?: downloadOptions;
|
||||||
initialVolume?: number;
|
initialVolume?: number;
|
||||||
bufferingTimeout?: number;
|
bufferingTimeout?: number;
|
||||||
|
onBeforeCreateStream?: (track: Track, source: TrackSource, queue: Queue) => Promise<Readable>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue