Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
12 kB
0
Indexable
Never
const profileEmbedCollector = profileMessage.createMessageComponentCollector({ filter: ({ customId }) => customId === 'profile', time: 300000, componentType: ComponentType.SelectMenu });

            profileEmbedCollector.on('collect', async (selectMenu) => {
                if (selectMenu.user.id !== interaction.user.id) return await selectMenu.reply({ content: '*You did not initiate this command!*', ephemeral: true });

                const selectMenuOptions = [];

                player.logs.matches.reverse();

                let total_pages = player.logs.matches.length,
                    current_page = 0,
                    matchLogCollector = null;

                for (const { data: { label, value, description } } of profileEmbedComponents.components[0].options) {
                    const selectMenuOption = {
                        label,
                        value,
                        description,
                        default: value === selectMenu.values[0] ? true : false
                    };

                    selectMenuOptions.push(selectMenuOption);
                };

                collectorProfileComponents = SelectMenuBuilder.from(profileEmbedComponents.components[0]).setOptions(selectMenuOptions);

                switch (selectMenu.values[0]) {
                    case 'match':
                        if (matchLogCollector) {
                            current_page = 0;
                            matchLogCollector.stop();
                            matchLogCollector = null;
                        };

                        await selectMenu.update({
                            embeds: [
                                {
                                    author: {
                                        name: member.user.tag,
                                        iconURL: member.user.displayAvatarURL()
                                    },
                                    color: Colors.Aqua,
                                    title: 'Match Statistics',
                                    description: `*Victory, Defeat & Rank Statistics of ${member}*`,
                                    thumbnail: {
                                        url: member.user.displayAvatarURL()
                                    },
                                    fields: [
                                        { name: 'IGN', value: player.name, inline: true },
                                        { name: 'Victories', value: player.statistics.wins, inline: true },
                                        { name: 'Defeats', value: player.statistics.loses, inline: true },
                                        { name: 'W/L Ratio', value: win_lose_ratio ? win_lose_ratio.toFixed(2) : '0', inline: true },
                                        { name: 'Matches', value: player.statistics.matches, inline: true },
                                        { name: 'Position Wise Rank', value: `#${playerPosition + 1}`, inline: true },
                                    ],
                                    footer: {
                                        text: interaction.guild.name,
                                        iconURL: interaction.guild.iconURL()
                                    },
                                    timestamp: new Date().toISOString()
                                }
                            ],
                            components: [
                                {
                                    type: ComponentType.ActionRow,
                                    components: [collectorProfileComponents]
                                }
                            ]
                        });
                        break;
                    case 'kd':
                        if (matchLogCollector) {
                            current_page = 0;
                            matchLogCollector.stop();
                            matchLogCollector = null;
                        };

                        const playerKDRPosition = (await PlayerStats.find({}).sort({ 'statistics.total_kills': -1 })).findIndex(({ id }) => id === player.id);
                        const kill_death_ratio = player.statistics.deaths / player.statistics.kills;

                        await selectMenu.update({
                            embeds: [
                                {
                                    author: {
                                        name: member.user.tag,
                                        iconURL: member.user.displayAvatarURL()
                                    },
                                    color: Colors.Aqua,
                                    title: 'Kills & Deaths Statistics',
                                    description: `*Kills, Deaths & KDR Statistics of ${member}*`,
                                    thumbnail: {
                                        url: member.user.displayAvatarURL()
                                    },
                                    fields: [
                                        { name: 'Kills', value: player.statistics.kills, inline: true },
                                        { name: 'Deaths', value: player.statistics.deaths, inline: true },
                                        { name: 'K/D Ratio', value: kill_death_ratio ? kill_death_ratio.toFixed(2) : '0', inline: true },
                                        { name: 'Highest Kills', value: player.logs.matches.length ? `${player.logs.matches.reduce((previousMatch, currentMatch) => previousMatch.kills > currentMatch.kills ? previousMatch : currentMatch).kills}` : '0', inline: true },
                                        { name: 'KDR Wise Rank', value: `#${playerKDRPosition + 1}`, inline: true },
                                        { name: 'Tier', value: `${interaction.guild.roles.cache.find(({ name }) => name.toLowerCase() === player.rank.toLowerCase())}`, inline: true }
                                    ],
                                    footer: {
                                        text: interaction.guild.name,
                                        iconURL: interaction.guild.iconURL()
                                    },
                                    timestamp: new Date().toISOString()
                                }
                            ],
                            components: [
                                {
                                    type: ComponentType.ActionRow,
                                    components: [collectorProfileComponents]
                                }
                            ]
                        });
                        break;
                    case 'match_logs':
                        const matchLogEmbedFields = [];

                        for (let page = 0; page < player.logs.matches.length; page += 1) {
                            let index = page;

                            const current_page_logs = player.logs.matches.slice(page, page + 1).map(({ description }) => `${++index}. ${description}`);

                            const logEmbed = {
                                author: {
                                    name: member.user.tag,
                                    iconURL: member.user.displayAvatarURL()
                                },
                                color: Colors.Aqua,
                                title: 'Match Logs & Data',
                                description: `*Match IDs, Logs & Data of ${member}*${`\n\n\`\`\`${current_page_logs.join('\n')}\`\`\``}`,
                                thumbnail: {
                                    url: member.user.displayAvatarURL()
                                },
                                footer: {
                                    text: `Page - ${(page + 1) / 1}/${total_pages}`,
                                    iconURL: interaction.guild.iconURL()
                                },
                                timestamp: new Date().toISOString()
                            };

                            matchLogEmbedFields.push(logEmbed);
                        };

                        const matchLogComponent = new ActionRowBuilder()
                            .addComponents([
                                new ButtonBuilder()
                                    .setCustomId('previous')
                                    .setLabel('Previous')
                                    .setEmoji('⏪')
                                    .setStyle(ButtonStyle.Primary),
                                new ButtonBuilder()
                                    .setCustomId('next')
                                    .setLabel('Next')
                                    .setEmoji('⏩')
                                    .setStyle(ButtonStyle.Primary)
                            ]);

                        const matchLogEmbed = await selectMenu.update({
                            embeds: [
                                matchLogEmbedFields[0]
                            ],
                            components: [
                                {
                                    type: ComponentType.ActionRow,
                                    components: [collectorProfileComponents]
                                },
                                matchLogComponent
                            ]
                        });

                        matchLogCollector = matchLogEmbed.createMessageComponentCollector({ filter: ({ customId }) => ['previous', 'next'].includes(customId), time: 300000, componentType: ComponentType.Button });

                        matchLogCollector.on('collect', async (button) => {
                            if (button.user.id !== interaction.user.id) return await button.reply({ content: '*You did not initiate this command!*', ephemeral: true });

                            switch (button.customId) {
                                case 'previous':
                                    if (current_page === 0) {
                                        current_page = total_pages - 1;
                                    } else {
                                        --current_page;
                                    };
                                    break;
                                case 'next':
                                    if (current_page === total_pages - 1) {
                                        current_page = 0;
                                    } else {
                                        ++current_page
                                    };
                                    break;
                            };
                            console.log(button.replied, selectMenu.replied);
                            await button.update({
                                embeds: [
                                    matchLogEmbedFields[current_page]
                                ],
                                components: [
                                    {
                                        type: ComponentType.ActionRow,
                                        components: [collectorProfileComponents]
                                    },
                                    matchLogComponent
                                ]
                            });
                        });
                        break;
                };
            });

            profileEmbedCollector.on('end', async (_collected, reason) => {
                if (reason === 'time') {
                    collectorProfileComponents = SelectMenuBuilder.from(collectorProfileComponents.components ? collectorProfileComponents.components[0] : collectorProfileComponents).setDisabled(true);

                    return await interaction.editReply({
                        components: [
                            {
                                type: ComponentType.ActionRow,
                                components: [collectorProfileComponents]
                            }
                        ]
                    });
                };
            });