diff --git a/commands/ticket.js b/commands/ticket.js index 63a1cf2..e5892f7 100644 --- a/commands/ticket.js +++ b/commands/ticket.js @@ -7,7 +7,6 @@ const { SlashCommandBuilder, PermissionFlagsBits, } = require("discord.js"); -const wait = require("node:timers/promises").setTimeout; module.exports = { data: new SlashCommandBuilder() @@ -16,14 +15,10 @@ module.exports = { .setDefaultMemberPermissions(PermissionFlagsBits.Administrator), async execute(interaction) { - console.log(interaction); - let channel; - await interaction - .deferReply - // { ephemeral: true } - (); + await interaction.deferReply({ ephemeral: true }); try { + let channel; const ticketEmbed = new EmbedBuilder() .setColor(0x0099ff) .setTitle("Ticket Menu") @@ -47,16 +42,6 @@ module.exports = { channel = await interaction.guild.channels.create({ name: `ticket`, type: ChannelType.GuildText, - permissionOverwrites: [ - // { - // id: interaction.user.id, - // allow: [PermissionFlagsBits.ViewChannel], - // }, - { - id: interaction.guild.roles.everyone.id, - deny: [PermissionFlagsBits.ViewChannel], - }, - ], }); channel.send({ @@ -64,8 +49,6 @@ module.exports = { components: [new ActionRowBuilder().addComponents(openTicket)], }); await interaction.editReply(`Ticket is ready, move on <#${channel.id}>`); - await wait(1000 * 6); - channel.delete(); } catch (err) { await interaction.editReply( `Tickets cannot be created \`\`\`\n${err}\`\`\``, diff --git a/events/interactionCreate.js b/events/interactionCreate.js index 13e8ebe..36f2fa5 100644 --- a/events/interactionCreate.js +++ b/events/interactionCreate.js @@ -1,50 +1,107 @@ -const { Collection, Events } = require("discord.js"); +const { + Collection, + ChannelType, + PermissionFlagsBits, + Events, +} = require("discord.js"); module.exports = { name: Events.InteractionCreate, async execute(interaction) { - if (!interaction.isChatInputCommand()) return; + if (interaction.isChatInputCommand()) { + // respond to the Chat - const command = interaction.client.commands.get(interaction.commandName); + const command = interaction.client.commands.get(interaction.commandName); - if (!command) { - console.error( - `No command matching ${interaction.commandName} was found.`, - ); - return; - } + if (!command) { + console.error( + `No command matching ${interaction.commandName} was found.`, + ); + return; + } - const { cooldowns } = interaction.client; + const { cooldowns } = interaction.client; - if (!cooldowns.has(command.data.name)) { - cooldowns.set(command.data.name, new Collection()); - } + 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 defaultCooldownDuration = 3; + const cooldownAmount = + (command.cooldown ?? defaultCooldownDuration) * 1000; + + if (timestamps.has(interaction.user.id)) { + const expirationTime = + timestamps.get(interaction.user.id) + cooldownAmount; - const now = Date.now(); - const timestamps = cooldowns.get(command.data.name); - const defaultCooldownDuration = 3; - const cooldownAmount = (command.cooldown ?? defaultCooldownDuration) * 1000; - - if (timestamps.has(interaction.user.id)) { - const expirationTime = - timestamps.get(interaction.user.id) + cooldownAmount; - - if (now < expirationTime) { - const expiredTimestamp = Math.round(expirationTime / 1000); - return interaction.reply({ - content: `Please wait, you are on a cooldown for \`${command.data.name}\`. You can use it again .`, - ephemeral: true, - }); + if (now < expirationTime) { + const expiredTimestamp = Math.round(expirationTime / 1000); + return interaction.reply({ + content: `Please wait, you are on a cooldown for \`${command.data.name}\`. You can use it again .`, + ephemeral: true, + }); + } } - } - try { - await command.execute(interaction); - timestamps.set(interaction.user.id, now); - setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount); - } catch (error) { - console.error(`Error executing ${interaction.commandName}`); - console.error(error); + try { + await command.execute(interaction); + timestamps.set(interaction.user.id, now); + setTimeout( + () => timestamps.delete(interaction.user.id), + cooldownAmount, + ); + } catch (error) { + console.error(`Error executing ${interaction.commandName}`); + console.error(error); + } + } else if (interaction.isButton()) { + // respond to the button + try { + let channel; + await interaction.deferReply({ ephemeral: true }); + switch (interaction.customId) { + case "open-ticket": + try { + channel = await interaction.guild.channels.create({ + name: `t-${interaction.user.username}`, + type: ChannelType.GuildText, + permissionOverwrites: [ + { + id: interaction.user.id, + allow: [PermissionFlagsBits.ViewChannel], + }, + { + id: interaction.guild.roles.everyone.id, + deny: [PermissionFlagsBits.ViewChannel], + }, + ], + }); + + channel.send({ + content: `<@${interaction.user.id}>`, + // embeds: [ticketEmbed], + // components: [new ActionRowBuilder().addComponents(openTicket)], + }); + + await interaction.editReply( + `Ticket is been created, move on <#${channel.id}>`, + ); + } catch (error) { + console.error(error); + } + break; + + default: + break; + } + } catch (error) { + console.error(`Error executing ${interaction.customId}`); + console.error(error); + } + } else if (interaction.isStringSelectMenu()) { + // respond to the select menu } }, };