const { Events, MessageFlags, Collection } = require('discord.js'); async function safeReply(interaction, content) { if (interaction.replied || interaction.deferred) { await interaction.editReply(content).catch(() => { interaction.followUp({ content, flags: MessageFlags.Ephemeral }).catch(console.error); }); } else { await interaction.reply({ content, flags: MessageFlags.Ephemeral }).catch(console.error); } } module.exports = { name: Events.InteractionCreate, async execute(interaction) { if (interaction.isChatInputCommand()) { const command = interaction.client.commands.get(interaction.commandName); if (!command) return console.error(`No command matching ${interaction.commandName} found.`); const { cooldowns } = interaction.client; if (!cooldowns.has(command.data.name)) cooldowns.set(command.data.name, new Collection()); const now = Date.now(); const timestamps = cooldowns.get(command.data.name); const defaultCooldown = 3; const cooldownAmount = (command.cooldown ?? defaultCooldown) * 1000; if (timestamps.has(interaction.user.id)) { const expirationTime = timestamps.get(interaction.user.id) + cooldownAmount; if (now < expirationTime) { const expired = Math.round(expirationTime / 1000); return interaction.reply({ content: `Please wait, you are on cooldown for \`${command.data.name}\`. Try again .`, flags: MessageFlags.Ephemeral, }); } } timestamps.set(interaction.user.id, now); setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount); try { await command.execute(interaction); } catch (error) { console.error(error); await safeReply(interaction, '❌ There was an error while executing this command!'); } } else if (interaction.isButton()) { if (interaction.customId === 'acceptnotifications') { const notifiRoleId = '1401849479149391882'; try { if (interaction.member.roles.cache.has(notifiRoleId)) { await interaction.reply({ content: 'You already have the notification role!', flags: MessageFlags.Ephemeral }); } else { await interaction.member.roles.add(notifiRoleId); await interaction.reply({ content: 'Thank you! Hope to see you soon in my streams!', flags: MessageFlags.Ephemeral }); } } catch (error) { console.error(error); await interaction.reply({ content: 'Something went wrong', flags: MessageFlags.Ephemeral }); } } if (interaction.customId === 'acceptrules') { const viewerRoleId = '1401153107601395774'; try { if (interaction.member.roles.cache.has(viewerRoleId)) { await interaction.reply({ content: 'You already accepted the rules!', flags: MessageFlags.Ephemeral }); } else { await interaction.member.roles.add(viewerRoleId); await interaction.reply({ content: 'Thank you for accepting the rules!', flags: MessageFlags.Ephemeral }); } } catch (error) { console.error(error); await interaction.reply({ content: 'Something went wrong', flags: MessageFlags.Ephemeral }); } } } else if (interaction.isStringSelectMenu()) { // handle select menu } }, };