discord-player-play-dl/src/Structures/ExtractorModel.ts

72 lines
1.9 KiB
TypeScript
Raw Normal View History

2021-06-11 15:32:22 +05:00
import { ExtractorModelData } from "../types/types";
class ExtractorModel {
name: string;
private _raw: any;
/**
* Model for raw Discord Player extractors
2021-06-13 10:55:20 +05:00
* @param {string} extractorName Name of the extractor
* @param {object} data Extractor object
2021-06-11 15:32:22 +05:00
*/
constructor(extractorName: string, data: any) {
/**
* The extractor name
2021-06-13 10:55:20 +05:00
* @type {string}
2021-06-11 15:32:22 +05:00
*/
this.name = extractorName;
2021-06-20 19:22:09 +05:00
/**
* The raw model
* @name ExtractorModel#_raw
* @type {any}
* @private
*/
2021-06-11 15:32:22 +05:00
Object.defineProperty(this, "_raw", { value: data, configurable: false, writable: false, enumerable: false });
}
/**
* Method to handle requests from `Player.play()`
2021-06-13 10:55:20 +05:00
* @param {string} query Query to handle
2021-06-11 15:32:22 +05:00
* @returns {Promise<ExtractorModelData>}
*/
async handle(query: string): Promise<ExtractorModelData> {
const data = await this._raw.getInfo(query);
if (!data) return null;
return {
2021-06-14 18:50:36 +05:00
playlist: data.playlist ?? null,
data:
data.info?.map((m: any) => ({
title: m.title,
duration: m.duration,
thumbnail: m.thumbnail,
engine: m.engine,
views: m.views,
author: m.author,
description: m.description,
url: m.url
})) ?? []
2021-06-11 15:32:22 +05:00
};
}
/**
* Method used by Discord Player to validate query with this extractor
2021-06-13 10:55:20 +05:00
* @param {string} query The query to validate
* @returns {boolean}
2021-06-11 15:32:22 +05:00
*/
validate(query: string): boolean {
return Boolean(this._raw.validate(query));
}
/**
* The extractor version
2021-06-13 10:55:20 +05:00
* @type {string}
2021-06-11 15:32:22 +05:00
*/
get version(): string {
return this._raw.version ?? "0.0.0";
}
}
export { ExtractorModel };