Untitled

mail@pastecode.io avatar
unknown
javascript
7 months ago
5.8 kB
1
Indexable
Never
const { setTimeout } = require('node:timers/promises')
const randomChar = require('anime-character-random')
const { EmbedBuilder, ActionRowBuilder, StringSelectMenuBuilder, ComponentType } = require('discord.js')
const AnimeGuessListSchema = require('../../schemas/AnimeGuessSchema');
const mongoose = require('mongoose');


async function loopGame(client, interaction) {
    try {

        const AnimeData = await randomChar.GetChar()
        const db = [AnimeData.OtherCharacterList[0], AnimeData.OtherCharacterList[1], AnimeData.OtherCharacterList[2], AnimeData.CharacterName]
        const list = db.sort(() => Math.random() - 0.5)
        const embed = new EmbedBuilder()
            .setTitle('Guess the anime character')
            .setAuthor({ name: interaction.user.username.charAt(0).toUpperCase() + interaction.user.username.slice(1), iconURL: interaction.member.displayAvatarURL() })
            .setDescription(`Anime Name : ${AnimeData.AnimeName}`)
            .setColor('Red')
            .setFooter({ text: 'Cziju || Guess The Anime Character', iconURL: client.user.avatarURL() })
            .setTimestamp()
            .setImage(AnimeData.CharacterImage)

        const row = new ActionRowBuilder()
            .addComponents(
                new StringSelectMenuBuilder()
                    .setCustomId('select')
                    .setPlaceholder('Select an Answer')
                    .addOptions(
                        {
                            label: list[0],
                            description: 'Choose The Correct Answer',
                            value: list[0]
                        },
                        {
                            label: list[1],
                            description: 'Choose The Correct Answer',
                            value: list[1]
                        },
                        {
                            label: list[2],
                            description: 'Choose The Correct Answer',
                            value: list[2]
                        },
                        {
                            label: list[3],
                            description: 'Choose The Correct Answer',
                            value: list[3]
                        }
                    )
            )
        const message = await interaction.editReply({ content: 'You Have **30 Seconds** To answer', components: [row], embeds: [embed], fetchReply: true })

        const filter = i => i.user.id === interaction.user.id

        const collector = message.createMessageComponentCollector({ filter, componentType: ComponentType.SelectMenu, time: 30000 })

        collector.on('collect', async i => {
            if (i.values[0] === AnimeData.CharacterName) {
                await interaction.editReply({ content: 'You have Answered **Correctly**', components: [] })
                let getUserProfile = await AnimeGuessListSchema.findOne({ guildId: interaction.guild.id });

                if (!getUserProfile) {
                    getUserProfile = new AnimeGuessListSchema({
                        _id: new mongoose.Types.ObjectId(),
                        guildId: interaction.guild.id,
                        memberId: interaction.member.id,
                        points: 1
                    });

                    await getUserProfile.save();
                } else {
                    console.log('USPESNO');
                    getUserProfile.updateOne({ guildId: interaction.guild.id, memberId: interaction.user.id }, {
                        $inc: {
                            points: 100
                        }
                    })
                }


            } else {
                await interaction.editReply({ content: `You have Answered **Incorrectly**\n The correct answer is **${AnimeData.CharacterName}**`, components: [] })
                let getUserProfile = await AnimeGuessListSchema.findOne({ guildId: interaction.guild.id });

                if (!getUserProfile) {
                    getUserProfile = new AnimeGuessListSchema({
                        _id: new mongoose.Types.ObjectId(),
                        guildId: interaction.guild.id,
                        memberId: interaction.member.id,
                        points: 1
                    });

                    await getUserProfile.save();
                } else {
                    console.log('USPESNO');
                    getUserProfile.updateOne({ guildId: interaction.guild.id, memberId: interaction.user.id }, {
                        $inc: {
                            points: 100
                        }
                    })
                }
            }
            loopGame(interaction);
        })

        collector.on('end', async collected => {
            if (collected.size === 0) await interaction.editReply({ content: `🕛 Time's Up\n The correct answer is **${AnimeData.CharacterName}**`, components: [] }).catch(error => {
                console.log('Unkown Message!')
            });
        })

    } catch (err) {
        console.log("Error")
        console.log(err)
    }
}
module.exports = {
    name: 'guess-anime-character',
    description: 'Guess the anime character from the image!',
    // devOnly: Boolean,
    testOnly: true,
    // options: Object[],
    //deleted: true,
    callback: async (client, interaction) => {
        try {
            await interaction.reply('*Working on it...*');
            loopGame(interaction); // Start the game initially
        } catch (err) {
            console.log("Error");
            console.log(err)
        }

    }
};
Leave a Comment