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

139 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-06-11 15:35:04 +05:00
import { Player } from "../Player";
import { Track } from "./Track";
2021-06-13 19:31:27 +05:00
import { PlaylistInitData, PlaylistJSON, TrackJSON, TrackSource } from "../types/types";
2021-06-11 15:35:04 +05:00
class Playlist {
public readonly player: Player;
public tracks: Track[];
2021-06-13 19:31:27 +05:00
public title: string;
public description: string;
public thumbnail: string;
public type: "album" | "playlist";
public source: TrackSource;
public author: {
name: string;
url: string;
};
public id: string;
public url: string;
2021-06-22 15:24:05 +05:00
public readonly rawPlaylist?: any; // eslint-disable-line @typescript-eslint/no-explicit-any
2021-06-11 15:35:04 +05:00
2021-06-20 19:22:09 +05:00
/**
* Playlist constructor
* @param {Player} player The player
* @param {PlaylistInitData} data The data
*/
2021-06-13 19:31:27 +05:00
constructor(player: Player, data: PlaylistInitData) {
2021-06-20 19:22:09 +05:00
/**
* The player
* @name Playlist#player
* @type {Player}
* @readonly
*/
2021-06-11 15:35:04 +05:00
this.player = player;
2021-06-20 19:22:09 +05:00
/**
* The tracks in this playlist
* @name Playlist#tracks
* @type {Track[]}
*/
2021-06-13 19:31:27 +05:00
this.tracks = data.tracks ?? [];
2021-06-20 19:22:09 +05:00
/**
* The author of this playlist
* @name Playlist#author
* @type {object}
*/
2021-06-13 19:31:27 +05:00
this.author = data.author;
2021-06-20 19:22:09 +05:00
/**
* The description
* @name Playlist#description
* @type {string}
*/
2021-06-13 19:31:27 +05:00
this.description = data.description;
2021-06-20 19:22:09 +05:00
/**
* The thumbnail of this playlist
* @name Playlist#thumbnail
* @type {string}
*/
2021-06-13 19:31:27 +05:00
this.thumbnail = data.thumbnail;
2021-06-20 19:22:09 +05:00
/**
* The playlist type:
* - `album`
* - `playlist`
* @name Playlist#type
* @type {string}
*/
2021-06-13 19:31:27 +05:00
this.type = data.type;
2021-06-20 19:22:09 +05:00
/**
* The source of this playlist:
* - `youtube`
* - `soundcloud`
* - `spotify`
* - `arbitrary`
* @name Playlist#source
* @type {string}
*/
2021-06-13 19:31:27 +05:00
this.source = data.source;
2021-06-20 19:22:09 +05:00
/**
* The playlist id
* @name Playlist#id
* @type {string}
*/
2021-06-13 19:31:27 +05:00
this.id = data.id;
2021-06-20 19:22:09 +05:00
/**
* The playlist url
* @name Playlist#url
* @type {string}
*/
2021-06-13 19:31:27 +05:00
this.url = data.url;
2021-06-20 19:22:09 +05:00
/**
* The playlist title
* @type {string}
*/
2021-06-13 19:31:27 +05:00
this.title = data.title;
2021-06-20 19:22:09 +05:00
/**
* @name Playlist#rawPlaylist
* @type {any}
* @readonly
*/
2021-06-11 15:35:04 +05:00
}
2021-06-11 19:57:49 +05:00
*[Symbol.iterator]() {
yield* this.tracks;
}
2021-06-13 19:31:27 +05:00
2021-06-20 19:22:09 +05:00
/**
* JSON representation of this playlist
* @param {boolean} [withTracks=true] If it should build json with tracks
* @returns {PlaylistJSON}
*/
2021-08-11 10:44:36 +05:00
toJSON(withTracks = true) {
2021-06-13 19:31:27 +05:00
const payload = {
id: this.id,
url: this.url,
title: this.title,
description: this.description,
thumbnail: this.thumbnail,
type: this.type,
source: this.source,
author: this.author,
tracks: [] as TrackJSON[]
};
2021-08-11 10:44:36 +05:00
if (withTracks) payload.tracks = this.tracks.map((m) => m.toJSON(true));
2021-06-13 19:31:27 +05:00
return payload as PlaylistJSON;
}
2021-06-11 15:35:04 +05:00
}
export { Playlist };