Add shuffle command

This commit is contained in:
Max Isom 2020-03-14 12:41:00 -05:00
parent fb91c8e89c
commit ad4d49f763
6 changed files with 52 additions and 68 deletions

29
src/commands/shuffle.ts Normal file
View file

@ -0,0 +1,29 @@
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 = 'shuffle';
public description = 'shuffle current queue';
private readonly queue: Queue;
constructor(@inject(TYPES.Services.Queue) queue: Queue) {
this.queue = queue;
}
public async execute(msg: Message, _: string []): Promise<void> {
const queue = this.queue.get(msg.guild!.id);
if (queue.length <= 2) {
await msg.channel.send('error: not enough songs to shuffle');
return;
}
this.queue.shuffle(msg.guild!.id);
await msg.channel.send('`' + JSON.stringify(this.queue.get(msg.guild!.id).slice(0, 10)) + '`');
}
}

View file

@ -25,6 +25,7 @@ import Config from './commands/config';
import Play from './commands/play';
import QueueCommad from './commands/queue';
import Seek from './commands/seek';
import Shuffle from './commands/shuffle';
let container = new Container();
@ -41,6 +42,7 @@ container.bind<Command>(TYPES.Command).to(Config).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(Shuffle).inSingletonScope();
// Config values
container.bind<string>(TYPES.Config.DISCORD_TOKEN).toConstantValue(DISCORD_TOKEN);

1
src/packages.d.ts vendored
View file

@ -1,2 +1,3 @@
declare module 'node-emoji';
declare module 'ytsr';
declare module 'array-shuffle';

View file

@ -1,4 +1,5 @@
import {injectable} from 'inversify';
import shuffle from 'array-shuffle';
export interface QueuedPlaylist {
title: string;
@ -84,6 +85,16 @@ export default class {
}
}
shuffle(guildId: string): void {
const queue = this.guildQueues.get(guildId);
if (!queue) {
throw new Error('Queue doesn\'t exist yet.');
}
this.guildQueues.set(guildId, [queue[0], ...shuffle(queue.slice(1))]);
}
size(guildId: string): number {
return this.get(guildId).length;
}