From c3c14c19df682394a22159ff633b5724ace442d0 Mon Sep 17 00:00:00 2001 From: en3sis <940170+en3sis@users.noreply.github.com> Date: Thu, 8 Feb 2024 13:33:51 +0100 Subject: [PATCH] fix: issue with captcha challenge per user --- src/controllers/plugins/verify.controller.ts | 16 ++++++++++------ src/libs/node-cache.ts | 5 +++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/controllers/plugins/verify.controller.ts b/src/controllers/plugins/verify.controller.ts index 3d8640e..cf34bba 100644 --- a/src/controllers/plugins/verify.controller.ts +++ b/src/controllers/plugins/verify.controller.ts @@ -12,7 +12,7 @@ import { TextInputStyle, } from 'discord.js' import { Hans } from '../..' -import { getFromCache, setToCache } from '../../libs/node-cache' +import { deleteFromCache, getFromCache, setToCache } from '../../libs/node-cache' import { updateMetadataGuildPlugin } from '../bot/plugins.controller' export const verifyGuildPluginSettings = async (interaction: ChatInputCommandInteraction) => { @@ -51,8 +51,9 @@ export const verifyModal = async (interaction: Interaction) => { if (interaction.isButton()) { if (interaction.customId !== 'open_verify_modal') return - const randomEmoji = emojiMap[Math.floor(Math.random() * emojiMap.length)] - setToCache('randomEmoji', randomEmoji) + const userCaptchaChallenge = emojiMap[Math.floor(Math.random() * emojiMap.length)] + deleteFromCache(`userCaptchaChallenge#${interaction.user.id}`) + setToCache(`userCaptchaChallenge#${interaction.user.id}`, userCaptchaChallenge, 1) // Define a modal const modal = new ModalBuilder() @@ -62,7 +63,7 @@ export const verifyModal = async (interaction: Interaction) => { new ActionRowBuilder().addComponents( new TextInputBuilder() .setCustomId('input1') - .setLabel(`Which emotion does this emoji ${randomEmoji.emoji} represent?`) + .setLabel(`Which emotion does this emoji ${userCaptchaChallenge.emoji} represent?`) .setPlaceholder('Type the name of the emotion.') .setStyle(TextInputStyle.Short), ), @@ -77,9 +78,12 @@ export const verifyModalSubmit = async (interaction: Interaction) => { if (interaction.type === InteractionType.ModalSubmit) { if (interaction.customId !== 'verify_modal') return const input = interaction.fields.getTextInputValue('input1').toLocaleLowerCase() - const randomEmoji = getFromCache('randomEmoji') as { emoji: string; name: string } + const userCaptchaChallenge = getFromCache(`userCaptchaChallenge#${interaction.user.id}`) as { + emoji: string + name: string + } - if (input !== randomEmoji.name) { + if (input !== userCaptchaChallenge.name) { await interaction.reply({ content: `❌ Failed to verify that you are human. Please try again.`, ephemeral: true, diff --git a/src/libs/node-cache.ts b/src/libs/node-cache.ts index 9053688..b0367e9 100644 --- a/src/libs/node-cache.ts +++ b/src/libs/node-cache.ts @@ -19,3 +19,8 @@ export const getFromCache = (key: string): string | object | null => { const value = CACHE.get(key) return value ? JSON.parse(value as string) : null } + +// Creates a function for deleting an item from the cache +export const deleteFromCache = (key: string) => { + CACHE.del(key) +}