const { SlashCommandBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } = require('discord.js'); const Character = require('../models/Character'); const User = require('../models/User'); const Asset = require('../models/Asset'); module.exports = { data: new SlashCommandBuilder() .setName('customize') .setDescription('Customize your character by selecting assets from your inventory'), async execute(interaction) { const user = await User.findOne({ discordId: interaction.user.id }).populate('activeCharacter inventory.masks inventory.backgrounds inventory.colors'); if (!user) return interaction.reply({ content: 'User not found.', ephemeral: true }); const optionsMenu = new ActionRowBuilder().addComponents( new ButtonBuilder().setCustomId('background').setLabel('Background').setStyle(ButtonStyle.Primary), new ButtonBuilder().setCustomId('avatarShape').setLabel('Avatar Shape').setStyle(ButtonStyle.Primary), new ButtonBuilder().setCustomId('embedColor').setLabel('Embed Color').setStyle(ButtonStyle.Primary) ); await interaction.reply({ content: 'Choose what you want to customize:', components: [optionsMenu], ephemeral: true }); const filter = i => i.user.id === interaction.user.id; const collector = interaction.channel.createMessageComponentCollector({ filter, time: 60000 }); collector.on('collect', async i => { let assets, title, field; if (i.customId === 'background') { assets = user.inventory.backgrounds; title = 'Choose your background'; field = 'background'; } else if (i.customId === 'avatarShape') { assets = user.inventory.masks; title = 'Choose your avatar shape'; field = 'avatarShape'; } else if (i.customId === 'embedColor') { assets = user.inventory.colors; // Embed colors are directly stored as strings in the user's inventory title = 'Choose your embed color'; field = 'embedColor'; } if (field !== 'embedColor') { // Fetch assets from the database assets = await Asset.find({ _id: { $in: assets } }); if (!assets || assets.length === 0) { return i.update({ content: `No assets found for ${field}.`, components: [], ephemeral: true }); } } let currentIndex = 0; const updateEmbed = () => { const embed = new EmbedBuilder() .setTitle(title) .setDescription(`Current selection: ${field === 'embedColor' ? assets[currentIndex] : assets[currentIndex].name}`) .setFooter({ text: `Asset ${currentIndex + 1} of ${assets.length}` }); if (field === 'embedColor') { embed.setColor(assets[currentIndex]); } else { embed.setImage(assets[currentIndex].url); } return embed; }; const buttons = new ActionRowBuilder().addComponents( new ButtonBuilder().setCustomId('previous').setLabel('Previous').setStyle(ButtonStyle.Secondary), new ButtonBuilder().setCustomId('next').setLabel('Next').setStyle(ButtonStyle.Secondary), new ButtonBuilder().setCustomId('confirm').setLabel('Confirm').setStyle(ButtonStyle.Success), new ButtonBuilder().setCustomId('cancel').setLabel('Cancel').setStyle(ButtonStyle.Danger) ); await i.update({ embeds: [updateEmbed()], components: [buttons] }); const buttonCollector = interaction.channel.createMessageComponentCollector({ filter, time: 60000 }); buttonCollector.on('collect', async buttonInteraction => { if (buttonInteraction.customId === 'previous') { currentIndex = (currentIndex - 1 + assets.length) % assets.length; await buttonInteraction.update({ embeds: [updateEmbed()] }); } else if (buttonInteraction.customId === 'next') { currentIndex = (currentIndex + 1) % assets.length; await buttonInteraction.update({ embeds: [updateEmbed()] }); } else if (buttonInteraction.customId === 'confirm') { if (field === 'background') user.activeCharacter.assets.background = assets[currentIndex]._id; else if (field === 'avatarShape') user.activeCharacter.assets.avatarShape = assets[currentIndex]._id; else if (field === 'embedColor') user.activeCharacter.assets.embedColor = assets[currentIndex]; await user.activeCharacter.save(); await buttonInteraction.update({ content: 'Selection confirmed!', components: [], embeds: [] }); buttonCollector.stop(); } else if (buttonInteraction.customId === 'cancel') { await buttonInteraction.update({ content: 'Action cancelled.', components: [], embeds: [] }); buttonCollector.stop(); } }); buttonCollector.on('end', async () => { if (!buttonCollector.ended) await interaction.editReply({ components: [] }); }); }); } };