Use manager instances for guild services

This commit is contained in:
Max Isom 2020-03-15 14:36:59 -05:00
parent 0cebca7917
commit 3408c7a0c2
13 changed files with 178 additions and 162 deletions

View file

@ -1,5 +1,4 @@
import {inject, injectable} from 'inversify';
import {VoiceConnection, VoiceChannel} from 'discord.js';
import {VoiceConnection, VoiceChannel, StreamDispatcher} from 'discord.js';
import {promises as fs, createWriteStream} from 'fs';
import {Readable, PassThrough} from 'stream';
import path from 'path';
@ -7,60 +6,44 @@ import hasha from 'hasha';
import ytdl from 'ytdl-core';
import {WriteStream} from 'fs-capacitor';
import ffmpeg from 'fluent-ffmpeg';
import {TYPES} from '../types';
import Queue, {QueuedSong} from './queue';
export enum Status {
Playing,
Paused,
Disconnected
export enum STATUS {
PLAYING,
PAUSED,
DISCONNECTED
}
export interface GuildPlayer {
status: Status;
voiceConnection: VoiceConnection | null;
}
@injectable()
export default class {
private readonly guildPlayers = new Map<string, GuildPlayer>();
public status = STATUS.DISCONNECTED;
private readonly queue: Queue;
private readonly cacheDir: string;
private voiceConnection: VoiceConnection | null = null;
private dispatcher: StreamDispatcher | null = null;
constructor(@inject(TYPES.Services.Queue) queue: Queue, @inject(TYPES.Config.CACHE_DIR) cacheDir: string) {
constructor(queue: Queue, cacheDir: string) {
this.queue = queue;
this.cacheDir = cacheDir;
}
async connect(guildId: string, channel: VoiceChannel): Promise<void> {
this.initGuild(guildId);
const guildPlayer = this.guildPlayers.get(guildId);
async connect(channel: VoiceChannel): Promise<void> {
const conn = await channel.join();
guildPlayer!.voiceConnection = conn;
this.guildPlayers.set(guildId, guildPlayer!);
this.voiceConnection = conn;
}
disconnect(guildId: string): void {
this.initGuild(guildId);
const guildPlayer = this.guildPlayers.get(guildId);
if (guildPlayer?.voiceConnection) {
guildPlayer.voiceConnection.disconnect();
disconnect(): void {
if (this.voiceConnection) {
this.voiceConnection.disconnect();
}
}
async seek(guildId: string, positionSeconds: number): Promise<void> {
const guildPlayer = this.get(guildId);
if (guildPlayer.voiceConnection === null) {
async seek(positionSeconds: number): Promise<void> {
if (this.voiceConnection === null) {
throw new Error('Not connected to a voice channel.');
}
const currentSong = this.getCurrentSong(guildId);
const currentSong = this.getCurrentSong();
if (!currentSong) {
throw new Error('No song currently playing');
@ -68,46 +51,52 @@ export default class {
await this.waitForCache(currentSong.url);
guildPlayer.voiceConnection.play(this.getCachedPath(currentSong.url), {seek: positionSeconds});
this.attachListeners(this.voiceConnection.play(this.getCachedPath(currentSong.url), {seek: positionSeconds}));
}
async play(guildId: string): Promise<void> {
const guildPlayer = this.get(guildId);
if (guildPlayer.voiceConnection === null) {
async play(): Promise<void> {
if (this.voiceConnection === null) {
throw new Error('Not connected to a voice channel.');
}
if (guildPlayer.status === Status.Playing) {
// Already playing, return
// Resume from paused state
if (this.status === STATUS.PAUSED && this.dispatcher) {
this.dispatcher.resume();
this.status = STATUS.PLAYING;
return;
}
const currentSong = this.getCurrentSong(guildId);
const currentSong = this.getCurrentSong();
if (!currentSong) {
throw new Error('Queue empty.');
}
let dispatcher: StreamDispatcher;
if (await this.isCached(currentSong.url)) {
this.get(guildId).voiceConnection!.play(this.getCachedPath(currentSong.url));
dispatcher = this.voiceConnection.play(this.getCachedPath(currentSong.url));
} else {
const stream = await this.getStream(currentSong.url);
this.get(guildId).voiceConnection!.play(stream, {type: 'webm/opus'});
dispatcher = this.voiceConnection.play(stream, {type: 'webm/opus'});
}
guildPlayer.status = Status.Playing;
this.attachListeners(dispatcher);
this.guildPlayers.set(guildId, guildPlayer);
this.status = STATUS.PLAYING;
this.dispatcher = dispatcher;
}
get(guildId: string): GuildPlayer {
this.initGuild(guildId);
pause(): void {
if (!this.dispatcher || this.status !== STATUS.PLAYING) {
throw new Error('Not currently playing.');
}
return this.guildPlayers.get(guildId) as GuildPlayer;
this.dispatcher.pause();
}
private getCurrentSong(guildId: string): QueuedSong|null {
const songs = this.queue.get(guildId);
private getCurrentSong(): QueuedSong|null {
const songs = this.queue.get();
if (songs.length === 0) {
return null;
@ -116,12 +105,6 @@ export default class {
return songs[0];
}
private initGuild(guildId: string): void {
if (!this.guildPlayers.get(guildId)) {
this.guildPlayers.set(guildId, {status: Status.Disconnected, voiceConnection: null});
}
}
private getCachedPath(url: string): string {
const hash = hasha(url);
return path.join(this.cacheDir, `${hash}.webm`);
@ -238,4 +221,23 @@ export default class {
return capacitor.createReadStream();
}
private attachListeners(stream: StreamDispatcher): void {
stream.on('speaking', async isSpeaking => {
// Automatically advance queued song at end
if (!isSpeaking && this.status === STATUS.PLAYING) {
if (this.queue.get().length > 0) {
this.queue.forward();
await this.play();
}
}
});
stream.on('close', () => {
// Remove dispatcher from guild player
this.dispatcher = null;
// TODO: set voiceConnection null as well?
});
}
}

View file

@ -1,4 +1,3 @@
import {injectable} from 'inversify';
import shuffle from 'array-shuffle';
export interface QueuedPlaylist {
@ -14,62 +13,40 @@ export interface QueuedSong {
playlist: QueuedPlaylist | null;
}
@injectable()
export default class {
private readonly guildQueues = new Map<string, QueuedSong[]>();
private readonly queuePositions = new Map<string, number>();
private queue: QueuedSong[] = [];
private position = 0;
forward(guildId: string): void {
const currentPosition = this.queuePositions.get(guildId);
if (currentPosition && currentPosition + 1 <= this.size(guildId)) {
this.queuePositions.set(guildId, currentPosition + 1);
forward(): void {
if (this.position + 1 <= this.size()) {
this.position++;
} else {
throw new Error('No songs in queue to forward to.');
}
}
back(guildId: string): void {
const currentPosition = this.queuePositions.get(guildId);
if (currentPosition && currentPosition - 1 >= 0) {
this.queuePositions.set(guildId, currentPosition - 1);
back(): void {
if (this.position - 1 >= 0) {
this.position--;
} else {
throw new Error('No songs in queue to go back to.');
}
}
get(guildId: string): QueuedSong[] {
const currentPosition = this.queuePositions.get(guildId);
if (currentPosition === undefined) {
return [];
}
const guildQueue = this.guildQueues.get(guildId);
if (!guildQueue) {
throw new Error('Bad state. Queue for guild exists but position does not.');
}
return guildQueue.slice(currentPosition);
get(): QueuedSong[] {
return this.queue.slice(this.position);
}
add(guildId: string, song: QueuedSong): void {
this.initQueue(guildId);
add(song: QueuedSong): void {
if (song.playlist) {
// Add to end of queue
this.guildQueues.set(guildId, [...this.guildQueues.get(guildId)!, song]);
} else if (this.guildQueues.get(guildId)!.length === 0) {
// Queue is currently empty
this.guildQueues.set(guildId, [song]);
this.queue.push(song);
} else {
// Not from playlist, add immediately
let insertAt = 0;
// Loop until playlist song
this.guildQueues.get(guildId)!.some(song => {
this.queue.some(song => {
if (song.playlist) {
return true;
}
@ -78,41 +55,26 @@ export default class {
return false;
});
this.guildQueues.set(guildId, [...this.guildQueues.get(guildId)!.slice(0, insertAt), song, ...this.guildQueues.get(guildId)!.slice(insertAt)]);
this.queue = [...this.queue.slice(0, insertAt), song, ...this.queue.slice(insertAt)];
}
}
shuffle(guildId: string): void {
const queue = this.guildQueues.get(guildId);
if (!queue) {
throw new Error('Queue doesn\'t exist yet.');
}
this.guildQueues.set(guildId, [queue[0], ...shuffle(queue.slice(1))]);
shuffle(): void {
this.queue = [this.queue[0], ...shuffle(this.queue.slice(1))];
}
clear(guildId: string): void {
this.initQueue(guildId);
const queue = this.guildQueues.get(guildId);
clear(): void {
const newQueue = [];
if (queue!.length > 0) {
newQueue.push(queue![0]);
// Don't clear curently playing song
if (this.queue.length > 0) {
newQueue.push(this.queue[0]);
}
this.guildQueues.set(guildId, newQueue);
this.queue = newQueue;
}
size(guildId: string): number {
return this.get(guildId).length;
}
private initQueue(guildId: string): void {
if (!this.guildQueues.get(guildId)) {
this.guildQueues.set(guildId, []);
this.queuePositions.set(guildId, 0);
}
size(): number {
return this.queue.length;
}
}