diff --git a/src/events/interactionCreate/handlers/button/index.ts b/src/events/interactionCreate/handlers/button/index.ts index e3e8281..092b5dd 100644 --- a/src/events/interactionCreate/handlers/button/index.ts +++ b/src/events/interactionCreate/handlers/button/index.ts @@ -1,14 +1,41 @@ -// Dependencies -import { BaseInteraction } from "discord.js"; - -export default async (interaction: BaseInteraction) => { - if (!interaction.isButton()) return; +import { + ActionRowBuilder, + ButtonBuilder, + ButtonInteraction, + ButtonStyle, + EmbedBuilder, +} from "discord.js"; +import getEmbedData from "../../../../helpers/getEmbedData"; +export default async (interaction: ButtonInteraction) => { + const { errorColor, footerText, footerIcon } = await getEmbedData( + interaction.guild + ); const { customId } = interaction; const currentButton = await import(`../../../buttons/${customId}`); if (!currentButton) throw new Error(`Unknown button ${customId}`); - await currentButton.execute(interaction); + await currentButton.execute(interaction).catch((error: Error) => { + const buttons = new ActionRowBuilder().addComponents( + new ButtonBuilder() + .setLabel("Report Problem") + .setStyle(ButtonStyle.Link) + .setEmoji("✏️") + .setURL("https://discord.zyner.org") + ); + + return interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setTitle(`:no_entry_sign:︱Your request failed`) + .setDescription(`${error.message}`) + .setColor(errorColor) + .setTimestamp(new Date()) + .setFooter({ text: footerText, iconURL: footerIcon }), + ], + components: [buttons], + }); + }); }; diff --git a/src/events/interactionCreate/handlers/chatInputCommand/index.ts b/src/events/interactionCreate/handlers/chatInputCommand/index.ts new file mode 100644 index 0000000..78ca532 --- /dev/null +++ b/src/events/interactionCreate/handlers/chatInputCommand/index.ts @@ -0,0 +1,42 @@ +import { + ActionRowBuilder, + ButtonBuilder, + ButtonStyle, + ChatInputCommandInteraction, + EmbedBuilder, +} from "discord.js"; +import getEmbedData from "../../../../helpers/getEmbedData"; + +export default async (interaction: ChatInputCommandInteraction) => { + const { errorColor, footerText, footerIcon } = await getEmbedData( + interaction.guild + ); + + if (!interaction.isCommand()) return; + const { client, commandName } = interaction; + + const currentCommand = client.commands.get(commandName); + if (!currentCommand) throw new Error(`Unknown command ${commandName}`); + + await currentCommand.execute(interaction).catch((error: Error) => { + const buttons = new ActionRowBuilder().addComponents( + new ButtonBuilder() + .setLabel("Report Problem") + .setStyle(ButtonStyle.Link) + .setEmoji("✏️") + .setURL("https://discord.zyner.org") + ); + + return interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setTitle(`:no_entry_sign:︱Your request failed`) + .setDescription(`${error.message}`) + .setColor(errorColor) + .setTimestamp(new Date()) + .setFooter({ text: footerText, iconURL: footerIcon }), + ], + components: [buttons], + }); + }); +}; diff --git a/src/events/interactionCreate/handlers/command/index.ts b/src/events/interactionCreate/handlers/command/index.ts deleted file mode 100644 index c49f7d2..0000000 --- a/src/events/interactionCreate/handlers/command/index.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Dependencies -import { ChatInputCommandInteraction } from "discord.js"; - -export default async (interaction: ChatInputCommandInteraction) => { - if (!interaction.isCommand()) return; - const { client, commandName } = interaction; - - const currentCommand = client.commands.get(commandName); - if (!currentCommand) throw new Error(`Unknown command ${commandName}`); - - await currentCommand.execute(interaction); -}; diff --git a/src/events/interactionCreate/handlers/index.ts b/src/events/interactionCreate/handlers/index.ts deleted file mode 100644 index 8e9d8c3..0000000 --- a/src/events/interactionCreate/handlers/index.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { - ActionRowBuilder, - BaseInteraction, - ButtonBuilder, - ButtonInteraction, - ButtonStyle, - ChatInputCommandInteraction, - CommandInteraction, - EmbedBuilder, -} from "discord.js"; -import getEmbedConfig from "../../../helpers/getEmbedData"; -import button from "./button"; -import command from "./command"; - -// Send interactions to all available handlers -export const execute = async (interaction: BaseInteraction) => { - await button(interaction); - await command(interaction); -}; - -// Handle interactions from commands -export const handleCommandInteraction = async ( - interaction: CommandInteraction -) => { - const { errorColor, footerText, footerIcon } = await getEmbedConfig( - interaction.guild - ); - - await command(interaction).catch((err) => { - const buttons = new ActionRowBuilder().addComponents( - new ButtonBuilder() - .setLabel("Report Problem") - .setStyle(ButtonStyle.Link) - .setEmoji("📝") - .setURL("https://discord.zyner.org") - ); - - return interaction.editReply({ - embeds: [ - new EmbedBuilder() - .setTitle(`:no_entry_sign:︱Your request failed`) - .setDescription(`${err.message}`) - .setColor(errorColor) - .setTimestamp(new Date()) - .setFooter({ text: footerText, iconURL: footerIcon }), - ], - components: [buttons], - }); - }); -}; diff --git a/src/events/interactionCreate/index.ts b/src/events/interactionCreate/index.ts index c7e9c63..86a21c5 100644 --- a/src/events/interactionCreate/index.ts +++ b/src/events/interactionCreate/index.ts @@ -1,34 +1,39 @@ -// 3rd party dependencies -import { - BaseInteraction, - CommandInteraction, - InteractionType, -} from "discord.js"; +import { BaseInteraction, InteractionType } from "discord.js"; +import upsertGuildMember from "../../helpers/upsertGuildMember"; import { IEventOptions } from "../../interfaces/EventOptions"; import logger from "../../middlewares/logger"; -// Dependencies import audits from "./audits"; -import { handleCommandInteraction as HandlersHandleCommandInteraction } from "./handlers"; +import button from "./handlers/button"; +import chatInputCommand from "./handlers/chatInputCommand"; export const options: IEventOptions = { type: "on", }; -// Execute the event export const execute = async (interaction: BaseInteraction) => { - const { guild, id } = interaction; + logger.silly({ interaction }); + const { guild, user } = interaction; - logger?.silly( - `New interaction: ${id} in guild: ${guild?.name} (${guild?.id})` - ); + if (guild) { + await upsertGuildMember(guild, user); + } switch (interaction.type) { - case InteractionType.ApplicationCommand: - await HandlersHandleCommandInteraction(interaction); - break; + case InteractionType.ApplicationCommand: { + if (interaction.isChatInputCommand()) { + await chatInputCommand(interaction); + return; + } + if (interaction.isButton()) { + await button(interaction); + return; + } + + break; + } default: - logger?.error(`Unknown interaction type: ${interaction.type}`); + throw new Error("Unknown interaction type"); } await audits.execute(interaction);