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
5 changed files with 56 additions and 75 deletions
Showing only changes of commit 36e3a5370d - Show all commits
+2 -7
View File
@@ -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";
1
@@ -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);
}
});
+13 -2
View File
@@ -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";
1
@@ -34,7 +33,19 @@ export const checkAltText = async(message: Message): Promise<void> => {
if (noDescription.size > 0) {
const reminder = getRandomValue(reminders);
await message.reply({
components: createAckButton(message.author.id),
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](<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;
-33
View File
@@ -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<ActionRowBuilder<ButtonBuilder>> => {
const button = new ButtonBuilder().
setCustomId(`ack-${userId}`).
setLabel("Got it!").
setStyle(ButtonStyle.Secondary).
setEmoji("✅");
const actionRow = new ActionRowBuilder<ButtonBuilder>().
addComponents(button);
return [ actionRow ];
};
export { createAckButton };
-32
View File
@@ -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<boolean> => {
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 };
+40
View File
@@ -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<void> => {
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.",
});
}
};