This commit is contained in:
Snowflake107 2021-06-26 00:59:07 +05:45
parent 2423ba72e1
commit 7afee914f3
3 changed files with 19 additions and 9 deletions

View file

@ -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 { Player } from "../Player";
import { StreamDispatcher } from "../VoiceInterface/StreamDispatcher"; import { StreamDispatcher } from "../VoiceInterface/StreamDispatcher";
import Track from "./Track"; import Track from "./Track";
@ -19,6 +19,7 @@ class Queue<T = unknown> {
public playing = false; public playing = false;
public metadata?: T = null; public metadata?: T = null;
public repeatMode: QueueRepeatMode = 0; public repeatMode: QueueRepeatMode = 0;
public readonly id: Snowflake = SnowflakeUtil.generate();
private _streamTime = 0; private _streamTime = 0;
public _cooldownsTimeout = new Collection<string, NodeJS.Timeout>(); public _cooldownsTimeout = new Collection<string, NodeJS.Timeout>();
private _activeFilters: any[] = []; // eslint-disable-line @typescript-eslint/no-explicit-any private _activeFilters: any[] = []; // eslint-disable-line @typescript-eslint/no-explicit-any
@ -82,6 +83,12 @@ class Queue<T = unknown> {
* @name Queue#connection * @name Queue#connection
*/ */
/**
* The ID of this queue
* @type {Snowflake}
* @name Queue#id
*/
Object.assign( Object.assign(
this.options, this.options,
{ {
@ -444,21 +451,21 @@ class Queue<T = unknown> {
/** /**
* Removes a track from the 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} * @returns {Track}
*/ */
remove(track: Track | number) { remove(track: Track | Snowflake | number) {
this.#watchDestroyed(); this.#watchDestroyed();
let trackFound: Track = null; let trackFound: Track = null;
if (typeof track === "number") { if (typeof track === "number") {
trackFound = this.tracks[track]; trackFound = this.tracks[track];
if (trackFound) { if (trackFound) {
this.tracks = this.tracks.filter((t) => t._trackID !== trackFound._trackID); this.tracks = this.tracks.filter((t) => t.id !== trackFound.id);
} }
} else { } 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) { 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<T = unknown> {
if (!track) return; if (!track) return;
if (!options.filtersUpdate) { 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); this.previousTracks.push(track);
} }
@ -667,6 +674,7 @@ class Queue<T = unknown> {
toJSON() { toJSON() {
this.#watchDestroyed(); this.#watchDestroyed();
return { return {
id: this.id,
guild: this.guild.id, guild: this.guild.id,
voiceChannel: this.connection?.channel?.id, voiceChannel: this.connection?.channel?.id,
options: this.options, options: this.options,

View file

@ -1,4 +1,4 @@
import { User, Util } from "discord.js"; import { User, Util, SnowflakeUtil, Snowflake } from "discord.js";
import { Player } from "../Player"; import { Player } from "../Player";
import { RawTrackData, TrackJSON } from "../types/types"; import { RawTrackData, TrackJSON } from "../types/types";
import { Playlist } from "./Playlist"; import { Playlist } from "./Playlist";
@ -16,7 +16,7 @@ class Track {
public requestedBy!: User; public requestedBy!: User;
public playlist?: Playlist; public playlist?: Playlist;
public readonly raw: RawTrackData = {} as RawTrackData; public readonly raw: RawTrackData = {} as RawTrackData;
public readonly _trackID = Date.now(); public readonly id: Snowflake = SnowflakeUtil.generate();
/** /**
* Track constructor * Track constructor
@ -164,6 +164,7 @@ class Track {
*/ */
toJSON(hidePlaylist?: boolean) { toJSON(hidePlaylist?: boolean) {
return { return {
id: this.id,
title: this.title, title: this.title,
description: this.description, description: this.description,
author: this.author, author: this.author,

View file

@ -406,6 +406,7 @@ export interface PlaylistInitData {
* @property {PlaylistJSON} [playlist] The playlist info (if any) * @property {PlaylistJSON} [playlist] The playlist info (if any)
*/ */
export interface TrackJSON { export interface TrackJSON {
id: Snowflake;
title: string; title: string;
description: string; description: string;
author: string; author: string;