Skip to content

Commit

Permalink
better logs
Browse files Browse the repository at this point in the history
  • Loading branch information
OscarVsp committed Apr 26, 2024
1 parent 08af6f3 commit 4e52af6
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions classes/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ async def load(cls, bot: Bot) -> bool:
cls._users_ws = cls._sheet.worksheet("users")
cls._guilds_ws = cls._sheet.worksheet("guilds")

logging.info("[Database] Spreadsheed loaded")
logging.info("[Database:load] Spreadsheed loaded")
except (ValueError, gspread.exceptions.SpreadsheetNotFound, gspread.exceptions.WorksheetNotFound) as err:
await bot.send_error_log(bot.tracebackEx(err))
return

logging.info("[Database] Loading data...")
logging.info("[Database:load] Loading data...")

# Load guilds
cls.ulb_guilds = {}
Expand All @@ -146,28 +146,30 @@ async def load(cls, bot: Bot) -> bool:
if role:
cls.ulb_guilds.setdefault(guild, UlbGuild(role, rename))
logging.trace(
f"[Database] Role {role.name}:{role.id} loaded from guild {guild.name}:{guild.id} with {rename=}"
f"[Database:load] Role {role.name}:{role.id} loaded from guild {guild.name}:{guild.id} with {rename=}"
)
else:
logging.warning(
f"[Database] Not able to find role from id={guild_data.get('role_id', int)} in guild {guild.name}:{guild.id}."
f"[Database:load] Not able to find role from id={guild_data.get('role_id', int)} in guild {guild.name}:{guild.id}."
)
else:
logging.warning(f"[GoogleSheet] Not able to find guild from id={guild_data.get('guild_id', int)}.")
logging.info(f"[Database] Found {len(cls.ulb_guilds)} guilds.")
logging.warning(f"[Database:load] Not able to find guild from id={guild_data.get('guild_id', int)}.")
logging.info(f"[Database:load] Found {len(cls.ulb_guilds)} guilds.")

# Load users
cls.ulb_users = {}
not_found_counter = 0
for user_data in cls._users_ws.get_all_records():
user = bot.get_user(user_data.get("user_id", int))
if user:
cls.ulb_users.setdefault(user, UlbUser(user_data.get("name", str), user_data.get("email", str)))
logging.trace(
f"[Database] User {user.name}:{user.id} loaded with name={user_data.get('name')} and email={user_data.get('email')}"
f"[Database:load] User {user.name}:{user.id} loaded with name={user_data.get('name')} and email={user_data.get('email')}"
)
else:
not_found_counter += 1
logging.warning(f"[Database] Not able to find user from id={user_data.get('user_id',int)}.")
logging.info(f"[Database] Found {len(cls.ulb_users)} users.")
logging.info(f"[Database:load] {len(cls.ulb_users)} users found, {not_found_counter} not found.")

cls._loaded = True

Expand All @@ -187,15 +189,15 @@ async def _set_user_task(cls, user_id: int, name: str, email: str):
user_cell: gspread.cell.Cell = cls._users_ws.find(str(user_id), in_column=1)
await asyncio.sleep(0.1)
if user_cell:
logging.debug(f"[Database] {user_id=} found")
logging.trace(f"[Database:_set_user_task] {user_id=} found")
cls._users_ws.update_cell(user_cell.row, 2, name)
await asyncio.sleep(0.1)
cls._users_ws.update_cell(user_cell.row, 3, email)
logging.info(f"[Database] {user_id=} updated with {name=} and {email=}")
logging.info(f"[Database:_set_user_task] {user_id=} updated with {name=} and {email=}")
else:
logging.debug(f"[Database] {user_id=} not found")
logging.trace(f"[Database:_set_user_task] {user_id=} not found")
cls._users_ws.append_row(values=[str(user_id), name, email])
logging.info(f"[Database] {user_id=} added with {name=} and {email=}")
logging.info(f"[Database:_set_user_task] {user_id=} added with {name=} and {email=}")

@classmethod
def set_user(cls, user: disnake.User, name: str, email: str):
Expand Down Expand Up @@ -228,10 +230,10 @@ async def _delete_user_task(cls, user_id: int):
"""
user_cell: gspread.cell.Cell = cls._users_ws.find(str(user_id), in_column=1)
await asyncio.sleep(0.1)
logging.trace(f"[Database] {user_id=} found")
logging.trace(f"[Database:_delete_user_task] {user_id=} found")
cls._users_ws.delete_row(user_cell.row)
await asyncio.sleep(0.1)
logging.info(f"[Database] {user_id=} deleted.")
logging.info(f"[Database:_delete_user_task] {user_id=} deleted.")

@classmethod
def delete_user(cls, user: disnake.User):
Expand Down Expand Up @@ -265,14 +267,14 @@ async def _set_guild_task(cls, guild_id: int, role_id: int, rename: bool):
guild_cell: gspread.cell.Cell = cls._guilds_ws.find(str(guild_id), in_column=1)
await asyncio.sleep(0.1)
if guild_cell:
logging.debug(f"[Database] {guild_id=} found.")
logging.trace(f"[Database:_set_guild_task] {guild_id=} found.")
cls._guilds_ws.update_cell(guild_cell.row, 2, str(role_id))
cls._guilds_ws.update_cell(guild_cell.row, 3, rename)
logging.info(f"[Database] {guild_id=} update with {role_id=} and {rename=}.")
logging.info(f"[Database:_set_guild_task] {guild_id=} update with {role_id=} and {rename=}.")
else:
logging.debug(f"[Database] {guild_id=} not found.")
logging.trace(f"[Database:_set_guild_task] {guild_id=} not found.")
cls._guilds_ws.append_row(values=[str(guild_id), str(role_id), rename])
logging.info(f"[Database] {guild_id=} added with {role_id=} and {rename=}.")
logging.info(f"[Database:_set_guild_task] {guild_id=} added with {role_id=} and {rename=}.")

@classmethod
def set_guild(cls, guild: disnake.Guild, role: disnake.Role, rename: bool):
Expand Down Expand Up @@ -303,10 +305,10 @@ async def _delete_guild_task(cls, guild_id: int):
"""
guild_cell: gspread.cell.Cell = cls._guilds_ws.find(str(guild_id), in_column=1)
await asyncio.sleep(0.1)
logging.trace(f"[Database] {guild_id=} found")
logging.trace(f"[Database:_delete_guild_task] {guild_id=} found")
cls._guilds_ws.delete_row(guild_cell.row)
await asyncio.sleep(0.1)
logging.info(f"[Database] {guild_id=} deleted.")
logging.info(f"[Database:_delete_guild_task] {guild_id=} deleted.")

@classmethod
def delete_guild(cls, guild: disnake.Guild):
Expand Down

0 comments on commit 4e52af6

Please sign in to comment.