rough structure
This commit is contained in:
parent
6bba82864c
commit
7a4c573ce4
4 changed files with 23 additions and 162 deletions
|
@ -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<Snowflake, Queue>();
|
||||
public Extractors = new Collection<string, ExtractorModel>();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
// @ts-nocheck
|
||||
|
||||
import { ExtractorModelData } from '../types/types';
|
||||
|
||||
class ExtractorModel {
|
||||
|
|
|
@ -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<void> {
|
||||
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 `<Queue ${this.guildID}>`;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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}`;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue