Untitled

mail@pastecode.io avatar
unknown
plain_text
22 days ago
3.7 kB
4
Indexable
Never
const { EmbedBuilder } = require('discord.js');
const Discord = require('discord.js');
const client = new Discord.Client({ intents: ['Guilds', 'GuildMessages'] });
const axios = require('axios');

async function getGuildMembers() {
    try {
        const response = await axios.get('https://stats.pika-network.net/api/clans/THEBOIS');
        const guildData = response.data;

        if (!guildData || !guildData.members || !Array.isArray(guildData.members)) {
            console.error('Invalid guild data format');
            return [];
        }

        const memberUsernames = guildData.members.map(member => member.user.username);
        return memberUsernames;
    } catch (error) {
        console.error('Error fetching guild members:', error.message);
        return [];
    }
}

async function getPlayerStats(playerIGN) {
    try {
        const response = await axios.get(`https://stats.pika-network.net/api/profile/${playerIGN}/leaderboard?type=bedwars&interval=weekly&mode=all_modes`);
        return response.data;
    } catch (error) {
        console.error(`Error fetching stats for ${playerIGN}:`, error.message);
        return null;
    }
}

async function main() {
    const memberUsernames = await getGuildMembers();
    if (memberUsernames.length === 0) {
        console.error('No guild members found.');
        return { memberStats: [], failedMembers: [] };
    }

    const memberStats = [];
    const failedMembers = [];

    for (const username of memberUsernames) {
        const stats = await getPlayerStats(username);
        if (stats) {
            const memberStat = {
                username,
                wins: 0
            };
            if (stats['Wins'] && stats['Wins'].entries && stats['Wins'].entries.length > 0) {
                const entry = stats['Wins'].entries.find(entry => entry.id === username);
                if (entry) {
                    memberStat['wins'] = entry.value || 0;
                }
            }
            memberStats.push(memberStat);
        } else {
            console.log(`Unable to retrieve stats for ${username}. Their API may be turned off.`);
            failedMembers.push(username);
        }
    }

    //sort memberStats by wins in descending order
    memberStats.sort((a, b) => b.wins - a.wins);

    return { memberStats, failedMembers };
}

const token = process.env.TOKEN;

client.once('ready', async () => {
    console.log('Bot is ready! Sending guild stats...');
    try {
        const { memberStats, failedMembers } = await main();
        
        const embed = new EmbedBuilder()
            .setColor(0x0099FF)
            .setTitle('Guild Bedwars Stats')
            .setDescription('Weekly Bedwars stats for guild members.');

        memberStats.forEach((memberStat, index) => {
            embed.addField(`#${index + 1}. ${memberStat.username}`, `${memberStat.wins} wins`);
        });

        const channel = client.channels.cache.get(process.env.CHANNEL_ID);
        if (channel) {
            channel.send({ embeds: [embed] });

            for (const failedMember of failedMembers) {
                const failedMessage = `Unable to retrieve stats for \`${failedMember}\`. They might have their API turned off.`;
                channel.send(failedMessage);
            }
            console.log('Failed members messages sent successfully.');
        } else {
            console.error('Channel not found.');
        }
    } catch (error) {
        console.error('Error retrieving guild stats:', error.message);
    }
});

client.login(token);
Leave a Comment