const { Collection, Permissions, Events, ComponentType } = require('discord.js'); const { ongoingGiveaways, enteredUsers, giveawayEntriesAmount, collector } = require('/home/devbot/commands/prefix/staff_utility/giveaway.js'); module.exports = { name: "interactionCreate", async run(bot, interaction) { try { if (!interaction.guild || interaction.user.bot) return; if (interaction.channel.partial) await interaction.channel.fetch(); // if (interaction.partial) await interaction.fetch(); await interaction.guild.members.fetch({ force: true }); if (interaction.isCommand()) { const command = bot.slashcommands.get(interaction.commandName); if (!command) { interaction.reply(`An error occurred while running this command!`); return bot.slashcommands.delete(interaction.commandName); } const args = []; for (let option of interaction.options.data) { if (option.type === 'SUB_COMMAND') { if (option.name) args.push(option.name); option.options?.forEach(x => { if (x.value) args.push(x.value); }); } else if (option.value) args.push(option.value); } // For bot developers only if (command.mode.developerOnly && interaction.user.id !== bot.config.settings.developer) { return interaction.reply('This command is limited to bot developers only!'); } // For owner guild only if (command.mode.ownerGuildOnly && interaction.user.id !== interaction.guild.ownerId) { return interaction.reply('This command is limited to the owner of the guild only!'); } // Bot permissions if (command.permissions.bot) { const botPermissions = [ Permissions.FLAGS.SEND_MESSAGES, Permissions.FLAGS.VIEW_CHANNEL, Permissions.FLAGS.ATTACH_FILES, Permissions.FLAGS.SPEAK, Permissions.FLAGS.CONNECT ]; const missingPermissions = botPermissions.filter(p => !interaction.guild.me.permissions.has(p)); if (missingPermissions.length) { return interaction.reply(`Looks like I'm missing the following permissions: ${missingPermissions.map(p => `\`${p}\``).join(', ')}`); } const commandBotPermissions = command.permissions.bot.filter(p => !interaction.guild.me.permissions.has(p)); if (commandBotPermissions.length) { return interaction.reply(`Looks like I'm missing the following permissions: ${commandBotPermissions.map(p => `\`${p}\``).join(', ')}`); } } // User permissions if (command.permissions.user) { const missingPermissions = command.permissions.user.filter(p => !interaction.member.permissions.has(p)); if (missingPermissions.length) { return interaction.reply(`Looks like you're missing the following permissions: ${missingPermissions.map(p => `\`${p}\``).join(', ')}`); } } // Cooldown if (!bot.cooldowns.has(command.name)) { bot.cooldowns.set(command.name, new Collection()); } const timeCooldowns = bot.cooldowns.get(command.name); if (interaction.user.id !== bot.config.settings.developer) { if (command.cooldown) { if (timeCooldowns.has(interaction.user.id)) { const expirationTime = timeCooldowns.get(interaction.user.id) + (command.cooldown * 1000); if (Date.now() < expirationTime) { const timeLeft = (expirationTime - Date.now()) / 1000; return interaction.reply(`Please wait ${bot.utils.msToTime(timeLeft.toFixed(1) * 1000)} before using that command again!`); } } timeCooldowns.set(interaction.user.id, Date.now()); setTimeout(() => { timeCooldowns.delete(interaction.user.id); }, command.cooldown * 1000); } } command.run(bot, interaction, args, timeCooldowns); } if (interaction.isButton()) { const userId = interaction.user.id; // Giveaway.js - Button interaction // Ensure interaction.member is defined and has the roles cache if (interaction.member && interaction.member.roles && interaction.member.roles.cache) { if (interaction.customId === 'giveaway_join') { if (!enteredUsers.has(userId)) { // User hasn't entered the giveaway giveawayEntriesAmount++; // Increment the giveawayEntriesAmount enteredUsers.add(userId); // Add the user ID to the set of entered users interaction.reply({ content: `Congratulations, <@${userId}>! You have joined the giveaway. Good luck!`, ephemeral: true }).then((reply) => { // Delete the reply message after 2500 milliseconds (2.5 seconds) setTimeout(() => { reply.delete(); }, 2500); }); } else { // User has already entered the giveaway interaction.reply({ content: `<@${userId}>, you have already joined the giveaway!`, ephemeral: true }).then((reply) => { // Delete the reply message after 3000 milliseconds (2.5 seconds) setTimeout(() => { reply.delete(); }, 2500); }); } } else if (interaction.customId === 'giveaway_quit') { if (enteredUsers.has(userId)) { // User has entered the giveaway giveawayEntriesAmount--; // Decrement the giveawayEntriesAmount enteredUsers.delete(userId); // Remove the user ID from the set of entered users interaction.reply({ content: `<@${userId}>, you have quit the giveaway!`, ephemeral: true }).then((reply) => { // Delete the reply message after 3000 milliseconds (2.5 seconds) setTimeout(() => { reply.delete(); }, 2500); }); } else { // User has not entered the giveaway interaction.reply({ content: `<@${userId}>, you have not yet joined the giveaway!`, ephemeral: true }).then((reply) => { // Delete the reply message after 3000 milliseconds (2.5 seconds) setTimeout(() => { reply.delete(); }, 2500); }); } } else if (interaction.customId === 'giveaway_participant') { if (enteredUsers.size === 0) { // No participants, handle this case interaction.reply({ content: 'No participants yet.', ephemeral: true }).then((reply) => { // Delete the reply message after 3000 milliseconds (2.5 seconds) setTimeout(() => { reply.delete(); }, 2500); }); } else { // Create an embed to display the participants const participantListEmbed = new Discord.MessageEmbed() .setColor('#7FB07C') .setDescription('## 🕵🏽‍ **ɢɪᴠᴇᴀᴡᴀʏ ᴘᴀʀᴛɪᴄɪᴘᴀɴᴛꜱ**`) .addFields({ name: ` `, value: Array.from(enteredUsers).map(userId => `<@${userId}>`).join(', ') }) .setFooter({ text: bot.config.embed.footer, iconURL: bot.config.embed.serverIcon }) .setTimestamp(); // Send the embed as a reply interaction.reply({ embeds: [participantListEmbed], ephemeral: true }).then((reply) => { // Delete the reply message after 3000 milliseconds (2.5 seconds) setTimeout(() => { reply.delete(); }, 4000); }); } } else { // Handle the case where interaction.member is not as expected console.error('Invalid Giveaway interaction', interaction.member); interaction.reply({ content: 'Invalid Giveaway interaction, Please try again later.', ephemeral: true }).then((reply) => { // Delete the reply message after 3000 milliseconds (2.5 seconds) setTimeout(() => { reply.delete(); }, 2500); }); } } } } catch (error) { bot.logger.error(error); } } };