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

66 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-04-06 17:55:29 +05:00
import Player from "../Player";
import { User } from "discord.js";
import { TrackData } from "../types/types";
2021-04-04 22:36:40 +05:00
export default class Track {
public player!: Player;
public title!: string;
public description!: string;
public author!: string;
public url!: string;
public thumbnail!: string;
public duration!: string;
public views!: number;
public requestedBy!: User;
public fromPlaylist!: boolean;
2021-04-05 11:45:55 +05:00
public raw!: TrackData;
2021-04-04 22:36:40 +05:00
2021-04-05 11:45:55 +05:00
constructor(player: Player, data: TrackData) {
2021-04-04 22:36:40 +05:00
/**
* The player that instantiated this Track
*/
2021-04-06 17:55:29 +05:00
Object.defineProperty(this, "player", { value: player, enumerable: false });
2021-04-04 22:36:40 +05:00
void this._patch(data);
}
2021-04-05 11:45:55 +05:00
private _patch(data: TrackData) {
2021-04-06 17:55:29 +05:00
this.title = data.title ?? "";
this.description = data.description ?? "";
this.author = data.author ?? "";
this.url = data.url ?? "";
this.thumbnail = data.thumbnail ?? "";
this.duration = data.duration ?? "";
2021-04-04 22:36:40 +05:00
this.views = data.views ?? 0;
this.requestedBy = data.requestedBy;
this.fromPlaylist = Boolean(data.fromPlaylist);
2021-04-05 11:45:55 +05:00
// raw
Object.defineProperty(this, "raw", { get: () => data, enumerable: false });
2021-04-04 22:36:40 +05:00
}
2021-04-06 17:55:29 +05:00
/**
* The queue in which this track is located
*/
get queue() {
return this.player.queues.find(q => q.tracks.includes(this));
}
/**
* The track duration in millisecond
*/
get durationMS() {
const times = (n: number, t: number) => {
let tn = 1;
for (let i = 0; i < t; i++) tn *= n;
return t <= 0 ? 1000 : tn * 1000;
};
return this.duration.split(":").reverse().map((m, i) => parseInt(m) * times(60, i)).reduce((a, c) => a + c, 0);
}
toString() {
return `${this.title} by ${this.author}`;
}
2021-04-04 22:44:45 +05:00
}