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 // Must be resuming play
if (queue.get().length === 0) { if (queue.get().length === 0 && !queue.getCurrent()) {
await res.stop(errorMsg('nothing to play')); await res.stop(errorMsg('nothing to play'));
return; return;
} }

View file

@ -27,6 +27,7 @@ import NaturalLanguage from './services/natural-language-commands';
import Command from './commands'; import Command from './commands';
import Clear from './commands/clear'; import Clear from './commands/clear';
import Config from './commands/config'; import Config from './commands/config';
import Disconnect from './commands/disconnect';
import ForwardSeek from './commands/fseek'; import ForwardSeek from './commands/fseek';
import Help from './commands/help'; import Help from './commands/help';
import Pause from './commands/pause'; 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(); container.bind<NaturalLanguage>(TYPES.Services.NaturalLanguage).to(NaturalLanguage).inSingletonScope();
// Commands // Commands
container.bind<Command>(TYPES.Command).to(Clear).inSingletonScope(); [
container.bind<Command>(TYPES.Command).to(Config).inSingletonScope(); Clear,
container.bind<Command>(TYPES.Command).to(ForwardSeek).inSingletonScope(); Config,
container.bind<Command>(TYPES.Command).to(Help).inSingletonScope(); Disconnect,
container.bind<Command>(TYPES.Command).to(Pause).inSingletonScope(); ForwardSeek,
container.bind<Command>(TYPES.Command).to(Play).inSingletonScope(); Help,
container.bind<Command>(TYPES.Command).to(QueueCommad).inSingletonScope(); Pause,
container.bind<Command>(TYPES.Command).to(Seek).inSingletonScope(); Play,
container.bind<Command>(TYPES.Command).to(Shortcuts).inSingletonScope(); QueueCommad,
container.bind<Command>(TYPES.Command).to(Shuffle).inSingletonScope(); Seek,
container.bind<Command>(TYPES.Command).to(Skip).inSingletonScope(); Shortcuts,
container.bind<Command>(TYPES.Command).to(Unskip).inSingletonScope(); Shuffle,
Skip,
Unskip
].forEach(command => {
container.bind<Command>(TYPES.Command).to(command).inSingletonScope();
});
// Config values // Config values
container.bind<string>(TYPES.Config.DISCORD_TOKEN).toConstantValue(DISCORD_TOKEN); container.bind<string>(TYPES.Config.DISCORD_TOKEN).toConstantValue(DISCORD_TOKEN);

View file

@ -45,7 +45,6 @@ export default class {
this.voiceConnection.disconnect(); this.voiceConnection.disconnect();
} }
this.positionInSeconds = 0;
this.voiceConnection = null; this.voiceConnection = null;
this.dispatcher = null; this.dispatcher = null;
} }
@ -97,12 +96,17 @@ export default class {
} }
// Resume from paused state // Resume from paused state
if (this.status === STATUS.PAUSED && this.getPosition() !== 0 && this.dispatcher && currentSong.url === this.nowPlaying?.url) { if (this.status === STATUS.PAUSED && this.getPosition() !== 0 && currentSong.url === this.nowPlaying?.url) {
if (this.dispatcher) {
this.dispatcher.resume(); this.dispatcher.resume();
this.status = STATUS.PLAYING; this.status = STATUS.PLAYING;
return; return;
} }
// Was disconnected, need to recreate stream
return this.seek(this.getPosition());
}
const stream = await this.getStream(currentSong.url); const stream = await this.getStream(currentSong.url);
this.dispatcher = this.voiceConnection.play(stream, {type: 'webm/opus'}); this.dispatcher = this.voiceConnection.play(stream, {type: 'webm/opus'});