Refactor: Simplify captcha interaction and API logic
Removed internal submission tracking (event, timeout flag, wait method) from `CaptchaModal`, streamlining its state management. Eliminated the `original_interactor_id` check in `CaptchaView`, allowing any user to attempt solving the captcha. Cleaned up redundant API path normalization logic and removed debugging print statements in `_make_api_request` for a more concise implementation.
This commit is contained in:
parent
149a96a90d
commit
04c2dd75b9
@ -22,42 +22,20 @@ class CaptchaModal(discord.ui.Modal, title="Solve Image Captcha"):
|
||||
max_length=100
|
||||
)
|
||||
self.add_item(self.solution)
|
||||
self.submitted = asyncio.Event() # Event to track when the modal is submitted
|
||||
self.timed_out = True # Default to True, set to False when submitted
|
||||
|
||||
async def on_submit(self, interaction: discord.Interaction):
|
||||
# This method will be called when the user submits the modal
|
||||
# The actual file upload logic will be handled by the calling function
|
||||
await interaction.response.defer(ephemeral=True) # Defer the response to prevent interaction timeout
|
||||
self.timed_out = False # Mark as not timed out since it was submitted
|
||||
self.submitted.set() # Set the event to indicate submission
|
||||
|
||||
async def wait(self) -> bool:
|
||||
"""Wait for the modal to be submitted or time out.
|
||||
|
||||
Returns:
|
||||
bool: True if the modal timed out, False if it was submitted.
|
||||
"""
|
||||
try:
|
||||
# Wait for the modal to be submitted with a timeout
|
||||
await asyncio.wait_for(self.submitted.wait(), timeout=600) # 10 minute timeout
|
||||
return self.timed_out
|
||||
except asyncio.TimeoutError:
|
||||
return True # Timed out
|
||||
|
||||
|
||||
class CaptchaView(discord.ui.View):
|
||||
def __init__(self, modal: CaptchaModal, original_interactor_id: int = None):
|
||||
def __init__(self, modal: CaptchaModal):
|
||||
super().__init__(timeout=600) # 10 minutes timeout
|
||||
self.modal = modal
|
||||
self.original_interactor_id = original_interactor_id
|
||||
|
||||
@discord.ui.button(label="Solve Captcha", style=discord.ButtonStyle.primary)
|
||||
async def solve_button(self, interaction: discord.Interaction, _: discord.ui.Button):
|
||||
# Only allow the original user to solve the captcha
|
||||
if self.original_interactor_id and interaction.user.id != self.original_interactor_id:
|
||||
await interaction.response.send_message("Only the user who initiated the upload can solve this captcha.", ephemeral=True)
|
||||
return
|
||||
async def solve_button(self, interaction: discord.Interaction, button: discord.ui.Button):
|
||||
await interaction.response.send_modal(self.modal)
|
||||
|
||||
|
||||
@ -66,7 +44,7 @@ class UploadCog(commands.Cog, name="Upload"):
|
||||
|
||||
def __init__(self, bot: commands.Bot):
|
||||
self.bot = bot
|
||||
self.api_base_url = "https://upload.slipstreamm.dev/upload" # Base URL already includes /upload
|
||||
self.api_base_url = "https://upload.slipstreamm.dev/upload"
|
||||
self.session = None
|
||||
self.captcha_cache = {} # Store captcha IDs temporarily
|
||||
self.headers = {
|
||||
@ -116,6 +94,7 @@ class UploadCog(commands.Cog, name="Upload"):
|
||||
|
||||
async def _make_api_request(self, method: str, endpoint: str, **kwargs) -> Dict[str, Any]:
|
||||
"""Make a request to the API"""
|
||||
print(f"Making {method} request to {endpoint} with params: {kwargs}")
|
||||
if not self.session:
|
||||
self.session = aiohttp.ClientSession(headers=self.headers)
|
||||
|
||||
@ -123,13 +102,7 @@ class UploadCog(commands.Cog, name="Upload"):
|
||||
if not endpoint.startswith("/"):
|
||||
endpoint = f"/{endpoint}"
|
||||
|
||||
# Fix for duplicate /upload in the path
|
||||
# If endpoint starts with /api/upload, remove the /upload part as it's already in the base URL
|
||||
if endpoint.startswith("/api/upload/"):
|
||||
endpoint = endpoint.replace("/api/upload/", "/api/")
|
||||
|
||||
url = f"{self.api_base_url}{endpoint}"
|
||||
print(f"Making {method} request to: {url}")
|
||||
|
||||
try:
|
||||
if method.upper() == "GET":
|
||||
@ -232,11 +205,7 @@ class UploadCog(commands.Cog, name="Upload"):
|
||||
)
|
||||
|
||||
file_id = upload_data.get("id", "unknown")
|
||||
# Construct the file URL correctly
|
||||
# The base URL is https://upload.slipstreamm.dev/upload
|
||||
# The file URL should be https://upload.slipstreamm.dev/upload/uploads/{file_id}
|
||||
file_url = f"{self.api_base_url}/uploads/{file_id}"
|
||||
print(f"File URL: {file_url}")
|
||||
|
||||
# Format file size nicely
|
||||
file_size_bytes = upload_data.get('file_size', 0)
|
||||
|
Loading…
x
Reference in New Issue
Block a user