Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
2.6 kB
5
Indexable
Never
const request = require('request');

async function ClanWars(tag, callback)
{
    request
    (
        {
            method: `GET`,
            url: `https://api.clashofclans.com/v1/clans/%23${tag}/currentwar`,
            headers:
            {
                "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiIsImtpZCI6IjI4YTMxOGY3LTAwMDAtYTFlYi03ZmExLTJjNzQzM2M2Y2NhNSJ9.eyJpc3MiOiJzdXBlcmNlbGwiLCJhdWQiOiJzdXBlcmNlbGw6Z2FtZWFwaSIsImp0aSI6IjI4OWRmOTA5LTA5MTMtNGZjMi1iN2Q1LTAxYmJjZWYxMjYxNyIsImlhdCI6MTY4MDU2NDE4MSwic3ViIjoiZGV2ZWxvcGVyL2ZmYTExM2ZhLWVkNzItMjkxNS05NjJkLTNlZjlhMjQ0NDZkYyIsInNjb3BlcyI6WyJjbGFzaCJdLCJsaW1pdHMiOlt7InRpZXIiOiJkZXZlbG9wZXIvc2lsdmVyIiwidHlwZSI6InRocm90dGxpbmcifSx7ImNpZHJzIjpbIjkyLjIxLjIxNS4xNTQiXSwidHlwZSI6ImNsaWVudCJ9XX0.wS2dJodP6nq9UXTumkNE74Q0R1lC4D6Cq5T9Stbyo53hYrUSpYh40lVTpCWk5V3qbp5hwd3scpb9uW2xYNzxdw"
            }
        }, (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 });
            
        }
    );
};


ClanWars('2PPJ98CRY', (err, body) =>
{
    if (err) 
    {
        console.log(err);
        return;
    }

    console.log(`Prep: ${body.prepStartStr}`);
    console.log(`Start: ${body.startStr}`);
    console.log(`End: ${body.endStr}`);
});