disrexom
Version:
A Discord.js module to simplify your music commands and play songs with audio filters on Discord without any API key. Support YouTube, SoundCloud, Bandcamp, Facebook, and 700+ more sites
104 lines • 3.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Playlist = void 0;
const __1 = require("..");
/**
* Class representing a playlist.
* @prop {string} source Playlist source
*/
class Playlist {
/**
* Create a playlist
* @param {Song[]|PlaylistInfo} playlist Playlist
* @param {Discord.GuildMember} member Requested user
* @param {Object} properties Custom properties
*/
constructor(playlist, member, properties = {}) {
var _a;
if (typeof playlist !== "object") {
throw new __1.DisTubeError("INVALID_TYPE", ["Array<Song>", "object"], playlist, "playlist");
}
if (typeof properties !== "object") {
throw new __1.DisTubeError("INVALID_TYPE", "object", properties, "properties");
}
// FIXME
const info = playlist;
/**
* The source of the playlist
* @type {string}
*/
this.source = (info.source || properties.source || "youtube").toLowerCase();
/**
* Playlist songs.
* @type {Array<Song>}
*/
this.songs = Array.isArray(info) ? info : info.items || info.songs;
if (!Array.isArray(this.songs) || !this.songs.length) {
throw new __1.DisTubeError("EMPTY_PLAYLIST");
}
this._patchMember(member || info.member);
/**
* Playlist name.
* @type {string}
*/
this.name =
info.name ||
info.title ||
(this.songs[0].name
? `${this.songs[0].name} and ${this.songs.length - 1} more songs.`
: `${this.songs.length} songs playlist`);
/**
* Playlist URL.
* @type {string}
*/
this.url = info.url || info.webpage_url;
/**
* Playlist thumbnail.
* @type {?string}
*/
this.thumbnail = ((_a = info.thumbnail) === null || _a === void 0 ? void 0 : _a.url) || info.thumbnail || this.songs[0].thumbnail;
for (const [key, value] of Object.entries(properties)) {
this[key] = value;
}
}
/**
* Playlist duration in second.
* @type {number}
*/
get duration() {
var _a;
return ((_a = this.songs) === null || _a === void 0 ? void 0 : _a.reduce((prev, next) => prev + (next.duration || 0), 0)) || 0;
}
/**
* Formatted duration string `hh:mm:ss`.
* @type {string}
*/
get formattedDuration() {
return (0, __1.formatDuration)(this.duration);
}
/**
* @param {?Discord.GuildMember} [member] Requested user
* @private
* @returns {Playlist}
*/
_patchMember(member) {
var _a;
if (member) {
/**
* User requested.
* @type {?Discord.GuildMember}
*/
this.member = member;
/**
* User requested.
* @type {?Discord.User}
*/
this.user = (_a = this.member) === null || _a === void 0 ? void 0 : _a.user;
}
this.songs.map(s => s.constructor.name === "Song" && s._patchPlaylist(this, this.member));
return this;
}
}
exports.Playlist = Playlist;
exports.default = Playlist;
//# sourceMappingURL=Playlist.js.map