From 7afee914f3116a3271a0949ec255f96a93fad636 Mon Sep 17 00:00:00 2001 From: Snowflake107 Date: Sat, 26 Jun 2021 00:59:07 +0545 Subject: [PATCH] track id --- src/Structures/Queue.ts | 22 +++++++++++++++------- src/Structures/Track.ts | 5 +++-- src/types/types.ts | 1 + 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Structures/Queue.ts b/src/Structures/Queue.ts index 7e356d4..64ae509 100644 --- a/src/Structures/Queue.ts +++ b/src/Structures/Queue.ts @@ -1,4 +1,4 @@ -import { Collection, Guild, StageChannel, VoiceChannel } from "discord.js"; +import { Collection, Guild, StageChannel, VoiceChannel, Snowflake, SnowflakeUtil } from "discord.js"; import { Player } from "../Player"; import { StreamDispatcher } from "../VoiceInterface/StreamDispatcher"; import Track from "./Track"; @@ -19,6 +19,7 @@ class Queue { public playing = false; public metadata?: T = null; public repeatMode: QueueRepeatMode = 0; + public readonly id: Snowflake = SnowflakeUtil.generate(); private _streamTime = 0; public _cooldownsTimeout = new Collection(); private _activeFilters: any[] = []; // eslint-disable-line @typescript-eslint/no-explicit-any @@ -82,6 +83,12 @@ class Queue { * @name Queue#connection */ + /** + * The ID of this queue + * @type {Snowflake} + * @name Queue#id + */ + Object.assign( this.options, { @@ -444,21 +451,21 @@ class Queue { /** * Removes a track from the queue - * @param {Track|number} track The track to remove + * @param {Track|Snowflake|number} track The track to remove * @returns {Track} */ - remove(track: Track | number) { + remove(track: Track | Snowflake | number) { this.#watchDestroyed(); let trackFound: Track = null; if (typeof track === "number") { trackFound = this.tracks[track]; if (trackFound) { - this.tracks = this.tracks.filter((t) => t._trackID !== trackFound._trackID); + this.tracks = this.tracks.filter((t) => t.id !== trackFound.id); } } else { - trackFound = this.tracks.find((s) => s._trackID === track._trackID); + trackFound = this.tracks.find((s) => s.id === (track instanceof Track ? track.id : track)); if (trackFound) { - this.tracks = this.tracks.filter((s) => s._trackID !== trackFound._trackID); + this.tracks = this.tracks.filter((s) => s.id !== trackFound.id); } } @@ -567,7 +574,7 @@ class Queue { if (!track) return; if (!options.filtersUpdate) { - this.previousTracks = this.previousTracks.filter((x) => x._trackID !== track._trackID); + this.previousTracks = this.previousTracks.filter((x) => x.id !== track.id); this.previousTracks.push(track); } @@ -667,6 +674,7 @@ class Queue { toJSON() { this.#watchDestroyed(); return { + id: this.id, guild: this.guild.id, voiceChannel: this.connection?.channel?.id, options: this.options, diff --git a/src/Structures/Track.ts b/src/Structures/Track.ts index e42947f..a026ce3 100644 --- a/src/Structures/Track.ts +++ b/src/Structures/Track.ts @@ -1,4 +1,4 @@ -import { User, Util } from "discord.js"; +import { User, Util, SnowflakeUtil, Snowflake } from "discord.js"; import { Player } from "../Player"; import { RawTrackData, TrackJSON } from "../types/types"; import { Playlist } from "./Playlist"; @@ -16,7 +16,7 @@ class Track { public requestedBy!: User; public playlist?: Playlist; public readonly raw: RawTrackData = {} as RawTrackData; - public readonly _trackID = Date.now(); + public readonly id: Snowflake = SnowflakeUtil.generate(); /** * Track constructor @@ -164,6 +164,7 @@ class Track { */ toJSON(hidePlaylist?: boolean) { return { + id: this.id, title: this.title, description: this.description, author: this.author, diff --git a/src/types/types.ts b/src/types/types.ts index a041e70..f37a494 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -406,6 +406,7 @@ export interface PlaylistInitData { * @property {PlaylistJSON} [playlist] The playlist info (if any) */ export interface TrackJSON { + id: Snowflake; title: string; description: string; author: string;