diff --git a/src/index.ts b/src/index.ts index cede599..9850b22 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,7 @@ import { DiscordAnalytics } from "@nhcarrigan/discord-analytics"; import { Client, GatewayIntentBits, Events, MessageFlags } from "discord.js"; import { blocks } from "./config/blocks.js"; import { checkAltText } from "./modules/checkAltText.js"; -import { handleAckButton } from "./modules/handleAckButton.js"; +import { processButton } from "./modules/processButton.js"; import { instantiateServer } from "./server/serve.js"; import { logger } from "./utils/logger.js"; @@ -36,19 +36,14 @@ client.on(Events.MessageCreate, (message) => { client.on(Events.InteractionCreate, (interaction) => { void analytics.logGatewayEvent(Events.InteractionCreate, { ...interaction }); - if (!interaction.isChatInputCommand() && !interaction.isButton()) { - return; - } - // Existing logic for slash commands if (interaction.isChatInputCommand()) { void interaction.reply({ components: blocks, flags: MessageFlags.IsComponentsV2, }); - return; } if (interaction.isButton()) { - void handleAckButton(interaction); + void processButton(interaction); } }); diff --git a/src/modules/checkAltText.ts b/src/modules/checkAltText.ts index df34948..7e0ea23 100644 --- a/src/modules/checkAltText.ts +++ b/src/modules/checkAltText.ts @@ -5,7 +5,6 @@ */ import { reminders } from "../config/reminders.js"; -import { createAckButton } from "../modules/createAckButton.js"; import { getRandomValue } from "../utils/getRandomValue.js"; import { logger } from "../utils/logger.js"; import type { Message } from "discord.js"; @@ -34,8 +33,20 @@ export const checkAltText = async(message: Message): Promise => { if (noDescription.size > 0) { const reminder = getRandomValue(reminders); await message.reply({ - components: createAckButton(message.author.id), - content: `${reminder}\n-# If you do not know how to do this, check [Discord's help article]()!\n-# Need help writing descriptive text? Our bot [Cordelia]() can do it for you!`, + components: [ { + components: [ + { + // eslint-disable-next-line @typescript-eslint/naming-convention -- Discord API requires this. + custom_id: message.author.id, + disabled: false, + label: "Okie Dokie!", + style: 3, + type: 2, + }, + ], + type: 1, + } ], + content: `${reminder}\n-# If you do not know how to do this, check [Discord's help article]()!\n-# Need help writing descriptive text? Our bot [Cordelia]() can do it for you!`, }).catch(() => { return null; }); diff --git a/src/modules/createAckButton.ts b/src/modules/createAckButton.ts deleted file mode 100644 index 3a99322..0000000 --- a/src/modules/createAckButton.ts +++ /dev/null @@ -1,33 +0,0 @@ -/** - * @copyright NHCarrigan - * @license Naomi's Public License - * @author Gurkirat Singh - Technical volunteer - */ - -import { - ActionRowBuilder, - ButtonBuilder, - ButtonStyle, -} from "discord.js"; - -/** - * Creates an acknowledgment button for alt-text reminders. - * @param userId - The ID of the user who can acknowledge the reminder. - * @returns An array containing an ActionRow with the acknowledgment button. - */ -const createAckButton = ( - userId: string, -): Array> => { - const button = new ButtonBuilder(). - setCustomId(`ack-${userId}`). - setLabel("Got it!"). - setStyle(ButtonStyle.Secondary). - setEmoji("✅"); - - const actionRow = new ActionRowBuilder(). - addComponents(button); - - return [ actionRow ]; -}; - -export { createAckButton }; diff --git a/src/modules/handleAckButton.ts b/src/modules/handleAckButton.ts deleted file mode 100644 index bc89084..0000000 --- a/src/modules/handleAckButton.ts +++ /dev/null @@ -1,32 +0,0 @@ -/** - * @copyright NHCarrigan - * @license Naomi's Public License - * @author Gurkirat Singh - Technical volunteer - */ - -import { type ButtonInteraction, MessageFlags } from "discord.js"; - -/** - * Handles button interactions for acknowledgment buttons. - * @param interaction - The button interaction to handle. - * @returns Promise that resolves to true if button was handled, false otherwise. - */ -const handleAckButton = async( - interaction: ButtonInteraction, -): Promise => { - if (!interaction.customId.startsWith("ack-")) { - return false; - } - const [ , authorizedUserId ] = interaction.customId.split("-"); - if (interaction.user.id !== authorizedUserId) { - await interaction.reply({ - content: "❌ This button is only for the recipient.", - flags: MessageFlags.Ephemeral, - }); - return true; - } - await interaction.message.delete(); - return true; -}; - -export { handleAckButton }; diff --git a/src/modules/processButton.ts b/src/modules/processButton.ts new file mode 100644 index 0000000..e76eb23 --- /dev/null +++ b/src/modules/processButton.ts @@ -0,0 +1,40 @@ +/** + * @copyright NHCarrigan + * @license Naomi's Public License + * @author Naomi Carrigan + */ + +import { logger } from "../utils/logger.js"; +import type { + ButtonInteraction, +} from "discord.js"; + +/** + * Handles a button interaction. If the user is not the author of the reminder, + * they cannot acknowledge it. + * @param interaction - The interaction payload from Discord. + */ +export const processButton = async( + interaction: ButtonInteraction, +): Promise => { + try { + await interaction.deferReply({ ephemeral: true }); + const { customId, user, message } = interaction; + if (customId !== user.id) { + await interaction.editReply({ + content: "You cannot acknowledge someone else's reminder.", + }); + } + await message.delete(); + await interaction.editReply({ + content: "Acknowledged!", + }); + } catch (error) { + if (error instanceof Error) { + await logger.error("process interaction module", error); + } + await interaction.editReply({ + content: "Oh dear, something went wrong.", + }); + } +};