Skip to content

Commit

Permalink
Fixes some crashes with rate-limiter on verify
Browse files Browse the repository at this point in the history
  • Loading branch information
en3sis committed Jul 23, 2024
1 parent b0de217 commit 5d065fa
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 84 deletions.
2 changes: 1 addition & 1 deletion src/controllers/plugins/chat-gpt.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const chatGTPController = async (prompt: string, apiKey: string, organiza
*/
export const sendPrompt = async ({
input,
model = 'gpt-3.5-turbo',
model = 'gpt-4o-mini',
max_tokens = 300,
temperature = 0.7,
presence_penalty = 0.5,
Expand Down
181 changes: 99 additions & 82 deletions src/controllers/plugins/verify.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,106 +16,123 @@ import { deleteFromCache, getFromCache, setToCache } from '../../libs/node-cache
import { updateMetadataGuildPlugin } from '../bot/plugins.controller'

export const verifyGuildPluginSettings = async (interaction: ChatInputCommandInteraction) => {
const role = interaction.options.get('role')?.value as string
try {
const role = interaction.options.get('role')?.value as string

const guildRole = interaction.guild.roles.cache.get(role)
const guildRole = interaction.guild.roles.cache.get(role)

await updateMetadataGuildPlugin({ role }, 'verify', interaction.guildId)
await updateMetadataGuildPlugin({ role }, 'verify', interaction.guildId)

const button = new ButtonBuilder()
.setCustomId('open_verify_modal')
.setLabel('Verify')
.setStyle(ButtonStyle.Primary)
const button = new ButtonBuilder()
.setCustomId('open_verify_modal')
.setLabel('Verify')
.setStyle(ButtonStyle.Primary)

// Send the button to a channel
await (interaction.channel as TextChannel).send({
content: `
// Send the button to a channel
await (interaction.channel as TextChannel).send({
content: `
## 🤖 Captcha Verification
Please click the button below to verify that you are human.
`,
components: [new ActionRowBuilder<ButtonBuilder>().addComponents(button)],
})

await interaction.editReply({
content: `Verify plugin settings updated. All verified users will receive the ${guildRole} role.`,
})
components: [new ActionRowBuilder<ButtonBuilder>().addComponents(button)],
})

await interaction.editReply({
content: `Verify plugin settings updated. All verified users will receive the ${guildRole} role.`,
})
} catch (error) {
console.error(error)
await interaction.editReply({
content: `Failed to update the plugin settings.`,
})
}
}

export const verifyModal = async (interaction: Interaction) => {
const emojiMap = [
{ emoji: '😊', name: 'happy' },
{ emoji: '😢', name: 'sad' },
{ emoji: '😡', name: 'angry' },
]

if (interaction.isButton()) {
if (interaction.customId !== 'open_verify_modal') return

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()
.setCustomId('verify_modal')
.setTitle('Captcha verification')
.addComponents(
new ActionRowBuilder<TextInputBuilder>().addComponents(
new TextInputBuilder()
.setCustomId('input1')
.setLabel(`Which emotion does this emoji ${userCaptchaChallenge.emoji} represent?`)
.setPlaceholder(
`Type the name of the emotion, ex: ${emojiMap.map((e) => e.name).join(', ')}`,
)
.setStyle(TextInputStyle.Short),
),
)

// Show the modal to the user
await interaction.showModal(modal)
try {
const emojiMap = [
{ emoji: '😊', name: 'happy' },
{ emoji: '😢', name: 'sad' },
{ emoji: '😡', name: 'angry' },
]

if (interaction.isButton()) {
if (interaction.customId !== 'open_verify_modal') return

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()
.setCustomId('verify_modal')
.setTitle('Captcha verification')
.addComponents(
new ActionRowBuilder<TextInputBuilder>().addComponents(
new TextInputBuilder()
.setCustomId('input1')
.setLabel(`Which emotion does this emoji ${userCaptchaChallenge.emoji} represent?`)
.setPlaceholder(
`Type the name of the emotion, ex: ${emojiMap.map((e) => e.name).join(', ')}`,
)
.setStyle(TextInputStyle.Short),
),
)

// Show the modal to the user
await interaction.showModal(modal)
}
} catch (error) {
console.error(error)
}
}

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 userCaptchaChallenge = getFromCache(`userCaptchaChallenge#${interaction.user.id}`) as {
emoji: string
name: string
}
try {
if (interaction.type === InteractionType.ModalSubmit) {
if (interaction.customId !== 'verify_modal') return
const input = interaction.fields.getTextInputValue('input1').toLocaleLowerCase()
const userCaptchaChallenge = getFromCache(`userCaptchaChallenge#${interaction.user.id}`) as {
emoji: string
name: string
}

if (input !== userCaptchaChallenge.name) {
await interaction.reply({
content: `❌ Failed to verify that you are human. Please try again.`,
ephemeral: true,
})
} else {
const member = interaction.member

// Ensure the member is indeed a GuildMember object to access the GuildMemberRoleManager
if (member instanceof GuildMember) {
const guildPluginSettings = await Hans.guildPluginSettings(interaction.guildId, 'verify')

const guildRole = interaction.guild.roles.cache.get(guildPluginSettings.metadata.role)

if (guildRole) {
await member.roles
.add(guildRole)
.then(() => interaction.reply({ content: '✅ You are now verified.', ephemeral: true }))
.catch((error) => {
// Handle errors, like missing permissions
console.error(error)
interaction.reply({ content: 'Failed to add the role.', ephemeral: true })
})
if (input !== userCaptchaChallenge.name) {
await interaction.reply({
content: `❌ Failed to verify that you are human. Please try again.`,
ephemeral: true,
})
} else {
const member = interaction.member

// Ensure the member is indeed a GuildMember object to access the GuildMemberRoleManager
if (member instanceof GuildMember) {
const guildPluginSettings = await Hans.guildPluginSettings(interaction.guildId, 'verify')

const guildRole = interaction.guild.roles.cache.get(guildPluginSettings.metadata.role)

if (guildRole) {
await member.roles
.add(guildRole)
.then(() =>
interaction.reply({ content: '✅ You are now verified.', ephemeral: true }),
)
.catch((error) => {
// Handle errors, like missing permissions
console.error(error)
interaction.reply({ content: 'Failed to add the role.', ephemeral: true })
})
} else {
// Role not found
interaction.reply({ content: 'Role not found.', ephemeral: true })
}
} else {
// Role not found
interaction.reply({ content: 'Role not found.', ephemeral: true })
// Member is not a GuildMember object
interaction.reply({ content: 'Could not resolve member details.', ephemeral: true })
}
} else {
// Member is not a GuildMember object
interaction.reply({ content: 'Could not resolve member details.', ephemeral: true })
}
}
} catch (error) {
console.error(error)
}
}
2 changes: 1 addition & 1 deletion src/realtime/presence.realtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const configsRealtime = () => {
table: 'configs',
},
async (payload) => {
if (!!process.env.ISDEV) {
if (!!process.env.ISDEV) {
console.log('Guild plugin updated:', payload.new)
}

Expand Down

0 comments on commit 5d065fa

Please sign in to comment.