Skip to content

Commit

Permalink
fix: issue with captcha challenge per user
Browse files Browse the repository at this point in the history
  • Loading branch information
en3sis committed Feb 8, 2024
1 parent bcf8fb8 commit c3c14c1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/controllers/plugins/verify.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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()
Expand All @@ -62,7 +63,7 @@ export const verifyModal = async (interaction: Interaction) => {
new ActionRowBuilder<TextInputBuilder>().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),
),
Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions src/libs/node-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit c3c14c1

Please sign in to comment.