From d70bd167973b5171dbe82bac2daba04e73602bfa Mon Sep 17 00:00:00 2001 From: Max Isom Date: Sat, 14 Mar 2020 22:03:31 -0500 Subject: [PATCH] Add queue clear command --- src/commands/clear.ts | 22 ++++++++++++++++++++++ src/inversify.config.ts | 2 ++ src/services/queue.ts | 25 +++++++++++++++++++++---- 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/commands/clear.ts diff --git a/src/commands/clear.ts b/src/commands/clear.ts new file mode 100644 index 0000000..98f7b7f --- /dev/null +++ b/src/commands/clear.ts @@ -0,0 +1,22 @@ +import {Message} from 'discord.js'; +import {TYPES} from '../types'; +import {inject, injectable} from 'inversify'; +import Queue from '../services/queue'; +import Command from '.'; + +@injectable() +export default class implements Command { + public name = 'clear'; + public description = 'clears all songs in queue (except currently playing)'; + private readonly queue: Queue; + + constructor(@inject(TYPES.Services.Queue) queue: Queue) { + this.queue = queue; + } + + public async execute(msg: Message, _: string []): Promise { + this.queue.clear(msg.guild!.id); + + await msg.channel.send('cleared'); + } +} diff --git a/src/inversify.config.ts b/src/inversify.config.ts index 275077c..e538183 100644 --- a/src/inversify.config.ts +++ b/src/inversify.config.ts @@ -21,6 +21,7 @@ import Player from './services/player'; // Comands import Command from './commands'; +import Clear from './commands/clear'; import Config from './commands/config'; import Play from './commands/play'; import QueueCommad from './commands/queue'; @@ -38,6 +39,7 @@ container.bind(TYPES.Services.Player).to(Player).inSingletonScope(); container.bind(TYPES.Services.Queue).to(Queue).inSingletonScope(); // Commands +container.bind(TYPES.Command).to(Clear).inSingletonScope(); container.bind(TYPES.Command).to(Config).inSingletonScope(); container.bind(TYPES.Command).to(Play).inSingletonScope(); container.bind(TYPES.Command).to(QueueCommad).inSingletonScope(); diff --git a/src/services/queue.ts b/src/services/queue.ts index be59c7f..0b98b8e 100644 --- a/src/services/queue.ts +++ b/src/services/queue.ts @@ -56,10 +56,7 @@ export default class { } add(guildId: string, song: QueuedSong): void { - if (!this.guildQueues.get(guildId)) { - this.guildQueues.set(guildId, []); - this.queuePositions.set(guildId, 0); - } + this.initQueue(guildId); if (song.playlist) { // Add to end of queue @@ -95,7 +92,27 @@ export default class { this.guildQueues.set(guildId, [queue[0], ...shuffle(queue.slice(1))]); } + clear(guildId: string): void { + this.initQueue(guildId); + const queue = this.guildQueues.get(guildId); + + const newQueue = []; + + if (queue!.length > 0) { + newQueue.push(queue![0]); + } + + this.guildQueues.set(guildId, newQueue); + } + size(guildId: string): number { return this.get(guildId).length; } + + private initQueue(guildId: string): void { + if (!this.guildQueues.get(guildId)) { + this.guildQueues.set(guildId, []); + this.queuePositions.set(guildId, 0); + } + } }