
const bot = require('./../bot');
const request = require('request-promise-native');
const e = require('./../../data/emoji.json');
/**
 * This method should return the response directly to the channel
 * @param {*string array} params 
 * @param {*message} message
 */
async function command(params, message) {
    if (params.length > 0) {
        let em = await request({
            uri: 'https://api.github.com/emojis',
            headers: {
                'User-Agent': 'Revive-Bot'
            }
        });
        em = JSON.parse(em);
        const files = [];
        for (let i = 0; i < params.length; i++) {
            if (/\<\:.+\:\d+\>/.test(params[i])) {
                files.push({ attachment: `https://cdn.discordapp.com/emojis/${params[i].match(/(\d+)(?!.*\d)/)[1]}.png`, name: "emoji.png" });

            } else {
                let url = em[params[i]];
                console.log(url);
                if (!url) {
                    url = e[params[i]];
                    if (url)
                        url = em[url];
                    console.log(url);
                }
                if (!url)
                {
                    let ecache = await request("https://blob-cache.herokuapp.com/");
                    ecache =  JSON.parse(ecache);
                    if(ecache[params[i]])
                       url = "https://cdn.discordapp.com/emojis/"+ecache[params[i]];
                }
                if (url)
                    files.push({ attachment: url, name: params[i] + ".png" });
            }
        }
        if (files.length === 0)
            return await message.channel.send("Invalid Emoji");
        else
            return await message.channel.send((files.length > 1) ? ('Emojis') : (""), { files: files });
    } else
        return await message.channel.send('Not enough arguments!');

}
/**
 * description of the command
 */
const description = "Gets url of the emoji";
/**
 * Define Exports
 */
module.exports = {
    execute: command,
    description: description
};

@@ -1391,32 +1391,39 @@
 cache%5Bparams%5Bi%5D%5D
++%22.png%22
 ;%0A              

Update emoji.js