refactor(Queue): Remove queue from createProgressBar

This commit is contained in:
Snowflake107 2021-07-05 12:13:06 +05:45
parent a9cbc9df01
commit 6e6bd432c0
No known key found for this signature in database
GPG key ID: FA40E3EC5CB6DCD6
3 changed files with 13 additions and 22 deletions

View file

@ -2,7 +2,7 @@ import { Client, Collection, GuildResolvable, Snowflake, User, VoiceState } from
import { TypedEmitter as EventEmitter } from "tiny-typed-emitter"; import { TypedEmitter as EventEmitter } from "tiny-typed-emitter";
import { Queue } from "./Structures/Queue"; import { Queue } from "./Structures/Queue";
import { VoiceUtils } from "./VoiceInterface/VoiceUtils"; import { VoiceUtils } from "./VoiceInterface/VoiceUtils";
import { PlayerEvents, PlayerOptions, QueryType, SearchOptions, DiscordPlayerInitOptions } from "./types/types"; import { PlayerEvents, PlayerOptions, QueryType, SearchOptions, PlayerInitOptions } from "./types/types";
import Track from "./Structures/Track"; import Track from "./Structures/Track";
import { QueryResolver } from "./utils/QueryResolver"; import { QueryResolver } from "./utils/QueryResolver";
import YouTube from "youtube-sr"; import YouTube from "youtube-sr";
@ -19,7 +19,7 @@ const soundcloud = new SoundCloud();
class Player extends EventEmitter<PlayerEvents> { class Player extends EventEmitter<PlayerEvents> {
public readonly client: Client; public readonly client: Client;
public readonly options: DiscordPlayerInitOptions = { public readonly options: PlayerInitOptions = {
autoRegisterExtractor: true, autoRegisterExtractor: true,
ytdlOptions: {} ytdlOptions: {}
}; };
@ -30,9 +30,9 @@ class Player extends EventEmitter<PlayerEvents> {
/** /**
* Creates new Discord Player * Creates new Discord Player
* @param {Client} client The Discord Client * @param {Client} client The Discord Client
* @param {DiscordPlayerInitOptions} [options={}] The player init options * @param {PlayerInitOptions} [options={}] The player init options
*/ */
constructor(client: Client, options: DiscordPlayerInitOptions = {}) { constructor(client: Client, options: PlayerInitOptions = {}) {
super(); super();
/** /**

View file

@ -522,20 +522,16 @@ class Queue<T = unknown> {
/** /**
* Returns player stream timestamp * Returns player stream timestamp
* @param {boolean} [queue=false] If it should generate timestamp for the queue
* @returns {PlayerTimestamp} * @returns {PlayerTimestamp}
*/ */
getPlayerTimestamp(queue = false) { getPlayerTimestamp() {
this.#watchDestroyed(); this.#watchDestroyed();
const previousTracksTime = this.previousTracks.length > 0 ? this.previousTracks.reduce((p, c) => p + c.durationMS, 0) : 0; const currentStreamTime = this.streamTime;
const currentStreamTime = queue ? previousTracksTime + this.streamTime : this.streamTime; const totalTime = this.current.durationMS;
const totalTracksTime = this.totalTime;
const totalTime = queue ? previousTracksTime + totalTracksTime : this.current.durationMS;
const currentTimecode = Util.buildTimeCode(Util.parseMS(currentStreamTime)); const currentTimecode = Util.buildTimeCode(Util.parseMS(currentStreamTime));
const endTimecode = Util.buildTimeCode(Util.parseMS(totalTime)); const endTimecode = Util.buildTimeCode(Util.parseMS(totalTime));
// why
return { return {
current: currentTimecode, current: currentTimecode,
end: endTimecode, end: endTimecode,
@ -548,15 +544,11 @@ class Queue<T = unknown> {
* @param {PlayerProgressbarOptions} options The progress bar options * @param {PlayerProgressbarOptions} options The progress bar options
* @returns {string} * @returns {string}
*/ */
createProgressBar(options: PlayerProgressbarOptions = { timecodes: true, queue: false }) { createProgressBar(options: PlayerProgressbarOptions = { timecodes: true }) {
this.#watchDestroyed(); this.#watchDestroyed();
const previousTracksTime = this.previousTracks.length > 0 ? this.previousTracks.reduce((p, c) => p + c.durationMS, 0) : 0;
const currentStreamTime = options.queue ? previousTracksTime + this.streamTime : this.streamTime;
const totalTracksTime = this.totalTime;
const totalTime = options.queue ? previousTracksTime + totalTracksTime : this.current.durationMS;
const length = typeof options.length === "number" ? (options.length <= 0 || options.length === Infinity ? 15 : options.length) : 15; const length = typeof options.length === "number" ? (options.length <= 0 || options.length === Infinity ? 15 : options.length) : 15;
const index = Math.round((currentStreamTime / totalTime) * length); const index = Math.round((this.streamTime / this.current.durationMS) * length);
const indicator = typeof options.indicator === "string" && options.indicator.length > 0 ? options.indicator : "🔘"; const indicator = typeof options.indicator === "string" && options.indicator.length > 0 ? options.indicator : "🔘";
const line = typeof options.line === "string" && options.line.length > 0 ? options.line : "▬"; const line = typeof options.line === "string" && options.line.length > 0 ? options.line : "▬";
@ -564,14 +556,14 @@ class Queue<T = unknown> {
const bar = line.repeat(length - 1).split(""); const bar = line.repeat(length - 1).split("");
bar.splice(index, 0, indicator); bar.splice(index, 0, indicator);
if (options.timecodes) { if (options.timecodes) {
const timestamp = this.getPlayerTimestamp(options.queue); const timestamp = this.getPlayerTimestamp();
return `${timestamp.current}${bar.join("")}${timestamp.end}`; return `${timestamp.current}${bar.join("")}${timestamp.end}`;
} else { } else {
return `${bar.join("")}`; return `${bar.join("")}`;
} }
} else { } else {
if (options.timecodes) { if (options.timecodes) {
const timestamp = this.getPlayerTimestamp(options.queue); const timestamp = this.getPlayerTimestamp();
return `${timestamp.current}${indicator}${line.repeat(length - 1)}${timestamp.end}`; return `${timestamp.current}${indicator}${line.repeat(length - 1)}${timestamp.end}`;
} else { } else {
return `${indicator}${line.repeat(length - 1)}`; return `${indicator}${line.repeat(length - 1)}`;

View file

@ -113,7 +113,6 @@ export interface TimeData {
*/ */
export interface PlayerProgressbarOptions { export interface PlayerProgressbarOptions {
timecodes?: boolean; timecodes?: boolean;
queue?: boolean;
length?: number; length?: number;
line?: string; line?: string;
indicator?: string; indicator?: string;
@ -449,11 +448,11 @@ export interface PlaylistJSON {
} }
/** /**
* @typedef {object} DiscordPlayerInitOptions * @typedef {object} PlayerInitOptions
* @property {boolean} [autoRegisterExtractor=true] If it should automatically register `@discord-player/extractor` * @property {boolean} [autoRegisterExtractor=true] If it should automatically register `@discord-player/extractor`
* @property {YTDLDownloadOptions} [ytdlOptions={}] The options passed to `ytdl-core` * @property {YTDLDownloadOptions} [ytdlOptions={}] The options passed to `ytdl-core`
*/ */
export interface DiscordPlayerInitOptions { export interface PlayerInitOptions {
autoRegisterExtractor?: boolean; autoRegisterExtractor?: boolean;
ytdlOptions?: downloadOptions; ytdlOptions?: downloadOptions;
} }