generated from nhcarrigan/template
feat: add button to clear reminder #3
@@ -64,3 +64,21 @@ export const blocks = [
|
||||
setURL("https://forum.nhcarrigan.com"),
|
||||
),
|
||||
];
|
||||
|
||||
// config for the acknowledgment button
|
||||
|
||||
export const createAckButton = (userId: string) => {
|
||||
const button = new ButtonBuilder()
|
||||
.setCustomId(`ack-${userId}`)
|
||||
.setLabel("Got it!")
|
||||
.setStyle(ButtonStyle.Secondary)
|
||||
.setEmoji("✅");
|
||||
|
|
||||
|
||||
|
||||
const actionRow = new ActionRowBuilder<ButtonBuilder>()
|
||||
.addComponents(button);
|
||||
|
||||
return [actionRow];
|
||||
};
|
||||
|
||||
export const replyForUnauthorized = "❌ This button is only for the person who received the reminder.";
|
||||
+15
-5
@@ -9,6 +9,7 @@ import { Client, GatewayIntentBits, Events, MessageFlags } from "discord.js";
|
||||
import { blocks } from "./config/blocks.js";
|
||||
import { checkAltText } from "./modules/checkAltText.js";
|
||||
import { instantiateServer } from "./server/serve.js";
|
||||
import { handleAckButton } from "./utils/buttonAck.js";
|
||||
import { logger } from "./utils/logger.js";
|
||||
|
||||
const client = new Client({
|
||||
@@ -35,13 +36,22 @@ client.on(Events.MessageCreate, (message) => {
|
||||
|
||||
client.on(Events.InteractionCreate, (interaction) => {
|
||||
void analytics.logGatewayEvent(Events.InteractionCreate, { ...interaction });
|
||||
if (!interaction.isChatInputCommand()) {
|
||||
if (!interaction.isChatInputCommand() && !interaction.isButton()) {
|
||||
return;
|
||||
}
|
||||
void interaction.reply({
|
||||
components: blocks,
|
||||
flags: MessageFlags.IsComponentsV2,
|
||||
});
|
||||
// Existing logic for slash commands
|
||||
if (interaction.isChatInputCommand()) {
|
||||
void interaction.reply({
|
||||
components: blocks,
|
||||
flags: MessageFlags.IsComponentsV2,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle button clicks
|
||||
if (interaction.isButton()) {
|
||||
void handleAckButton(interaction);
|
||||
}
|
||||
});
|
||||
|
||||
await client.login(process.env.BOT_TOKEN);
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
import { reminders } from "../config/reminders.js";
|
||||
import { createAckButton } from "../utils/buttonAck.js";
|
||||
import { getRandomValue } from "../utils/getRandomValue.js";
|
||||
import { logger } from "../utils/logger.js";
|
||||
import type { Message } from "discord.js";
|
||||
@@ -33,7 +34,8 @@ export const checkAltText = async(message: Message): Promise<void> => {
|
||||
if (noDescription.size > 0) {
|
||||
const reminder = getRandomValue(reminders);
|
||||
await message.reply({
|
||||
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!`,
|
||||
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
|
||||
}).catch(() => {
|
||||
return null;
|
||||
});
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* @copyright NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @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";
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
|
naomi
commented
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.
|
||||
|
||||
export const createAckButton = (userId: string) => {
|
||||
return createAckButtonConfig(userId);
|
||||
};
|
||||
|
||||
// function to handle button interaction
|
||||
export const handleAckButton = async (interaction: ButtonInteraction) => {
|
||||
if (!interaction.customId.startsWith("ack-")) {
|
||||
return false; // Not our button
|
||||
}
|
||||
|
||||
|
naomi
commented
This should get its own module file as well, in This should get its own module file as well, in `src/modules/handleAckButton.ts`.
|
||||
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;
|
||||
}
|
||||
|
||||
await interaction.message.delete();
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user
This function should be in its own
src/modules/createAckButton.tsfile.