diff --git a/src/Player.ts b/src/Player.ts index 7742838..b732e1c 100644 --- a/src/Player.ts +++ b/src/Player.ts @@ -1,9 +1,13 @@ import { EventEmitter } from 'events'; -import { Client } from 'discord.js'; +import { Client, Collection, Snowflake, Message } from 'discord.js'; import Util from './utils/Util'; +import Queue from './Structures/Queue'; +import { ExtractorModel } from './Structures/ExtractorModel'; export class Player extends EventEmitter { public client: Client; + public queues = new Collection(); + public Extractors = new Collection(); constructor(client: Client) { super(); @@ -13,8 +17,19 @@ export class Player extends EventEmitter { enumerable: false }); - // check FFmpeg - void Util.alertFFmpeg(); + Util.alertFFmpeg(); + } + + public createQueue(message: Message) { + if (this.queues.has(message.guild.id)) return this.queues.get(message.guild.id); + + const queue = new Queue(this, message); + void this.queues.set(message.guild.id, queue); + return queue; + } + + public getQueue(message: Message) { + return this.queues.get(message.guild.id) ?? null; } diff --git a/src/Structures/ExtractorModel.ts b/src/Structures/ExtractorModel.ts index 7bce372..578470a 100644 --- a/src/Structures/ExtractorModel.ts +++ b/src/Structures/ExtractorModel.ts @@ -1,5 +1,3 @@ -// @ts-nocheck - import { ExtractorModelData } from '../types/types'; class ExtractorModel { diff --git a/src/Structures/Queue.ts b/src/Structures/Queue.ts index bfc1748..d83a369 100644 --- a/src/Structures/Queue.ts +++ b/src/Structures/Queue.ts @@ -1,107 +1,10 @@ -// @ts-nocheck - -import { Message, Snowflake, VoiceConnection } from 'discord.js'; -import AudioFilters from '../utils/AudioFilters'; import { Player } from '../Player'; -import { EventEmitter } from 'events'; -import { Track } from './Track'; -import { QueueFilters } from '../types/types'; -export class Queue extends EventEmitter { - public player!: Player; - public guildID: Snowflake; - public voiceConnection?: VoiceConnection; - public stream?: any; - public tracks: Track[]; - public previousTracks: Track[]; - public stopped: boolean; - public lastSkipped: boolean; - public volume: number; - public paused: boolean; - public repeatMode: boolean; - public loopMode: boolean; - public filters: QueueFilters; - public additionalStreamTime: number; - public firstMessage: Message; - - public autoPlay = false; - - constructor(player: Player, message: Message) { - super(); +export class Queue { + public readonly player: Player; + constructor(player: Player, data: any) { Object.defineProperty(this, 'player', { value: player, enumerable: false }); - - this.guildID = message.guild.id; - - this.voiceConnection = null; - - this.tracks = []; - - this.previousTracks = []; - - this.stopped = false; - - this.lastSkipped = false; - - this.volume = 100; - - this.paused = Boolean(this.voiceConnection?.dispatcher?.paused); - - this.repeatMode = false; - - this.loopMode = false; - - this.additionalStreamTime = 0; - - this.firstMessage = message; - - this.filters = {}; - - Object.keys(AudioFilters).forEach((fn) => { - this.filters[fn as keyof QueueFilters] = false; - }); - } - - - get playing(): Track { - return this.tracks[0]; - } - - - get calculatedVolume(): number { - return this.filters.normalizer ? this.volume + 70 : this.volume; - } - - get totalTime(): number { - return this.tracks.length > 0 ? this.tracks.map((t) => t.durationMS).reduce((p, c) => p + c) : 0; - } - - get currentStreamTime(): number { - return this.voiceConnection?.dispatcher?.streamTime + this.additionalStreamTime || 0; - } - - setFilters(filters: QueueFilters): Promise { - return this.player.setFilters(this.firstMessage, filters); - } - - getFiltersEnabled(): string[] { - const filters: string[] = []; - - for (const filter in this.filters) { - if (this.filters[filter as keyof QueueFilters] !== false) filters.push(filter); - } - - return filters; - } - - getFiltersDisabled(): string[] { - const enabled = this.getFiltersEnabled(); - - return Object.keys(this.filters).filter((f) => !enabled.includes(f)); - } - - toString(): string { - return ``; } } diff --git a/src/Structures/Track.ts b/src/Structures/Track.ts index 6f77e9d..253dffa 100644 --- a/src/Structures/Track.ts +++ b/src/Structures/Track.ts @@ -1,65 +1,10 @@ -// @ts-nocheck - import { Player } from '../Player'; -import { User } from 'discord.js'; -import { TrackData } from '../types/types'; -import Queue from './Queue'; export class Track { - public player!: Player; - public title!: string; - public description!: string; - public author!: string; - public url!: string; - public thumbnail!: string; - public duration!: string; - public views!: number; - public requestedBy!: User; - public fromPlaylist!: boolean; - public raw!: TrackData; + public readonly player: Player; - constructor(player: Player, data: TrackData) { - + constructor(player: Player, data: any) { Object.defineProperty(this, 'player', { value: player, enumerable: false }); - - void this._patch(data); - } - - private _patch(data: TrackData) { - this.title = data.title ?? ''; - this.description = data.description ?? ''; - this.author = data.author ?? ''; - this.url = data.url ?? ''; - this.thumbnail = data.thumbnail ?? ''; - this.duration = data.duration ?? ''; - this.views = data.views ?? 0; - this.requestedBy = data.requestedBy; - this.fromPlaylist = Boolean(data.fromPlaylist); - - // raw - Object.defineProperty(this, 'raw', { get: () => data, enumerable: false }); - } - - get queue(): Queue { - return this.player.queues.find((q) => q.tracks.includes(this)); - } - - get durationMS(): number { - const times = (n: number, t: number) => { - let tn = 1; - for (let i = 0; i < t; i++) tn *= n; - return t <= 0 ? 1000 : tn * 1000; - }; - - return this.duration - .split(':') - .reverse() - .map((m, i) => parseInt(m) * times(60, i)) - .reduce((a, c) => a + c, 0); - } - - toString(): string { - return `${this.title} by ${this.author}`; } }