Untitled

 avatar
unknown
javascript
2 years ago
6.9 kB
14
Indexable
async function ClanWars(tag, callback)
{
    request
    (
        {
            method: `GET`,
            url: `https://api.clashofclans.com/v1/clans/%23${tag}/currentwar`,
            headers:
            {
                "Authorization": "Bearer TOKEN"
            }
        }, (err, res, body) =>
        {

            const clanwar = JSON.parse(body);

            const prepStart = new Date(clanwar.preparationStartTime.slice(0, -8));
            const prepStartStr = `${prepStart.getDate()}/${prepStart.getMonth() + 1}/${prepStart.getFullYear()} ${prepStart.getHours()} ${prepStart.getMinutes()} ${prepStart.getSeconds()}`;
            
            const start = new Date(clanwar.startTime.slice(0, -8));
            const startStr = `${start.getDate()}/${start.getMonth() + 1}/${start.getFullYear()} ${start.getHours()} ${start.getMinutes()} ${start.getSeconds()}`;
            
            const end = new Date(clanwar.endTime.slice(0, -8));
            const endStr = `${end.getDate()}/${end.getMonth() + 1}/${end.getFullYear()} ${end.getHours()} ${end.getMinutes()} ${end.getSeconds()}`;
            
            if (err) 
            {
                callback(err);
                return;
            };
            
            if (res.statusCode === 404) 
            {
                const error = new Error(`Make sure you've provided a tag without # : \n :x: \`.clan #XYZ12345\`\n:white_check_mark: \`.clan XYZ12345\`\nIf the command still doesn't work contact staff and/or report using the .report (message) command`);
                callback(error);
                return;
            };
            
            callback(null, { clanwar, prepStartStr, startStr, endStr });
            
        }
    );
};


// Where I use it


    const args = message.content.split(' ');

    if (args[0].toLocaleLowerCase() === `.clanwar`)
    {
        const tag = args[1];

        ClanWars(tag, (err, body) =>
        {
            var WinnerTag;
            var WinnerName;
            var Winnerbadge;

            if (body.clanwar.clan.stars >= body.clanwar.opponent.stars)
            {
                if (body.clanwar.clan.destructionPercentage >= body.clanwar.opponent.destructionPercentage)
                {
                    WinnerTag = body.clanwar.clan.tag;
                    WinnerName = body.clanwar.clan.name;
                    Winnerbadge = body.clanwar.clan.badgeUrls.medium;
                }
            }
            else if (body.clanwar.clan.stars <= body.clanwar.opponent.stars)
            {
                if (body.clanwar.clan.destructionPercentage <= body.clanwar.opponent.destructionPercentage)
                {
                    WinnerTag = body.clanwar.opponent.tag;
                    WinnerName = body.clanwar.opponent.name;
                    Winnerbadge = body.clanwar.opponent.badgeUrls.medium;
                };
            };

            const WarEndedEmbed = new EmbedBuilder()
            .setColor(0xF1D412)
            .setTitle(`Winner: ${WinnerName}`)
            .setURL(`https://link.clashofclans.com/en?action=OpenClanProfile&tag=${WinnerTag}`)
            .setDescription(`**${body.clanwar.clan.name}** Has ended a war vs **${body.clanwar.opponent.name}** clan`)
            .addFields(
                {name: `Team Size `, value: `${body.clanwar.teamSize}`, inline: true},
                {name: `Attacks Per Memeber `, value: `${body.clanwar.attacksPerMember}`, inline: true},
                {name: `Name + level`, value: `${body.clanwar.clan.name} lvl ${body.clanwar.clan.clanLevel} vs ${body.clanwar.opponent.name} lvl ${body.clanwar.opponent.clanLevel}`, inline: true},

                {name: `Preporation Start date`, value: `${body.prepStartStr}`, inline: true},
                {name: `War Start date`, value: `${body.startStr}`, inline: true},
                {name: `War End date`, value: `${body.endStr}`, inline: true},
            )
            .setThumbnail(Winnerbadge)

            const WarEmbed = new EmbedBuilder()
            .setColor(0xF1D412)
            .setTitle(body.clanwar.clan.name)
            .setURL(`https://link.clashofclans.com/en?action=OpenClanProfile&tag=${tag}`)
            .setDescription(`**${body.clanwar.clan.name}** Is currently in war Against **${body.clanwar.opponent.name}** clan`)
            .addFields(
                {name: `Team Size `, value: `${body.clanwar.teamSize}`, inline: true},
                {name: `Attacks Per Memeber `, value: `${body.clanwar.attacksPerMember}`, inline: true},
                {name: `Name + level`, value: `${body.clanwar.clan.name} LVL ${body.clanwar.clan.clanLevel} vs ${body.clanwar.opponent.name} LVL ${body.clanwar.opponent.clanLevel}`, inline: true},

                {name: `Preporation Start date`, value: `${body.prepStartStr}`, inline: true},
                {name: `War Start date`, value: `${body.startStr}`, inline: true},
                {name: `War End date`, value: `${body.endStr}`, inline: true},
            )
            .setThumbnail(body.clanwar.clan.badgeUrls.medium)
            .setThumbnail(body.clanwar.opponent.badgeUrls.medium);

            const NotInWarEmbed = new EmbedBuilder()
            .setColor(0xF1D412)
            .setTitle(`Not In War`)
            .setDescription(`The Clan is currently not in war or still searching for a match!`);

            const ErrorEmbed = new EmbedBuilder()
            .setColor(0xFF0000)
            .setTitle(`An Error Occured`)
            .setDescription(`An Error Occured!\nMake sure you've provided a tag without # :\n:x: **.clan #XYZ12345**\n:white_check_mark: **.clan XYZ12345** \nIf the command still doesn't work contact staff and/or report using the .report (message) command`);

            if (err) 
            {
                message.channel.send({ embeds: [ErrorEmbed] });
                return;
            }

            else if (body.clanwar.state == 'inWar')
            {
                message.channel.send({ embeds: [WarEmbed] }); 
                console.log(`Prep: ${body.prepStartStr}`);
                console.log(`Start: ${body.startStr}`);
                console.log(`End: ${body.endStr}`);
                return;
            }

            else if (body.clanwar.state == 'warEnded')
            {
                message.channel.send({ embeds: [WarEndedEmbed] }); 
                console.log(`Prep: ${body.prepStartStr}`);
                console.log(`Start: ${body.startStr}`);
                console.log(`End: ${body.endStr}`);
                return;
            }

            else (body.clanwar.state == 'notInWar')
            {
                message.channel.send({ embeds: [NotInWarEmbed] }); return;
            };
        });
    }
Editor is loading...