From 42fd49115a4cdfab78e8f144c849bac40fe97b58 Mon Sep 17 00:00:00 2001 From: "sentry-autofix[bot]" <157164994+sentry-autofix[bot]@users.noreply.github.com> Date: Sat, 19 Apr 2025 06:27:05 +0000 Subject: [PATCH] fix: prevent duplicate hub rules acceptance entries and handle errors --- src/interactions/RulesScreening.ts | 53 +++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/src/interactions/RulesScreening.ts b/src/interactions/RulesScreening.ts index ee4471220..7a3ddcf7d 100644 --- a/src/interactions/RulesScreening.ts +++ b/src/interactions/RulesScreening.ts @@ -258,24 +258,47 @@ export default class RulesScreeningInteraction { return; } - await db.hubRulesAcceptance.create({ - data: { - userId, - hubId, - }, - }); + try { + await db.hubRulesAcceptance.upsert({ + where: { + userId_hubId: { + userId, + hubId + } + }, + update: {}, // No updates - preserve the original acceptedAt timestamp + create: { + userId, + hubId + } + }); - await interaction.deleteReply(); + await interaction.deleteReply(); - const locale = await fetchUserLocale(interaction.user.id); - const embed = new InfoEmbed().setDescription( - t('rules.hubAccepted', locale, { - emoji: getEmoji('tick_icon', interaction.client), - }), - ); + const locale = await fetchUserLocale(interaction.user.id); + const embed = new InfoEmbed().setDescription( + t('rules.hubAccepted', locale, { + emoji: getEmoji('tick_icon', interaction.client), + }), + ); - await interaction.followUp({ embeds: [embed], components: [], flags: ['Ephemeral'] }); - await this.redis.del(`${RedisKeys.RulesShown}:${interaction.user.id}:${hubId}`); + if (!interaction.replied) { + await interaction.followUp({ embeds: [embed], components: [], flags: ['Ephemeral'] }); + await this.redis.del(`${RedisKeys.RulesShown}:${interaction.user.id}:${hubId}`); + } + } catch (error) { + Logger.error('Error while accepting hub rules:', error); + // Don't attempt to reply if the interaction was already replied to + if (!interaction.replied) { + const locale = await fetchUserLocale(interaction.user.id); + await interaction.followUp({ + content: t('errors.generalError', locale, { + emoji: getEmoji('x_icon', interaction.client) + }), + flags: ['Ephemeral'] + }); + } + } } @RegisterInteractionHandler('rulesScreen', 'decline')