Revamped event handlers
This commit is contained in:
@@ -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
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
})
|
||||
|
||||
@@ -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 }
|
||||
|
||||
Reference in New Issue
Block a user