refactor(Player): update search method
This commit is contained in:
parent
ce39e0e656
commit
6b3874b894
3 changed files with 36 additions and 32 deletions
|
@ -2,7 +2,7 @@ import { Client, Collection, GuildResolvable, Snowflake, User, VoiceState, Inten
|
||||||
import { TypedEmitter as EventEmitter } from "tiny-typed-emitter";
|
import { TypedEmitter as EventEmitter } from "tiny-typed-emitter";
|
||||||
import { Queue } from "./Structures/Queue";
|
import { Queue } from "./Structures/Queue";
|
||||||
import { VoiceUtils } from "./VoiceInterface/VoiceUtils";
|
import { VoiceUtils } from "./VoiceInterface/VoiceUtils";
|
||||||
import { PlayerEvents, PlayerOptions, QueryType, SearchOptions, PlayerInitOptions } from "./types/types";
|
import { PlayerEvents, PlayerOptions, QueryType, SearchOptions, PlayerInitOptions, PlayerSearchResult } from "./types/types";
|
||||||
import Track from "./Structures/Track";
|
import Track from "./Structures/Track";
|
||||||
import { QueryResolver } from "./utils/QueryResolver";
|
import { QueryResolver } from "./utils/QueryResolver";
|
||||||
import YouTube from "youtube-sr";
|
import YouTube from "youtube-sr";
|
||||||
|
@ -198,13 +198,40 @@ class Player extends EventEmitter<PlayerEvents> {
|
||||||
* Search tracks
|
* Search tracks
|
||||||
* @param {string|Track} query The search query
|
* @param {string|Track} query The search query
|
||||||
* @param {SearchOptions} options The search options
|
* @param {SearchOptions} options The search options
|
||||||
* @returns {Promise<SearchResult>}
|
* @returns {Promise<PlayerSearchResult>}
|
||||||
*/
|
*/
|
||||||
async search(query: string | Track, options: SearchOptions) {
|
async search(query: string | Track, options: SearchOptions): Promise<PlayerSearchResult> {
|
||||||
if (query instanceof Track) return { playlist: null, tracks: [query] };
|
if (query instanceof Track) return { playlist: query.playlist || null, tracks: [query] };
|
||||||
if (!options) throw new PlayerError("DiscordPlayer#search needs search options!", ErrorStatusCode.INVALID_ARG_TYPE);
|
if (!options) throw new PlayerError("DiscordPlayer#search needs search options!", ErrorStatusCode.INVALID_ARG_TYPE);
|
||||||
options.requestedBy = this.client.users.resolve(options.requestedBy);
|
options.requestedBy = this.client.users.resolve(options.requestedBy);
|
||||||
if (!("searchEngine" in options)) options.searchEngine = QueryType.AUTO;
|
if (!("searchEngine" in options)) options.searchEngine = QueryType.AUTO;
|
||||||
|
if (this.extractors.has(options.searchEngine)) {
|
||||||
|
const extractor = this.extractors.get(options.searchEngine);
|
||||||
|
if (!extractor.validate(query)) return { playlist: null, tracks: [] };
|
||||||
|
const data = await extractor.handle(query);
|
||||||
|
if (data && data.data.length) {
|
||||||
|
const playlist = !data.playlist
|
||||||
|
? null
|
||||||
|
: new Playlist(this, {
|
||||||
|
...data.playlist,
|
||||||
|
tracks: []
|
||||||
|
});
|
||||||
|
|
||||||
|
const tracks = data.data.map(
|
||||||
|
(m) =>
|
||||||
|
new Track(this, {
|
||||||
|
...m,
|
||||||
|
requestedBy: options.requestedBy as User,
|
||||||
|
duration: Util.buildTimeCode(Util.parseMS(m.duration)),
|
||||||
|
playlist: playlist
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
if (playlist) playlist.tracks = tracks;
|
||||||
|
|
||||||
|
return { playlist: playlist, tracks: tracks };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
for (const [_, extractor] of this.extractors) {
|
for (const [_, extractor] of this.extractors) {
|
||||||
|
|
28
src/types/EventEmitter.d.ts
vendored
28
src/types/EventEmitter.d.ts
vendored
|
@ -1,28 +0,0 @@
|
||||||
declare module "tiny-typed-emitter" {
|
|
||||||
export type ListenerSignature<L> = {
|
|
||||||
[E in keyof L]: (...args: any[]) => any;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type DefaultListener = {
|
|
||||||
[k: string]: (...args: any[]) => any;
|
|
||||||
};
|
|
||||||
|
|
||||||
export class TypedEmitter<L extends ListenerSignature<L> = DefaultListener> {
|
|
||||||
static defaultMaxListeners: number;
|
|
||||||
addListener<U extends keyof L>(event: U, listener: L[U]): this;
|
|
||||||
prependListener<U extends keyof L>(event: U, listener: L[U]): this;
|
|
||||||
prependOnceListener<U extends keyof L>(event: U, listener: L[U]): this;
|
|
||||||
removeListener<U extends keyof L>(event: U, listener: L[U]): this;
|
|
||||||
removeAllListeners(event?: keyof L): this;
|
|
||||||
once<U extends keyof L>(event: U, listener: L[U]): this;
|
|
||||||
on<U extends keyof L>(event: U, listener: L[U]): this;
|
|
||||||
off<U extends keyof L>(event: U, listener: L[U]): this;
|
|
||||||
emit<U extends keyof L>(event: U, ...args: Parameters<L[U]>): boolean;
|
|
||||||
eventNames<U extends keyof L>(): U[];
|
|
||||||
listenerCount(type: keyof L): number;
|
|
||||||
listeners<U extends keyof L>(type: U): L[U][];
|
|
||||||
rawListeners<U extends keyof L>(type: U): L[U][];
|
|
||||||
getMaxListeners(): number;
|
|
||||||
setMaxListeners(n: number): this;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -8,6 +8,11 @@ import { downloadOptions } from "ytdl-core";
|
||||||
|
|
||||||
export type FiltersName = keyof QueueFilters;
|
export type FiltersName = keyof QueueFilters;
|
||||||
|
|
||||||
|
export interface PlayerSearchResult {
|
||||||
|
playlist: Playlist | null;
|
||||||
|
tracks: Track[];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {AudioFilters} QueueFilters
|
* @typedef {AudioFilters} QueueFilters
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue