generated from nhcarrigan/template
feat: add button to clear reminder #3
+6
-3
@@ -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";
|
||||||
|
|
||||||
@@ -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);
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user