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 62 additions and 6 deletions
+6 -3
View File
@@ -8,6 +8,7 @@ import { DiscordAnalytics } from "@nhcarrigan/discord-analytics";
import { Client, GatewayIntentBits, Events, MessageFlags } from "discord.js"; import { Client, GatewayIntentBits, Events, MessageFlags } from "discord.js";
import { blocks } from "./config/blocks.js"; import { blocks } from "./config/blocks.js";
import { checkAltText } from "./modules/checkAltText.js"; import { checkAltText } from "./modules/checkAltText.js";
import { processButton } from "./modules/processButton.js";
import { instantiateServer } from "./server/serve.js"; import { instantiateServer } from "./server/serve.js";
import { logger } from "./utils/logger.js"; import { logger } from "./utils/logger.js";
1
@@ -35,13 +36,15 @@ client.on(Events.MessageCreate, (message) => {
client.on(Events.InteractionCreate, (interaction) => { client.on(Events.InteractionCreate, (interaction) => {
void analytics.logGatewayEvent(Events.InteractionCreate, { ...interaction }); void analytics.logGatewayEvent(Events.InteractionCreate, { ...interaction });
if (!interaction.isChatInputCommand()) { if (interaction.isChatInputCommand()) {
return;
}
void interaction.reply({ void interaction.reply({
components: blocks, components: blocks,
flags: MessageFlags.IsComponentsV2, flags: MessageFlags.IsComponentsV2,
}); });
}
if (interaction.isButton()) {
void processButton(interaction);
}
}); });
await client.login(process.env.BOT_TOKEN); await client.login(process.env.BOT_TOKEN);
+13
View File
1
@@ -33,6 +33,19 @@ export const checkAltText = async(message: Message): Promise<void> => {
if (noDescription.size > 0) { if (noDescription.size > 0) {
const reminder = getRandomValue(reminders); const reminder = getRandomValue(reminders);
await message.reply({ 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](<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!`, 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(() => { }).catch(() => {
return null; return null;
+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.",
});
}
};