const https = require('https');

let insultURL = "https://evilinsult.com/generate_insult.php?lang=en&type=json";

function makeRequest(url, attribute) {
    return new Promise((resolve, reject) => {
        https.get(url, (res) => {
            let body = "";

            res.on("data", (chunk) => {
                body += chunk;
            });

            res.on("end", () => {
                try {
                    let result = JSON.parse(body);
                    resolve(result[attribute]);
                } catch (error) {
                    reject(error.message);
                };
            });

        }).on("error", (error) => {
            console.error(error.message);
        });
    });
}

module.exports = {
    insult: async function (bot, entity) {
        if (entity.type != 'player')
            return
        var insult = await makeRequest(insultURL, "insult");
        bot.chat(entity.username + " " + insult);
    }
}