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

150 lines
3.5 KiB
TypeScript
Raw Normal View History

2021-05-01 12:59:42 +05:00
// @ts-nocheck
2021-04-06 20:38:17 +05:00
import { Player } from '../Player';
2021-04-06 17:58:46 +05:00
import { User } from 'discord.js';
import { TrackData } from '../types/types';
2021-04-19 18:32:10 +05:00
import Queue from './Queue';
2021-04-04 22:36:40 +05:00
2021-04-06 20:38:17 +05:00
export class Track {
2021-04-04 22:36:40 +05:00
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-19 18:32:10 +05:00
/**
* Track constructor
2021-04-21 10:08:33 +05:00
* @param {Player} player The player that instantiated this Track
* @param {TrackData} data Track data
2021-04-19 18:32:10 +05:00
*/
2021-04-05 11:45:55 +05:00
constructor(player: Player, data: TrackData) {
2021-04-21 14:10:44 +05:00
/**
2021-04-21 15:39:51 +05:00
* The player that instantiated this Track
2021-04-21 14:10:44 +05:00
* @name Track#player
* @type {Player}
* @readonly
*/
2021-04-06 17:58:46 +05:00
Object.defineProperty(this, 'player', { value: player, enumerable: false });
2021-04-04 22:36:40 +05:00
2021-04-21 14:10:44 +05:00
/**
* Title of this track
* @name Track#title
* @type {String}
*/
/**
* Description of this track
* @name Track#description
* @type {String}
*/
/**
* Author of this track
* @name Track#author
* @type {String}
*/
/**
* URL of this track
* @name Track#url
* @type {String}
*/
/**
* Thumbnail of this track
* @name Track#thumbnail
* @type {String}
*/
/**
* Duration of this track
* @name Track#duration
* @type {String}
*/
/**
* Views count of this track
* @name Track#views
* @type {Number}
*/
/**
* Person who requested this track
* @name Track#requestedBy
* @type {DiscordUser}
*/
/**
* If this track belongs to playlist
* @name Track#fromPlaylist
* @type {Boolean}
*/
/**
* Raw track data
* @name Track#raw
* @type {TrackData}
*/
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:58:46 +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
2021-04-06 17:58:46 +05:00
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
2021-04-21 10:08:33 +05:00
* @type {Queue}
2021-05-01 12:59:42 +05:00
* @readonly
2021-04-06 17:55:29 +05:00
*/
2021-04-19 18:32:10 +05:00
get queue(): Queue {
2021-04-06 17:58:46 +05:00
return this.player.queues.find((q) => q.tracks.includes(this));
2021-04-06 17:55:29 +05:00
}
/**
* The track duration in millisecond
2021-04-21 11:52:59 +05:00
* @type {Number}
2021-04-06 17:55:29 +05:00
*/
2021-04-19 18:32:10 +05:00
get durationMS(): number {
2021-04-06 17:55:29 +05:00
const times = (n: number, t: number) => {
let tn = 1;
for (let i = 0; i < t; i++) tn *= n;
return t <= 0 ? 1000 : tn * 1000;
};
2021-04-06 17:58:46 +05:00
return this.duration
.split(':')
.reverse()
.map((m, i) => parseInt(m) * times(60, i))
.reduce((a, c) => a + c, 0);
2021-04-06 17:55:29 +05:00
}
2021-04-19 18:32:10 +05:00
/**
* String representation of this track
2021-04-21 14:10:44 +05:00
* @returns {String}
2021-04-19 18:32:10 +05:00
*/
toString(): string {
2021-04-06 17:55:29 +05:00
return `${this.title} by ${this.author}`;
}
2021-04-04 22:44:45 +05:00
}
2021-04-06 20:38:17 +05:00
export default Track;