From 5b40473b5e485ad92be527dbe1f9029d1b9e5ab4 Mon Sep 17 00:00:00 2001 From: a Date: Sat, 12 Jul 2025 22:12:31 -0500 Subject: [PATCH] noot --- ts/src/activities/discord.ts | 68 ++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 11 deletions(-) diff --git a/ts/src/activities/discord.ts b/ts/src/activities/discord.ts index 753b121..e914827 100644 --- a/ts/src/activities/discord.ts +++ b/ts/src/activities/discord.ts @@ -3,6 +3,17 @@ import { c } from '#/di' import type { InteractionRef } from '#/discord' import { InteractionResponseTypes } from '@discordeno/types' import { Bot } from '#/discord/bot' +import { ApplicationError } from '@temporalio/activity' +import { logger } from '#/logger' + +const log = logger.child({ component: 'discord-activity' }) + +// Custom error class for non-retryable Discord errors +export class DiscordInteractionExpiredError extends ApplicationError { + constructor(message: string) { + super(message, { nonRetryable: true }) + } +} // from https://github.com/discordeno/discordeno/blob/21.0.0/packages/bot/src/transformers/interaction.ts#L33 export const reply_to_interaction = async (props: { @@ -20,16 +31,51 @@ export const reply_to_interaction = async (props: { data.flags = MessageFlags.Ephemeral } - switch (type) { - case InteractionResponseTypes.UpdateMessage: - if(ref.acknowledged) { - return await bot.helpers.editOriginalInteractionResponse(ref.token, data) - } - default: - if (ref.acknowledged) { - const followUp = await bot.helpers.sendFollowupMessage(ref.token, data) - return followUp - } - return await bot.helpers.sendInteractionResponse(ref.id, ref.token, { type, data }, { withResponse: options?.withResponse }) + try { + switch (type) { + case InteractionResponseTypes.UpdateMessage: + if(ref.acknowledged) { + return await bot.helpers.editOriginalInteractionResponse(ref.token, data) + } + default: + if (ref.acknowledged) { + const followUp = await bot.helpers.sendFollowupMessage(ref.token, data) + return followUp + } + return await bot.helpers.sendInteractionResponse(ref.id, ref.token, { type, data }, { withResponse: options?.withResponse }) + } + } catch (error: any) { + // Check if it's a Discord API error + if (error.status === 404 || error.code === 10062) { + // 10062 is Discord's "Unknown interaction" error code + log.warn( + { + interactionId: ref.id, + error: error.message, + code: error.code, + status: error.status + }, + 'Discord interaction expired (404) - not retrying' + ) + + // Throw non-retryable error to prevent Temporal from retrying + throw new DiscordInteractionExpiredError( + `Discord interaction ${ref.id} has expired and cannot be responded to` + ) + } + + // Log other errors and re-throw them (these will be retried by Temporal) + log.error( + { + interactionId: ref.id, + error: error.message, + stack: error.stack, + code: error.code, + status: error.status + }, + 'Failed to reply to Discord interaction' + ) + + throw error } }