generated from nhcarrigan/template
feat: add button to clear reminder #3
+24
-15
@@ -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 = (
|
||||
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 };
|
||||
|
||||
@@ -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
@@ -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 = (
|
||||
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(
|
||||
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 };
|
||||
|
||||
Reference in New Issue
Block a user