From 4703e44dff9463612fb124a0adb9be46b44bfe33 Mon Sep 17 00:00:00 2001 From: Jonathan Styles Date: Thu, 1 Aug 2024 13:40:34 -0400 Subject: [PATCH] Bugfix discord role adding --- app/util/approve.py | 15 +++++++++++---- app/util/discord.py | 11 ++++++++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/app/util/approve.py b/app/util/approve.py index cdc24e9..12215bf 100644 --- a/app/util/approve.py +++ b/app/util/approve.py @@ -105,7 +105,10 @@ def approve_member(member_id: uuid.UUID): creds = {"username": None, "password": None} # Assign the Dues-Paying Member role - Discord.assign_role(discord_id, Settings().discord.member_role) + try: + Discord.assign_role(discord_id, Settings().discord.member_role) + except: + logger.exception("Failed to assign role") # Send Discord message saying they are a member welcome_msg = f"""Hello {user_data.first_name}, and welcome to Hack@UCF! @@ -126,9 +129,13 @@ def approve_member(member_id: uuid.UUID): Happy Hacking, - Hack@UCF Bot """ - - Discord.send_message(discord_id, welcome_msg) - Email.send_email("Welcome to Hack@UCF", welcome_msg, user_data.email) + try: + Discord.send_message(discord_id, welcome_msg) + Email.send_email( + "Welcome to Hack@UCF", welcome_msg, user_data.email + ) + except Exception: + logger.exception("Failed to send welcome message") # Set member as a "full" member. user_data.is_full_member = True session.add(user_data) diff --git a/app/util/discord.py b/app/util/discord.py index ec2b66e..1a632c6 100644 --- a/app/util/discord.py +++ b/app/util/discord.py @@ -1,9 +1,12 @@ import json +import logging import requests from app.util.settings import Settings +logger = logging.getLogger(__name__) + if Settings().discord.enable: headers = { "Authorization": f"Bot {Settings().discord.bot_token.get_secret_value()}", @@ -23,13 +26,18 @@ def __init__(self): def assign_role(discord_id, role_id): if not Settings().discord.enable: return + logger.info( + f"Assigning role {role_id} to {discord_id}, on guild {Settings().discord.guild_id}" + ) discord_id = str(discord_id) req = requests.put( f"https://discord.com/api/guilds/{Settings().discord.guild_id}/members/{discord_id}/roles/{role_id}", headers=headers, ) - + if req.status_code >= 400: + raise Exception(f"Discord api error: {req.json()}") + logger.exception(f"Failed to assign role {role_id} to {discord_id}") return req.status_code < 400 def get_dm_channel_id(discord_id): @@ -66,6 +74,7 @@ def join_hack_server(self, discord_id, token): if not Settings().discord.enable: return # Make user join the Hack@UCF Discord, if it's their first rodeo. + logger.info(f"Joining {discord_id} to Hack@UCF Discord") headers = { "Authorization": f"Bot {Settings().discord.bot_token.get_secret_value()}", "Content-Type": "application/json",