feat: Refactor captcha handling to use a view for modal submission

This commit is contained in:
Slipstream 2025-05-21 11:37:42 -06:00
parent 35546ed55b
commit 085d9be740
Signed by: slipstream
GPG Key ID: 13E498CE010AC6FD

View File

@ -11,10 +11,9 @@ from typing import Optional, Dict, Any, Union
import discord.ui
class CaptchaModal(discord.ui.Modal, title="Solve Image Captcha"):
def __init__(self, captcha_id: str, captcha_image_file: discord.File):
def __init__(self, captcha_id: str):
super().__init__()
self.captcha_id = captcha_id
self.captcha_image_file = captcha_image_file
self.solution = discord.ui.TextInput(
label="Captcha Solution",
placeholder="Enter the text from the image...",
@ -30,6 +29,16 @@ class CaptchaModal(discord.ui.Modal, title="Solve Image Captcha"):
await interaction.response.defer(ephemeral=True) # Defer the response to prevent interaction timeout
class CaptchaView(discord.ui.View):
def __init__(self, modal: CaptchaModal):
super().__init__(timeout=600) # 10 minutes timeout
self.modal = modal
@discord.ui.button(label="Solve Captcha", style=discord.ButtonStyle.primary)
async def solve_button(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.send_modal(self.modal)
class UploadCog(commands.Cog, name="Upload"):
"""Cog for interacting with the upload API"""
@ -150,9 +159,14 @@ class UploadCog(commands.Cog, name="Upload"):
# Send the captcha image and instructions
await interaction.followup.send(embed=embed, file=captcha_image_file, ephemeral=True)
# Create and send the modal
modal = CaptchaModal(captcha_id=captcha_id, captcha_image_file=captcha_image_file)
await interaction.followup.send_modal(modal)
# Create the modal instance
modal = CaptchaModal(captcha_id=captcha_id)
# Create a view with a button that sends the modal
view = CaptchaView(modal=modal)
# Send the captcha image and instructions with the view
await interaction.followup.send(embed=embed, file=captcha_image_file, view=view, ephemeral=True)
# Wait for the modal submission
timed_out = await modal.wait()