diff options
author | 2021-03-16 16:10:27 +0300 | |
---|---|---|
committer | 2021-03-16 17:08:25 +0300 | |
commit | e256043200e7c9c1d95c37903d98c9585cf34c96 (patch) | |
tree | a9f989d21b8e4ca19b96a90ae7331e5fc0030587 | |
parent | Moves Webhook & Role Helper To Discord File (diff) |
Adds Discord Direct Message Helpers
Signed-off-by: Hassan Abouelela <[email protected]>
-rw-r--r-- | backend/discord.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/backend/discord.py b/backend/discord.py index 560ab69..7c04103 100644 --- a/backend/discord.py +++ b/backend/discord.py @@ -1,5 +1,6 @@ """Various utilities for working with the Discord API.""" import asyncio +import typing from urllib import parse import httpx @@ -146,3 +147,34 @@ async def assign_role(form: Form, request_user: User) -> None: ) await make_request("PUT", url) + + +async def get_direct_message_channel(user_id: int) -> typing.Optional[int]: + """Get the ID of a direct message channel.""" + try: + response = await make_request("POST", "users/@me/channels", {"recipient_id": user_id}) + return response.json().get("id") + except httpx.HTTPStatusError as e: + # This is most likely caused by an incorrect ID + if e.response.status_code == 400: + return + + raise e + + +async def send_direct_message(form: Form, response: FormResponse, user: Request.user) -> None: + """A helper method to assign a discord role to a user.""" + channel = await get_direct_message_channel(user.payload['id']) + if not channel: + return + + message = format_message(build_ctx_variables(form, response, user), form.dm_message) + + try: + await make_request("POST", f"channels/{channel}/messages", {"content": message}) + except httpx.HTTPStatusError as e: + # This is most likely caused by closed DMs + if e.response.status_code == 403: + return + + raise e |