fetchBeforeQueued

This commit is contained in:
Snowflake107 2021-05-13 13:55:11 +05:45
parent bead343493
commit 30af411c07
3 changed files with 66 additions and 14 deletions

View file

@ -203,6 +203,19 @@ export class Player extends EventEmitter {
source: 'spotify' source: 'spotify'
}); });
if (this.options.fetchBeforeQueued) {
const searchQueryString = this.options.disableArtistSearch
? spotifyTrack.title
: `${spotifyTrack.title}${' - ' + spotifyTrack.author}`;
const ytv = await YouTube.search(searchQueryString, { limit: 1, type: "video" }).catch(e => { });
if (ytv && ytv[0]) Util.define({
target: spotifyTrack,
prop: 'backupLink',
value: ytv[0].url
});
}
tracks = [spotifyTrack]; tracks = [spotifyTrack];
} }
} }
@ -219,7 +232,7 @@ export class Player extends EventEmitter {
let tracks: Track[] = []; let tracks: Track[] = [];
if (playlist.type !== 'playlist') if (playlist.type !== 'playlist')
tracks = playlist.tracks.items.map((m: any) => { tracks = await Promise.all(playlist.tracks.items.map(async (m: any) => {
const data = new Track(this, { const data = new Track(this, {
title: m.name ?? '', title: m.name ?? '',
description: m.description ?? '', description: m.description ?? '',
@ -235,10 +248,24 @@ export class Player extends EventEmitter {
fromPlaylist: true, fromPlaylist: true,
source: 'spotify' source: 'spotify'
}); });
if (this.options.fetchBeforeQueued) {
const searchQueryString = this.options.disableArtistSearch
? data.title
: `${data.title}${' - ' + data.author}`;
const ytv = await YouTube.search(searchQueryString, { limit: 1, type: "video" }).catch(e => { });
if (ytv && ytv[0]) Util.define({
target: data,
prop: 'backupLink',
value: ytv[0].url
});
}
return data; return data;
}); }));
else { else {
tracks = playlist.tracks.items.map((m: any) => { tracks = await Promise.all(playlist.tracks.items.map(async (m: any) => {
const data = new Track(this, { const data = new Track(this, {
title: m.track.name ?? '', title: m.track.name ?? '',
description: m.track.description ?? '', description: m.track.description ?? '',
@ -255,8 +282,21 @@ export class Player extends EventEmitter {
source: 'spotify' source: 'spotify'
}); });
if (this.options.fetchBeforeQueued) {
const searchQueryString = this.options.disableArtistSearch
? data.title
: `${data.title}${' - ' + data.author}`;
const ytv = await YouTube.search(searchQueryString, { limit: 1, type: "video" }).catch(e => {});
if (ytv && ytv[0]) Util.define({
target: data,
prop: 'backupLink',
value: ytv[0].url
});
}
return data; return data;
}); }));
} }
if (!tracks.length) return void this.emit(PlayerEvents.NO_RESULTS, message, query); if (!tracks.length) return void this.emit(PlayerEvents.NO_RESULTS, message, query);
@ -1285,11 +1325,7 @@ export class Player extends EventEmitter {
const searchQueryString = this.options.disableArtistSearch const searchQueryString = this.options.disableArtistSearch
? queue.playing.title ? queue.playing.title
: `${queue.playing.title}${' - ' + queue.playing.author}`; : `${queue.playing.title}${' - ' + queue.playing.author}`;
const yteqv = await Util.ytSearch(searchQueryString, { const yteqv = await YouTube.search(searchQueryString, { type: "video", limit: 1 }).catch(() => {});
player: this,
limit: 1,
user: queue.playing.requestedBy
}).catch(() => {});
if (!yteqv || !yteqv.length) if (!yteqv || !yteqv.length)
return void this.emit( return void this.emit(
@ -1300,11 +1336,10 @@ export class Player extends EventEmitter {
new PlayerError('Could not find alternative track on youtube!', 'SpotifyTrackError') new PlayerError('Could not find alternative track on youtube!', 'SpotifyTrackError')
); );
Object.defineProperty(queue.playing, 'backupLink', { Util.define({
value: yteqv[0].url, target: queue.playing,
writable: true, prop: 'backupLink',
enumerable: false, value: yteqv[0].url
configurable: true
}); });
} }
@ -1496,6 +1531,7 @@ export default Player;
* @property {Boolean} [useSafeSearch=false] If it should use `safe search` method for youtube searches * @property {Boolean} [useSafeSearch=false] If it should use `safe search` method for youtube searches
* @property {Boolean} [disableAutoRegister=false] If it should disable auto-registeration of `@discord-player/extractor` * @property {Boolean} [disableAutoRegister=false] If it should disable auto-registeration of `@discord-player/extractor`
* @property {Boolean} [disableArtistSearch=false] If it should disable artist search for spotify * @property {Boolean} [disableArtistSearch=false] If it should disable artist search for spotify
* @property {Boolean} [fetchBeforeQueued=false] If it should fetch all songs loaded from spotify before playing
*/ */
/** /**

View file

@ -14,6 +14,7 @@ export interface PlayerOptions {
useSafeSearch?: boolean; useSafeSearch?: boolean;
disableAutoRegister?: boolean; disableAutoRegister?: boolean;
disableArtistSearch?: boolean; disableArtistSearch?: boolean;
fetchBeforeQueued?: boolean;
} }
export type FiltersName = keyof QueueFilters; export type FiltersName = keyof QueueFilters;

View file

@ -204,6 +204,21 @@ export class Util {
return null; return null;
} }
} }
/**
*
* @param target
* @param prop
* @param value
*/
static define(ops: { target: any, prop: any, value: any, enumerate?: boolean }) {
Object.defineProperty(ops.target, ops.prop, {
value: ops.value,
writable: true,
enumerable: Boolean(ops.enumerate),
configurable: true
});
}
} }
export default Util; export default Util;