From 4f0db62170398c542d23fcee3812f0887901f977 Mon Sep 17 00:00:00 2001 From: Thongrapee Panyapatiphan Date: Fri, 25 Feb 2022 06:24:45 +0700 Subject: [PATCH] Add stop command (#540) Co-authored-by: Max Isom --- CHANGELOG.md | 2 ++ src/commands/stop.ts | 37 +++++++++++++++++++++++++++++++++++++ src/inversify.config.ts | 2 ++ src/services/player.ts | 6 ++++++ 4 files changed, 47 insertions(+) create mode 100644 src/commands/stop.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index ad3d623..51319f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- `/stop` command to disconnect and clear the queue ## [1.1.2] - 2022-02-21 ### Changed diff --git a/src/commands/stop.ts b/src/commands/stop.ts new file mode 100644 index 0000000..3c32c7f --- /dev/null +++ b/src/commands/stop.ts @@ -0,0 +1,37 @@ +import {CommandInteraction} from 'discord.js'; +import {SlashCommandBuilder} from '@discordjs/builders'; +import {TYPES} from '../types.js'; +import {inject, injectable} from 'inversify'; +import PlayerManager from '../managers/player.js'; +import {STATUS} from '../services/player.js'; +import Command from '.'; + +@injectable() +export default class implements Command { + public readonly slashCommand = new SlashCommandBuilder() + .setName('stop') + .setDescription('stop playback, disconnect, and clear all songs in the queue'); + + public requiresVC = true; + + private readonly playerManager: PlayerManager; + + constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) { + this.playerManager = playerManager; + } + + public async execute(interaction: CommandInteraction) { + const player = this.playerManager.get(interaction.guild!.id); + + if (!player.voiceConnection) { + throw new Error('not connected'); + } + + if (player.status !== STATUS.PLAYING) { + throw new Error('not currently playing'); + } + + player.stop(); + await interaction.reply('u betcha'); + } +} diff --git a/src/inversify.config.ts b/src/inversify.config.ts index d29d3de..7116984 100644 --- a/src/inversify.config.ts +++ b/src/inversify.config.ts @@ -26,6 +26,7 @@ import Remove from './commands/remove.js'; import Seek from './commands/seek.js'; import Shuffle from './commands/shuffle.js'; import Skip from './commands/skip.js'; +import Stop from './commands/stop.js'; import Unskip from './commands/unskip.js'; import ThirdParty from './services/third-party.js'; import FileCacheProvider from './services/file-cache.js'; @@ -66,6 +67,7 @@ container.bind(TYPES.Services.AddQueryToQueue).to(AddQueryToQue Seek, Shuffle, Skip, + Stop, Unskip, ].forEach(command => { container.bind(TYPES.Command).to(command).inSingletonScope(); diff --git a/src/services/player.ts b/src/services/player.ts index 79ef2bb..c315089 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -342,6 +342,12 @@ export default class { return this.queueSize() === 0; } + stop(): void { + this.disconnect(); + this.queuePosition = 0; + this.queue = []; + } + private getHashForCache(url: string): string { return hasha(url); }