Skip to content

Commit

Permalink
Merge pull request #187 from MiscGuild/bug_fix
Browse files Browse the repository at this point in the history
Bug fix
  • Loading branch information
Amxgh authored May 25, 2024
2 parents f3c7420 + 94cc2fd commit d5994e4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/func/String.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ async def dnklremove(self):
else:
message_id, _, username = row
# Delete row
await delete_dnkl(username)
await delete_dnkl(uuid)

# Delete DNKL message
try:
Expand Down
4 changes: 2 additions & 2 deletions src/func/Union.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ async def sync(self, ctx, name: str, tag: str = None, is_fs=False):
# User is a member
if guild_name == guild_handle:
roles_to_add.append(bot.member_role)
roles_to_remove.extend([bot.guest, bot.ally])
roles_to_remove.extend([bot.guest, bot.ally, bot.new_member_role, bot.processing])

# Add active role if eligible
for member in guild_data["members"]:
Expand All @@ -129,7 +129,7 @@ async def sync(self, ctx, name: str, tag: str = None, is_fs=False):
elif guild_name in allies:
if not can_tag:
new_nick += f" {await get_gtag(guild_name)}"
roles_to_remove.extend([bot.new_member_role, bot.member_role])
roles_to_remove.extend([bot.new_member_role, bot.member_role, bot.new_member_role, bot.processing])
roles_to_add.extend([bot.guest, bot.ally])

# User is a guest
Expand Down
10 changes: 7 additions & 3 deletions src/utils/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ async def connect_db():

# DNKL table:
await bot.db.execute("""CREATE TABLE IF NOT EXISTS dnkl (
uuid text PRIMARY KEY NOT NULL,
message_id integer NOT NULL,
uuid text NOT NULL,
username text NOT NULL)""")

# Giveaways table
Expand Down Expand Up @@ -76,8 +76,8 @@ async def update_dnkl(message_id: int, uuid: str):
await bot.db.commit()


async def delete_dnkl(username: str):
await bot.db.execute("DELETE FROM dnkl WHERE username = (?)", (username,))
async def delete_dnkl(uuid: str):
await bot.db.execute("DELETE FROM dnkl WHERE uuid = (?)", (uuid,))
await bot.db.commit()


Expand Down Expand Up @@ -153,3 +153,7 @@ async def update_member(discord_id: int, uuid: str, username: str):
async def check_uuid_in_db(uuid: str):
discord_id = (await select_one("SELECT discord_id from members WHERE uuid=(?)", (uuid,)))
return discord_id[0] if discord_id else 0

async def update_db_username(uuid: str, username: str):
await bot.db.execute("UPDATE members SET username = ? WHERE uuid = ?", (username, uuid,))
await bot.db.commit()
29 changes: 19 additions & 10 deletions src/utils/request_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@
import discord

from src.utils.consts import config, error_channel_id
from src.utils.db_utils import get_db_username_from_uuid, update_db_username


async def get_hyapi_key():
return random.choice(config["api_keys"])


async def update_usernames(uuid: str, username: str):
db_username = await get_db_username_from_uuid(uuid)
if db_username is None:
return
elif db_username != username:
await update_db_username(uuid, username)


# Base JSON-getter for all JSON based requests. Catches Invalid API Key errors
async def get_json_response(url: str):
async with aiohttp.ClientSession() as session:
Expand All @@ -39,12 +48,12 @@ async def get_mojang_profile(name: str):

# If player and request is valid
if resp and ("errorMessage" not in resp) and ("name" in resp):
await update_usernames(uuid=resp["id"], username=resp["name"])
return resp["name"], resp["id"]

api_key = await get_hyapi_key()
resp = await get_json_response(f"https://api.hypixel.net/player?key={api_key}&name={name}")
if resp and 'player' in resp and resp['player']:
return resp['player']['displayname'], resp['player']['uuid']
resp = await get_hypixel_player(name=name)
if resp:
return resp['displayname'], resp['uuid']

# Player does not exist
return None, None
Expand All @@ -61,6 +70,7 @@ async def get_hypixel_player(name: str = None, uuid: str = None):
if "player" not in resp or not resp["player"]:
return None

await update_usernames(uuid=resp["player"]["uuid"], username=resp["player"]["displayname"])
# Player exists
return resp["player"]

Expand All @@ -75,13 +85,12 @@ async def get_name_by_uuid(uuid: str):
continue
if "name" not in resp:
continue
await update_usernames(uuid=uuid, username=resp["name"])
return resp["name"]
api_key = await get_hyapi_key()
# If the Mojang API fails to return a name, the bot checks using the hypixel API
resp = await get_json_response(f"https://api.hypixel.net/player?key={api_key}&uuid={uuid}")
if "player" not in resp:
return None
return resp['player']['displayname']
resp = await get_hypixel_player(uuid=uuid)
if resp:
await update_usernames(uuid=uuid, username=resp['displayname'])
return resp['displayname']


def session_get_name_by_uuid(session, uuid):
Expand Down

0 comments on commit d5994e4

Please sign in to comment.