diff options
| author | Max Isom <[email protected]> | 2020-03-17 17:59:26 -0500 |
|---|---|---|
| committer | Max Isom <[email protected]> | 2020-03-17 17:59:26 -0500 |
| commit | 15d4e251f2af47288b4d5720b8a7b763e72731c0 (patch) | |
| tree | 4a21df4ff46747cad0c491ea67c9127b303c3113 /src/services | |
| parent | 1a1bdfd674970c87f6de941dcd51b0aff16156ce (diff) | |
| download | muse-15d4e251f2af47288b4d5720b8a7b763e72731c0.tar.xz muse-15d4e251f2af47288b4d5720b8a7b763e72731c0.zip | |
Add better responses
Diffstat (limited to 'src/services')
| -rw-r--r-- | src/services/player.ts | 47 |
1 files changed, 26 insertions, 21 deletions
diff --git a/src/services/player.ts b/src/services/player.ts index d578704..74803f8 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -10,12 +10,11 @@ import Queue, {QueuedSong} from './queue'; export enum STATUS { PLAYING, - PAUSED, - DISCONNECTED + PAUSED } export default class { - public status = STATUS.DISCONNECTED; + public status = STATUS.PAUSED; public voiceConnection: VoiceConnection | null = null; private readonly queue: Queue; private readonly cacheDir: string; @@ -35,17 +34,24 @@ export default class { this.voiceConnection = conn; } - disconnect(): void { + disconnect(breakConnection = true): void { if (this.voiceConnection) { if (this.status === STATUS.PLAYING) { this.pause(); } - this.voiceConnection.disconnect(); + if (breakConnection) { + this.voiceConnection.disconnect(); + } + + this.voiceConnection = null; + this.dispatcher = null; } } async seek(positionSeconds: number): Promise<void> { + this.status = STATUS.PAUSED; + if (this.voiceConnection === null) { throw new Error('Not connected to a voice channel.'); } @@ -79,22 +85,26 @@ export default class { throw new Error('Not connected to a voice channel.'); } + const currentSong = this.getCurrentSong(); + + if (!currentSong) { + throw new Error('Queue empty.'); + } + // Resume from paused state - if (this.status === STATUS.PAUSED) { + if (this.status === STATUS.PAUSED && this.getPosition() !== 0) { if (this.dispatcher) { this.dispatcher.resume(); this.status = STATUS.PLAYING; - } else { - await this.seek(this.getPosition()); + return; } - return; - } - - const currentSong = this.getCurrentSong(); + if (!currentSong.isLive) { + await this.seek(this.getPosition()); + return; + } - if (!currentSong) { - throw new Error('Queue empty.'); + // Must be livestream, continue } if (await this.isCached(currentSong.url)) { @@ -153,7 +163,7 @@ export default class { } } - private async waitForCache(url: string, maxRetries = 50, retryDelay = 500): Promise<void> { + private async waitForCache(url: string, maxRetries = 500, retryDelay = 200): Promise<void> { // eslint-disable-next-line no-async-promise-executor return new Promise(async (resolve, reject) => { if (await this.isCached(url)) { @@ -278,12 +288,7 @@ export default class { } this.voiceConnection.on('disconnect', () => { - // Automatically pause - if (this.status === STATUS.PLAYING) { - this.pause(); - } - - this.dispatcher = null; + this.disconnect(false); }); if (!this.dispatcher) { |
