summaryrefslogtreecommitdiff
path: root/src/commands/utility/fortnitestats.ts
blob: 84e011553cc9f9c8504421fc5d41ace19cdea3f4 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { Command, CommandoMessage } from 'discord.js-commando';
import { MessageEmbed } from 'discord.js';
import emoji from 'emoji-random'
import axios from 'axios'
import config from '../../config.json'
const platforms = ['pc', 'xbl', 'psn']

module.exports = class FortniteStatsUtility extends Command {
    constructor(client) {
        super(client, {
            name: 'fortnitestats',
            aliases: [
                'fortnite-stats',
                'fortnitestatistics',
                'fortnite-statistics',
                'fnstats',
                'fn-stats',
                'fnstatistics',
                'fn-statistics',
                'fns',
                'fn-s'
            ],
            group: 'utility',
            memberName: 'fortnitestats',
            description: 'Grabs a specified player\'s Fortnite statistics.',
            details: 'Available platforms are `pc` (PC), `xbp` (Xbox Live) and `psn` (Playstation Network).',
            examples: [
                'uwu!fortnitestats Frozen',
                'uwu!fns Sin'
            ],
            userPermissions: ['SEND_MESSAGES', 'READ_MESSAGE_HISTORY'],
            clientPermissions: ['SEND_MESSAGES', 'READ_MESSAGE_HISTORY'],
            throttling: {
                usages: 5,
                duration: 30
            },
            args: [
                {
                    key: 'pPlatform',
                    prompt: 'What platform would you like to search on.',
                    type: 'string',
                    parse: platform => platform.toLowerCase(),
                    oneOf: platforms
                },
                {
                    key: 'pUsername',
                    prompt: 'What user would you like to look up?',
                    type: 'string'
                }
            ]
        });
    }
    async run(msg: CommandoMessage, { pPlatform, pUsername }) {
        try {
            const stats = (
                await axios
                    .get(`https://api.fortnitetracker.com/v1/profile/${pPlatform}/${pUsername}`, {
                        headers: { 'TRN-Api-Key': config.fortniteTrackerNetworkToken }
                    })
                    .catch(err => {
                        console.error(err)
                        return msg.reply('Woops, There was an error with the (https://api.fortnitetracker.com) API. ' + emoji.random())
                    })
            ).data

            if (stats.error === 'Player Not Found') {
                return msg.reply('Specified player was not found on that platform. ' + emoji.random())
            }

            console.debug(`Result for ${pUsername} on ${pPlatform}:`, stats)
            let emb = new MessageEmbed()
                .setTitle(stats.epicUserHandle)
                .setURL(`https://fortnitetracker.com/profile/${pPlatform}/${encodeURIComponent(pUsername)}`)
                .setColor(0xFFCC4D)
                .setFooter('Information providied by The Tracker Network.',)
            
            if (stats.lifeTimeStats[8] && stats.lifeTimeStats[9]) {
                emb.addField("🏆 Wins", `${stats.lifeTimeStats[8].value} wins (${stats.lifeTimeStats[9].value})`)
            }

            if (stats.lifeTimeStats[10] && stats.lifeTimeStats[11]) {
                emb.addField(
                    "💀 Kills",
                    `${stats.lifeTimeStats[10].value} kills. ${stats.lifeTimeStats[11].value} K/D ratio.`
                );
            }

            if (stats.lifeTimeStats[7]) {
                emb.addField("🎮 Matches Played", stats.lifeTimeStats[7].value.toString());
            }

            if (stats.lifeTimeStats[6]) {
                emb.addField("🔢 Score", stats.lifeTimeStats[6].value.toString());
            }

            return msg.replyEmbed(emb);
        } finally {
            msg.channel.stopTyping()
        }
    }
};