diff --git a/src/events/autocomplete/unban.js b/src/events/autocomplete/unban.js index 91db9c4..d3037ae 100644 --- a/src/events/autocomplete/unban.js +++ b/src/events/autocomplete/unban.js @@ -6,7 +6,6 @@ module.exports = { /** @param { import("discord.js").AutocompleteInteraction } interaction */ async execute(interaction) { - if (interaction.commandName !== "unban") return const focusedOption = interaction.options.getFocused(true) if (focusedOption.name !== "user") return diff --git a/src/events/modals/denyreasonbox.js b/src/events/modals/denyreasonbox.js index 1f5cbd4..24beaf2 100644 --- a/src/events/modals/denyreasonbox.js +++ b/src/events/modals/denyreasonbox.js @@ -1,4 +1,4 @@ -const { InteractionType, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require("discord.js") +const { EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require("discord.js") const { color } = require("../../../config/options.json") const guildapp = require("../../schemas/guildAppSchema.js") @@ -10,10 +10,6 @@ module.exports = { /** @param { import('discord.js').ModalSubmitInteraction } interaction */ async execute(interaction) { - - if (interaction.type !== InteractionType.ModalSubmit) return - if (interaction.customId !== "denyreasonbox") return - interaction.deferReply() const guild = interaction.guild diff --git a/src/events/modals/staffdenyreasonbox.js b/src/events/modals/staffdenyreasonbox.js index 45cb0f7..c5747b1 100644 --- a/src/events/modals/staffdenyreasonbox.js +++ b/src/events/modals/staffdenyreasonbox.js @@ -1,4 +1,4 @@ -const { InteractionType, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require("discord.js") +const { EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require("discord.js") const { color } = require("../../../config/options.json") const staffapp = require("../../schemas/staffAppSchema.js") @@ -10,10 +10,6 @@ module.exports = { /** @param { import('discord.js').ModalSubmitInteraction } interaction */ async execute(interaction) { - - if (interaction.type !== InteractionType.ModalSubmit) return - if (interaction.customId !== "staffdenyreasonbox") return - interaction.deferReply() const guild = interaction.guild diff --git a/src/events/modals/verifyModal.js b/src/events/modals/verifyModal.js index 9289865..6173e8b 100644 --- a/src/events/modals/verifyModal.js +++ b/src/events/modals/verifyModal.js @@ -1,4 +1,3 @@ -const { InteractionType } = require("discord.js") const { getUUID, getPlayer, getGuild, getHeadURL } = require("../../utils/utils.js") const { color, hypixelGuildID, devMessage } = require("../../../config/options.json") const verify = require("../../schemas/verifySchema.js") @@ -12,7 +11,6 @@ module.exports = { /** @param { import('discord.js').ModalSubmitInteraction } interaction */ async execute(interaction) { - if (interaction.type !== InteractionType.ModalSubmit) return await interaction.deferReply({ ephemeral: true }) const user1 = interaction.user diff --git a/src/index.js b/src/index.js index b1d4e20..50019d3 100644 --- a/src/index.js +++ b/src/index.js @@ -24,8 +24,9 @@ const client = new Client({ }) client.commands = new Collection() -client.events = new Collection() +client.buttons = new Collection() client.modals = new Collection() +client.autocomplete = new Collection() init() loadSlashCommandsEvents(client) diff --git a/src/utils/eventHandlers/autocomplete.js b/src/utils/eventHandlers/autocomplete.js index 16daad2..21bb8f9 100644 --- a/src/utils/eventHandlers/autocomplete.js +++ b/src/utils/eventHandlers/autocomplete.js @@ -14,22 +14,31 @@ function loadAutocompleteEvents(client) { const autocomplete = require(filePath) if ("name" in autocomplete && "execute" in autocomplete && autocomplete.type === "autocomplete") { - client.on(Events.InteractionCreate, async interaction => { - if (!interaction.isAutocomplete()) return - - try { - await autocomplete.execute(interaction) - } catch (error) { - console.error(error) - await interaction.respond({ - content: "There was an error while executing this command!", - ephemeral: true - }) - } - }) + client.autocomplete.set(autocomplete.name, autocomplete) } else { console.log(`[WARNING] The autocomplete at ${filePath} is missing a required "name", "execute" or "type" property.`) } + + client.on(Events.InteractionCreate, async interaction => { + if (!interaction.isAutocomplete()) return + + const autocomplete = interaction.client.autocomplete.get(interaction.commandName) + + if (!autocomplete) { + console.error(`No autocomplete matching ${interaction.commandName} was found.`) + return + } + + try { + await autocomplete.execute(interaction, client) + } catch (error) { + console.error(error) + await interaction.respond({ + content: "There was an error while executing this autocomplete!", + ephemeral: true + }) + } + }) } } diff --git a/src/utils/eventHandlers/button.js b/src/utils/eventHandlers/button.js index 3e7fbf3..960726b 100644 --- a/src/utils/eventHandlers/button.js +++ b/src/utils/eventHandlers/button.js @@ -14,28 +14,28 @@ function loadButtonEvents(client) { const btn = require(filePath) if ("name" in btn && "execute" in btn && btn.type === "button") { - client.events.set(btn.name, btn) + client.buttons.set(btn.name, btn) } else { console.log(`[WARNING] The button at ${filePath} is missing a required "name", "execute" or "type" property.`) } } - client.on(Events.InteractionCreate, async event => { - if (!event.isButton()) + client.on(Events.InteractionCreate, async interaction => { + if (!interaction.isButton()) return - const event2 = event.client.events.get(event.customId) + const button = interaction.client.buttons.get(interaction.customId) - if (!event2) { - console.error(`No event matching ${event.customId} was found.`) + if (!button) { + console.error(`No event matching ${interaction.customId} was found.`) return } try { - await event2.execute(event) + await button.execute(interaction) } catch (error) { console.error(error) - await event.reply({ + await interaction.reply({ content: "There was an error while executing this event!", ephemeral: true }) diff --git a/src/utils/eventHandlers/modal.js b/src/utils/eventHandlers/modal.js index 9575e2a..f7b9d67 100644 --- a/src/utils/eventHandlers/modal.js +++ b/src/utils/eventHandlers/modal.js @@ -14,11 +14,33 @@ function loadModalEvents(client) { const modal = require(filePath) if ("name" in modal && "execute" in modal && modal.type === "modal") { - client.on(Events.InteractionCreate, modal.execute) + client.modals.set(modal.name, modal) } else { console.log(`[WARNING] The modal at ${filePath} is missing a required "name", "execute" or "type" property.`) } } + + client.on(Events.InteractionCreate, async interaction => { + if (!interaction.isModalSubmit()) + return + + const modal = interaction.client.modals.get(interaction.customId) + + if (!modal) { + console.error(`No modal matching ${interaction.customId} was found.`) + return + } + + try { + await modal.execute(interaction) + } catch (error) { + console.error(error) + await interaction.reply({ + content: "There was an error while executing this modal!", + ephemeral: true + }) + } + }) } module.exports = { loadModalEvents }