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

135 lines
2.9 KiB
TypeScript
Raw Normal View History

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-09 17:59:14 +05:00
/**
* The player that instantiated this Track
2021-04-21 10:08:33 +05:00
* @type {Player}
2021-04-09 17:59:14 +05:00
*/
2021-04-04 22:36:40 +05:00
public player!: Player;
2021-04-19 18:32:10 +05:00
/**
* Title of this track
2021-04-21 11:52:59 +05:00
* @type {String}
2021-04-19 18:32:10 +05:00
*/
2021-04-04 22:36:40 +05:00
public title!: string;
2021-04-19 18:32:10 +05:00
/**
* Description of this track
2021-04-21 11:52:59 +05:00
* @type {String}
2021-04-19 18:32:10 +05:00
*/
2021-04-04 22:36:40 +05:00
public description!: string;
2021-04-19 18:32:10 +05:00
/**
* Author of this track
2021-04-21 11:52:59 +05:00
* @type {String}
2021-04-19 18:32:10 +05:00
*/
2021-04-04 22:36:40 +05:00
public author!: string;
2021-04-19 18:32:10 +05:00
/**
* Link of this track
2021-04-21 11:52:59 +05:00
* @type {String}
2021-04-19 18:32:10 +05:00
*/
2021-04-04 22:36:40 +05:00
public url!: string;
2021-04-19 18:32:10 +05:00
/**
* Thumbnail of this track
2021-04-21 11:52:59 +05:00
* @type {String}
2021-04-19 18:32:10 +05:00
*/
2021-04-04 22:36:40 +05:00
public thumbnail!: string;
2021-04-19 18:32:10 +05:00
/**
* Duration of this track
2021-04-21 11:52:59 +05:00
* @type {String}
2021-04-19 18:32:10 +05:00
*/
2021-04-04 22:36:40 +05:00
public duration!: string;
2021-04-19 18:32:10 +05:00
/**
* View count of this track
2021-04-21 11:52:59 +05:00
* @type {Number}
2021-04-19 18:32:10 +05:00
*/
2021-04-04 22:36:40 +05:00
public views!: number;
2021-04-19 18:32:10 +05:00
/**
* Person who requested this track
2021-04-21 10:08:33 +05:00
* @type {Discord.User}
2021-04-19 18:32:10 +05:00
*/
2021-04-04 22:36:40 +05:00
public requestedBy!: User;
2021-04-19 18:32:10 +05:00
/**
* If this track belongs to a playlist
2021-04-21 11:52:59 +05:00
* @type {Boolean}
2021-04-19 18:32:10 +05:00
*/
2021-04-04 22:36:40 +05:00
public fromPlaylist!: boolean;
2021-04-19 18:32:10 +05:00
/**
* Raw data of this track
2021-04-21 10:08:33 +05:00
* @type {TrackData}
2021-04-19 18:32:10 +05:00
*/
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-06 17:58:46 +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: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-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 11:52:59 +05:00
* @type {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;