From 7657ffedefc5dfe4bc6b8373ec2690b442d8f760 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Lidwin?= <62100117+imLinguin@users.noreply.github.com> Date: Sun, 8 Nov 2020 14:17:14 +0100 Subject: [PATCH] :bug: Fixed author and duration undefined errors (#141) --- src/Track.js | 166 +++++++++++++++++++++++++++------------------------ 1 file changed, 88 insertions(+), 78 deletions(-) diff --git a/src/Track.js b/src/Track.js index 39e815d..1d8e280 100644 --- a/src/Track.js +++ b/src/Track.js @@ -6,89 +6,99 @@ const Player = require('./Player') * Represents a track. */ class Track { + /** + * @param {Object} videoData The video data for this track + * @param {Discord.User | null} user The user who requested the track + * @param {Player} player + */ + constructor(videoData, user, player) { /** - * @param {Object} videoData The video data for this track - * @param {Discord.User | null} user The user who requested the track - * @param {Player} player + * The player instantiating the track + * @type {Player} */ - constructor (videoData, user, player) { - /** - * The player instantiating the track - * @type {Player} - */ - this.player = player - /** - * The track title - * @type {string} - */ - this.title = videoData.title - /** - * The Youtube URL of the track - * @type {string} - */ - this.url = videoData.url - /** - * The video duration (formatted). - * @type {string} - */ - this.duration = videoData.durationFormatted - /** - * The video description - * @type {string} - */ - this.description = videoData.description - /** - * The video thumbnail - * @type {string} - */ - this.thumbnail = typeof videoData.thumbnail === 'object' ? videoData.thumbnail.url : videoData.thumbnail - /** - * The video views - * @type {?number} - */ - this.views = parseInt(videoData.views) - /** - * The video channel - * @type {string} - */ - this.author = videoData.channel.name - /** - * The user who requested the track - * @type {Discord.User?} - */ - this.requestedBy = user - /** - * Whether the track was added from a playlist - * @type {boolean} - */ - this.fromPlaylist = videoData.fromPlaylist || false - } + this.player = player + /** + * The track title + * @type {string} + */ + this.title = videoData.title + /** + * The Youtube URL of the track + * @type {string} + */ + this.url = videoData.url + /** + * The video duration (formatted). + * @type {string} + */ + this.duration = + videoData.durationFormatted || + `${Math.floor(parseInt(videoData.videoDetails.lengthSeconds) / 60)}:${ + parseInt(videoData.videoDetails.lengthSeconds) % 60 + }` + /** + * The video description + * @type {string} + */ + this.description = videoData.description; + /** + * The video thumbnail + * @type {string} + */ + this.thumbnail = + typeof videoData.thumbnail === "object" + ? videoData.thumbnail.url + : videoData.thumbnail + /** + * The video views + * @type {?number} + */ + this.views = parseInt(videoData.views) + /** + * The video channel + * @type {string} + */ + this.author = videoData.channel + ? videoData.channel.name + : videoData.author.name + /** + * The user who requested the track + * @type {Discord.User?} + */ + this.requestedBy = user + /** + * Whether the track was added from a playlist + * @type {boolean} + */ + this.fromPlaylist = videoData.fromPlaylist || false + } - /** - * The queue in which the track is - * @type {Queue} - */ - get queue () { - return this.player.queues.find((queue) => queue.tracks.includes(this)) - } + /** + * The queue in which the track is + * @type {Queue} + */ + get queue() { + return this.player.queues.find((queue) => queue.tracks.includes(this)) + } - /** - * The track duration - * @type {number} - */ - get durationMS () { - const args = this.duration.split(':') - if (args.length === 3) { - return parseInt(args[0]) * 60 * 60 * 1000 + - parseInt(args[1]) * 60 * 1000 + - parseInt(args[2]) * 1000 - } else if (args.length === 2) { - return parseInt(args[0]) * 60 * 1000 + - parseInt(args[1]) * 1000 - } else { - return parseInt(args[0]) * 1000 - } + /** + * The track duration + * @type {number} + */ + get durationMS() { + const args = this.duration.split(":") + if (args.length === 3) { + return ( + parseInt(args[0]) * 60 * 60 * 1000 + + parseInt(args[1]) * 60 * 1000 + + parseInt(args[2]) * 1000 + ); + } else if (args.length === 2) { + return parseInt(args[0]) * 60 * 1000 + parseInt(args[1]) * 1000 + } else { + return parseInt(args[0]) * 1000 } + } } module.exports = Track