Skip to content

Commit

Permalink
Bugfix discord role adding
Browse files Browse the repository at this point in the history
  • Loading branch information
jontyms committed Aug 1, 2024
1 parent 965e8df commit 4703e44
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
15 changes: 11 additions & 4 deletions app/util/approve.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion app/util/discord.py
Original file line number Diff line number Diff line change
@@ -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()}",
Expand All @@ -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):
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 4703e44

Please sign in to comment.