From 8340f9b95a377580871d6e73bf87eb29ed69e6c7 Mon Sep 17 00:00:00 2001 From: Max Isom Date: Wed, 18 Mar 2020 18:29:32 -0500 Subject: [PATCH] Add disconnect command --- src/commands/disconnect.ts | 27 +++++++++++++++++++++++++++ src/commands/play.ts | 2 +- src/inversify.config.ts | 30 ++++++++++++++++++------------ src/services/player.ts | 14 +++++++++----- 4 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 src/commands/disconnect.ts diff --git a/src/commands/disconnect.ts b/src/commands/disconnect.ts new file mode 100644 index 0000000..ecf41ab --- /dev/null +++ b/src/commands/disconnect.ts @@ -0,0 +1,27 @@ +import {Message} from 'discord.js'; +import {TYPES} from '../types'; +import {inject, injectable} from 'inversify'; +import PlayerManager from '../managers/player'; +import Command from '.'; + +@injectable() +export default class implements Command { + public name = 'disconnect'; + public examples = [ + ['disconnect', 'pauses and disconnects player'] + ]; + + private readonly playerManager: PlayerManager; + + constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) { + this.playerManager = playerManager; + } + + public async execute(msg: Message, _: string []): Promise { + const player = this.playerManager.get(msg.guild!.id); + + player.disconnect(); + + await msg.channel.send('u betcha'); + } +} diff --git a/src/commands/play.ts b/src/commands/play.ts index b155eae..bee2b4c 100644 --- a/src/commands/play.ts +++ b/src/commands/play.ts @@ -57,7 +57,7 @@ export default class implements Command { } // Must be resuming play - if (queue.get().length === 0) { + if (queue.get().length === 0 && !queue.getCurrent()) { await res.stop(errorMsg('nothing to play')); return; } diff --git a/src/inversify.config.ts b/src/inversify.config.ts index 590d8f5..2e53f14 100644 --- a/src/inversify.config.ts +++ b/src/inversify.config.ts @@ -27,6 +27,7 @@ import NaturalLanguage from './services/natural-language-commands'; import Command from './commands'; import Clear from './commands/clear'; import Config from './commands/config'; +import Disconnect from './commands/disconnect'; import ForwardSeek from './commands/fseek'; import Help from './commands/help'; import Pause from './commands/pause'; @@ -53,18 +54,23 @@ container.bind(TYPES.Services.GetSongs).to(GetSongs).inSingletonScope( container.bind(TYPES.Services.NaturalLanguage).to(NaturalLanguage).inSingletonScope(); // Commands -container.bind(TYPES.Command).to(Clear).inSingletonScope(); -container.bind(TYPES.Command).to(Config).inSingletonScope(); -container.bind(TYPES.Command).to(ForwardSeek).inSingletonScope(); -container.bind(TYPES.Command).to(Help).inSingletonScope(); -container.bind(TYPES.Command).to(Pause).inSingletonScope(); -container.bind(TYPES.Command).to(Play).inSingletonScope(); -container.bind(TYPES.Command).to(QueueCommad).inSingletonScope(); -container.bind(TYPES.Command).to(Seek).inSingletonScope(); -container.bind(TYPES.Command).to(Shortcuts).inSingletonScope(); -container.bind(TYPES.Command).to(Shuffle).inSingletonScope(); -container.bind(TYPES.Command).to(Skip).inSingletonScope(); -container.bind(TYPES.Command).to(Unskip).inSingletonScope(); +[ + Clear, + Config, + Disconnect, + ForwardSeek, + Help, + Pause, + Play, + QueueCommad, + Seek, + Shortcuts, + Shuffle, + Skip, + Unskip +].forEach(command => { + container.bind(TYPES.Command).to(command).inSingletonScope(); +}); // Config values container.bind(TYPES.Config.DISCORD_TOKEN).toConstantValue(DISCORD_TOKEN); diff --git a/src/services/player.ts b/src/services/player.ts index 38a92aa..e4d54a6 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -45,7 +45,6 @@ export default class { this.voiceConnection.disconnect(); } - this.positionInSeconds = 0; this.voiceConnection = null; this.dispatcher = null; } @@ -97,10 +96,15 @@ export default class { } // Resume from paused state - if (this.status === STATUS.PAUSED && this.getPosition() !== 0 && this.dispatcher && currentSong.url === this.nowPlaying?.url) { - this.dispatcher.resume(); - this.status = STATUS.PLAYING; - return; + if (this.status === STATUS.PAUSED && this.getPosition() !== 0 && currentSong.url === this.nowPlaying?.url) { + if (this.dispatcher) { + this.dispatcher.resume(); + this.status = STATUS.PLAYING; + return; + } + + // Was disconnected, need to recreate stream + return this.seek(this.getPosition()); } const stream = await this.getStream(currentSong.url);