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

57 lines
1.5 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-11 15:35:04 +05:00
2021-06-13 19:31:27 +05:00
constructor(player: Player, data: PlaylistInitData) {
2021-06-11 15:35:04 +05:00
this.player = player;
2021-06-13 19:31:27 +05:00
this.tracks = data.tracks ?? [];
this.author = data.author;
this.description = data.description;
this.thumbnail = data.thumbnail;
this.type = data.type;
this.source = data.source;
this.id = data.id;
this.url = data.url;
this.title = data.title;
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
toJSON(withTracks = true) {
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[]
};
if (withTracks) payload.tracks = this.tracks.map((m) => m.toJSON());
return payload as PlaylistJSON;
}
2021-06-11 15:35:04 +05:00
}
export { Playlist };