summaryrefslogtreecommitdiff
path: root/commands/voice/shuffle.js
blob: 3771f16a812d2cac32864d16083dc10797b4dd94 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const { Command } = require('discord.js-commando');
const { MessageEmbed } = require('discord.js');
const emoji = require('emoji-random');

module.exports = class ShuffleVoice extends Command {
    constructor(client) {
        super(client, {
            name: 'shuffle',
            group: 'voice',
            memberName: 'shuffle',
            description: 'shuffle the queue',
            guildOnly: true,
            examples: ['s5n!shuffle']
        });
    }
    run(msg) {
        var voiceChannel = msg.member.voice.channel;
        if (!voiceChannel) return msg.reply('join channel and try again k ' + emoji.random());

        if (
            typeof msg.guild.musicData.songDispatcher == 'undefined' ||
            msg.guild.musicData.songDispatcher == null
        ) {
            return msg.reply('there is no song playing right now ' + emoji.random());
        }

        if (msg.guild.musicData.queue.length < 1)
            return msg.say('There are no songs in queue ' + emoji.random());

        shuffleQueue(msg.guild.musicData.queue);

        const titleArray = [];
        msg.guild.musicData.queue.map(obj => {
            titleArray.push(obj.title);
        });
        var queueEmbed = new MessageEmbed()
            .setColor(0xF97DAE)
            .setTitle('new music q ' + emoji.random());
        for (let i = 0; i < titleArray.length; i++) {
            queueEmbed.addField(`${i + 1}:`, `${titleArray[i]}`);
        }
        return msg.say(queueEmbed);
    }
};

function shuffleQueue(queue) {
    for (let i = queue.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [queue[i], queue[j]] = [queue[j], queue[i]];
    }
}