feat: add button to clear reminder #3

Merged
naomi merged 5 commits from Add-button-to-clear-reminder-#2 into main 2025-11-02 09:23:30 -08:00
3 changed files with 68 additions and 42 deletions
Showing only changes of commit 22f741e585 - Show all commits
+24 -15
View File
@@ -14,7 +14,7 @@ import {
ActionRowBuilder,
} from "discord.js";
export const blocks = [
const blocks = [
new ContainerBuilder().
addTextDisplayComponents(
new TextDisplayBuilder().setContent("# About Altaria"),
@@ -65,20 +65,29 @@ export const blocks = [
),
];
// config for the acknowledgment button
// Config for the acknowledgment button
export const createAckButton = (userId: string) => {
const button = new ButtonBuilder()
.setCustomId(`ack-${userId}`)
.setLabel("Got it!")
.setStyle(ButtonStyle.Secondary)
.setEmoji("✅");
/**
* 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 = (
Outdated
Review

This function should be in its own src/modules/createAckButton.ts file.

This function should be in its own `src/modules/createAckButton.ts` file.
userId: string,
): Array<ActionRowBuilder<ButtonBuilder>> => {
const button = new ButtonBuilder().
setCustomId(`ack-${userId}`).
setLabel("Got it!").
setStyle(ButtonStyle.Secondary).
setEmoji("✅");
const actionRow = new ActionRowBuilder<ButtonBuilder>()
.addComponents(button);
const actionRow = new ActionRowBuilder<ButtonBuilder>().
addComponents(button);
return [actionRow];
};
export const replyForUnauthorized = "❌ This button is only for the person who received the reminder.";
return [ actionRow ];
};
const replyForUnauthorized
= "❌ This button is only for the person who received the reminder.";
export { blocks, createAckButton, replyForUnauthorized };
+2 -1
View File
1
@@ -33,9 +33,10 @@ export const checkAltText = async(message: Message): Promise<void> => {
if (noDescription.size > 0) {
const reminder = getRandomValue(reminders);
// Button for acknowledgment
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](<https://support.discord.com/hc/en-us/articles/211866427-How-do-I-upload-images-and-GIFs>)!\n-# Need help writing descriptive text? Our bot [Cordelia](<https://cordelia.nhcarrigan.com>) can do it for you!`, // Button for acknowledgment
content: `${reminder}\n-# If you do not know how to do this, check [Discord's help article](<https://support.discord.com/hc/en-us/articles/211866427-How-do-I-upload-images-and-GIFs>)!\n-# Need help writing descriptive text? Our bot [Cordelia](<https://cordelia.nhcarrigan.com>) can do it for you!`,
}).catch(() => {
return null;
});
+42 -26
View File
@@ -4,37 +4,53 @@
* @author Gurkirat Singh - Technical volunteer
*/
import { ButtonInteraction, MessageFlags } from "discord.js";
import { createAckButton as createAckButtonConfig } from "../config/blocks.js";
import { replyForUnauthorized } from "../config/blocks.js";
import { type ButtonInteraction, MessageFlags } from "discord.js";
import {
createAckButton as createAckButtonConfig,
replyForUnauthorized,
} from "../config/blocks.js";
/**
* Creates an acknowledgment button for alt-text reminders
* @param userId - The ID of the user who can acknowledge the reminder
* @returns ActionRow with the acknowledgment button
* Creates an acknowledgment button for alt-text reminders.
* @param userId - The ID of the user who can acknowledge the reminder.
* @returns ActionRow with the acknowledgment button.
*/
export const createAckButton = (userId: string) => {
const createAckButton = (
Outdated
Review

This function is entirely unnecessary - after moving the other function to a module file, we can just use that directly.

This function is entirely unnecessary - after moving the other function to a module file, we can just use that directly.
userId: string,
): ReturnType<typeof createAckButtonConfig> => {
return createAckButtonConfig(userId);
};
// function to handle button interaction
export const handleAckButton = async (interaction: ButtonInteraction) => {
if (!interaction.customId.startsWith("ack-")) {
return false; // Not our button
}
const authorizedUserId = interaction.customId.split("-")[1];
if (interaction.user.id !== authorizedUserId) {
// Show error that auto-deletes after 2 seconds
await interaction.reply({ content: replyForUnauthorized, flags: MessageFlags.Ephemeral });
setTimeout(() => interaction.deleteReply().catch(() => {}), 2000);
return true;
}
/**
* Handles button interaction for acknowledgment buttons.
* @param interaction - The button interaction to handle.
* @returns Promise that resolves to true if the button was handled, false otherwise.
*/
const handleAckButton = async(
Outdated
Review

This should get its own module file as well, in src/modules/handleAckButton.ts.

This should get its own module file as well, in `src/modules/handleAckButton.ts`.
interaction: ButtonInteraction,
): Promise<boolean> => {
if (!interaction.customId.startsWith("ack-")) {
// Not our button
return false;
}
await interaction.message.delete();
const [ , authorizedUserId ] = interaction.customId.split("-");
if (interaction.user.id !== authorizedUserId) {
// Show error that auto-deletes after 2 seconds
await interaction.reply({
content: replyForUnauthorized,
flags: MessageFlags.Ephemeral,
});
setTimeout(() => {
void interaction.deleteReply().catch(() => {
// Intentionally empty - ignore deletion errors
});
}, 2000);
return true;
}
}
await interaction.message.delete();
return true;
};
export { createAckButton, handleAckButton };