Add disconnect command

This commit is contained in:
Max Isom 2020-03-18 18:29:32 -05:00
parent 6a02088b04
commit 8340f9b95a
4 changed files with 55 additions and 18 deletions

View file

@ -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<void> {
const player = this.playerManager.get(msg.guild!.id);
player.disconnect();
await msg.channel.send('u betcha');
}
}

View file

@ -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;
}

View file

@ -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<GetSongs>(TYPES.Services.GetSongs).to(GetSongs).inSingletonScope(
container.bind<NaturalLanguage>(TYPES.Services.NaturalLanguage).to(NaturalLanguage).inSingletonScope();
// Commands
container.bind<Command>(TYPES.Command).to(Clear).inSingletonScope();
container.bind<Command>(TYPES.Command).to(Config).inSingletonScope();
container.bind<Command>(TYPES.Command).to(ForwardSeek).inSingletonScope();
container.bind<Command>(TYPES.Command).to(Help).inSingletonScope();
container.bind<Command>(TYPES.Command).to(Pause).inSingletonScope();
container.bind<Command>(TYPES.Command).to(Play).inSingletonScope();
container.bind<Command>(TYPES.Command).to(QueueCommad).inSingletonScope();
container.bind<Command>(TYPES.Command).to(Seek).inSingletonScope();
container.bind<Command>(TYPES.Command).to(Shortcuts).inSingletonScope();
container.bind<Command>(TYPES.Command).to(Shuffle).inSingletonScope();
container.bind<Command>(TYPES.Command).to(Skip).inSingletonScope();
container.bind<Command>(TYPES.Command).to(Unskip).inSingletonScope();
[
Clear,
Config,
Disconnect,
ForwardSeek,
Help,
Pause,
Play,
QueueCommad,
Seek,
Shortcuts,
Shuffle,
Skip,
Unskip
].forEach(command => {
container.bind<Command>(TYPES.Command).to(command).inSingletonScope();
});
// Config values
container.bind<string>(TYPES.Config.DISCORD_TOKEN).toConstantValue(DISCORD_TOKEN);

View file

@ -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);