diff --git a/src/index.ts b/src/index.ts index a41ee18..9850b22 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +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 { processButton } from "./modules/processButton.js"; import { instantiateServer } from "./server/serve.js"; import { logger } from "./utils/logger.js"; @@ -35,13 +36,15 @@ client.on(Events.MessageCreate, (message) => { client.on(Events.InteractionCreate, (interaction) => { void analytics.logGatewayEvent(Events.InteractionCreate, { ...interaction }); - if (!interaction.isChatInputCommand()) { - return; + if (interaction.isChatInputCommand()) { + void interaction.reply({ + components: blocks, + flags: MessageFlags.IsComponentsV2, + }); + } + if (interaction.isButton()) { + void processButton(interaction); } - void interaction.reply({ - components: blocks, - flags: MessageFlags.IsComponentsV2, - }); }); await client.login(process.env.BOT_TOKEN); diff --git a/src/modules/checkAltText.ts b/src/modules/checkAltText.ts index 2883567..7e0ea23 100644 --- a/src/modules/checkAltText.ts +++ b/src/modules/checkAltText.ts @@ -33,6 +33,19 @@ export const checkAltText = async(message: Message): Promise => { if (noDescription.size > 0) { const reminder = getRandomValue(reminders); await message.reply({ + 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/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.", + }); + } +};