From 3abbff13d5ff171c74ff89336f9ed96db808de29 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Wed, 12 Mar 2025 09:50:58 -0400 Subject: [PATCH 1/8] Use walrus operator when assigning and checking for None. --- cogs/commands/submission.py | 21 +++++++++------------ cogs/guild_listener/__init__.py | 3 +-- cogs/submission/verification_view.py | 4 +--- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/cogs/commands/submission.py b/cogs/commands/submission.py index fab15dc..caa011d 100644 --- a/cogs/commands/submission.py +++ b/cogs/commands/submission.py @@ -64,10 +64,7 @@ class SubmissionCog(commands.Cog): .where(col(Category.group_id) == int(group_id)) .where(col(Category.id) == int(category_id)) ) - - category: Category | None = session.scalar(stmt) - - if category is None: + if (category := session.scalar(stmt)) is None: await interaction.response.send_message("Category not found.", ephemeral=True) return @@ -79,10 +76,13 @@ class SubmissionCog(commands.Cog): return # check with regex if score is in correct format - verified_score = await self.verify_score(interaction, score, is_time_record=category.is_time_record) - # check if verify_score was successful. It replies with corrections required itself. - if verified_score is None: + if ( + verified_score := await self.verify_score(interaction, score, is_time_record=category.is_time_record) + ) is None: + # This response message will be filled out better once command is split up to + # know which type it expects... + await interaction.response.send_message("Format was incorrect.", ephemeral=True) return # Create submission verification request or submit score if verification not needed. @@ -92,16 +92,13 @@ class SubmissionCog(commands.Cog): return stmt = select(col(Guild.verification_channel)).where(col(Guild.id) == interaction.guild_id) - verification_channel_id: int | None = session.scalar(stmt) - if verification_channel_id is None: + if (verification_channel_id := session.scalar(stmt)) is None: error_response = "No verification channel was registered. Contact server admins." await interaction.response.send_message(error_response, ephemeral=True) return - channel = interaction.guild.get_channel(verification_channel_id) - - if channel is None: + if (channel := interaction.guild.get_channel(verification_channel_id)) is None: error_response = "Registered Verification channel does not exist. Contact server admins." await interaction.response.send_message(error_response, ephemeral=True) return diff --git a/cogs/guild_listener/__init__.py b/cogs/guild_listener/__init__.py index 9e6fc61..2707f6b 100644 --- a/cogs/guild_listener/__init__.py +++ b/cogs/guild_listener/__init__.py @@ -26,10 +26,9 @@ class GuildListener(commands.Cog, name="Guild Listener"): """ with Session(engine) as session: stmt = select(Guild).where(col(Guild.id == guild.id)) - guild_data = session.scalar(stmt) # just in case check if the guild already exists - if guild_data is None: + if (guild_data := session.scalar(stmt)) is None: new_guild = Guild(id=guild.id, name=guild.name) session.add(new_guild) else: diff --git a/cogs/submission/verification_view.py b/cogs/submission/verification_view.py index 9a41e55..6a71e93 100644 --- a/cogs/submission/verification_view.py +++ b/cogs/submission/verification_view.py @@ -39,9 +39,7 @@ class VerificationView(nextcord.ui.View): .where(col(Submission.message_id) == interaction.message.id) ) - submission = session.scalar(stmt) - - if submission is None: + if (submission := session.scalar(stmt)) is None: await interaction.response.send_message("Could not find submission data in database.", ephemeral=True) await interaction.message.delete() return From b28938f66af1991ece42c2e113137b27d18a1412 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Wed, 12 Mar 2025 21:27:12 -0400 Subject: [PATCH 2/8] Had col around the wrong variables. --- cogs/loops/guild_data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/loops/guild_data.py b/cogs/loops/guild_data.py index 8ed6c83..f686d49 100644 --- a/cogs/loops/guild_data.py +++ b/cogs/loops/guild_data.py @@ -31,7 +31,7 @@ class GuildData(commands.Cog, name="Guild Data"): with Session(engine) as session: for guild in self.bot.guilds: # update given guild name for guild id - stmt = select(Guild).where(col(Guild.id == guild.id)) + stmt = select(Guild).where(col(Guild.id) == guild.id) guild_data = session.scalar(stmt) # just in case checking if the guild doesn't exist. From 68592a7df8b3360ce8ff9972cdc7759008114a10 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Wed, 12 Mar 2025 21:27:29 -0400 Subject: [PATCH 3/8] Typo on col usage. --- utilities/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utilities/utils.py b/utilities/utils.py index 3ad97cc..dc24976 100644 --- a/utilities/utils.py +++ b/utilities/utils.py @@ -64,7 +64,7 @@ def autofill_groups(guild_id: int, group_begin: str) -> "dict[str, str]": """ with Session(engine) as session: # Select all groups in guild. - stmt = select(CategoryGroup).wherecol((CategoryGroup.guild_id) == guild_id) + stmt = select(CategoryGroup).where(col(CategoryGroup.guild_id) == guild_id) # dict has to have key values of same type for discord. group_dict: dict[str, str] = {group.name: str(group.id) for group in session.scalars(stmt)} From e1b761e84867931856868c07883e1fcfb0e5ab34 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Wed, 12 Mar 2025 21:29:01 -0400 Subject: [PATCH 4/8] Use correct variable for guild name. --- cogs/loops/guild_data.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/loops/guild_data.py b/cogs/loops/guild_data.py index f686d49..3ce385f 100644 --- a/cogs/loops/guild_data.py +++ b/cogs/loops/guild_data.py @@ -39,6 +39,6 @@ class GuildData(commands.Cog, name="Guild Data"): new_guild = Guild(id=guild.id, name=guild.name) session.add(new_guild) else: - guild_data.GuildName = guild.name + guild_data.name = guild.name session.commit() From 170ce1cbfb28964d0745cb36181c6419954ed38c Mon Sep 17 00:00:00 2001 From: imcompiling Date: Wed, 12 Mar 2025 21:30:56 -0400 Subject: [PATCH 5/8] Use correct message id reference. --- utilities/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utilities/utils.py b/utilities/utils.py index dc24976..6cd3fb2 100644 --- a/utilities/utils.py +++ b/utilities/utils.py @@ -347,7 +347,7 @@ async def send_highscore_message(session: Session, guild: nextcord.Guild, group: if group.message_id is None: # message doesn't exist. message: nextcord.Message = await channel.send(highscore_string) - group.MessageId = message.id + group.id = message.id session.commit() else: message: nextcord.Message = await channel.fetch_message(group.message_id) From 74ce22dd26f8406a723e403a14de90f9d957fab8 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Thu, 13 Mar 2025 00:39:47 -0400 Subject: [PATCH 6/8] Was saving the message id to the wrong spot. --- utilities/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utilities/utils.py b/utilities/utils.py index 6cd3fb2..6b703b1 100644 --- a/utilities/utils.py +++ b/utilities/utils.py @@ -347,7 +347,7 @@ async def send_highscore_message(session: Session, guild: nextcord.Guild, group: if group.message_id is None: # message doesn't exist. message: nextcord.Message = await channel.send(highscore_string) - group.id = message.id + group.message_id = message.id session.commit() else: message: nextcord.Message = await channel.fetch_message(group.message_id) From e5d90a71d9f5d321fe25275be79bfdf6d2564d0f Mon Sep 17 00:00:00 2001 From: imcompiling Date: Fri, 14 Mar 2025 16:02:18 -0400 Subject: [PATCH 7/8] Correct parameter usage. --- cogs/commands/set.py | 2 +- utilities/utils.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cogs/commands/set.py b/cogs/commands/set.py index d2bd194..7b4bdd5 100644 --- a/cogs/commands/set.py +++ b/cogs/commands/set.py @@ -179,7 +179,7 @@ class SetCog(commands.Cog, name="Set command."): with Session(engine) as session: stmt = select(Guild).where(col(Guild.id) == interaction.guild_id) guild = session.scalar(stmt) - guild.VerificationChannel = channel_id + guild.verification_channel = channel_id session.commit() await interaction.send(f"Registered <#{channel_id}>.", ephemeral=True) diff --git a/utilities/utils.py b/utilities/utils.py index 6b703b1..8681a6b 100644 --- a/utilities/utils.py +++ b/utilities/utils.py @@ -390,7 +390,7 @@ async def submit_score( existing_score = session.scalar(stmt) if existing_score: - existing_score.Score = score_f + existing_score.score = score_f else: score_obj = Score(username=username, score=score_f, category_id=category_id) session.add(score_obj) From c25d354fc09f8d5099bcd450789049b64450fbd5 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Fri, 14 Mar 2025 16:09:52 -0400 Subject: [PATCH 8/8] Correct some parameter usage, handle errors if searches return nothing. --- .vscode/settings.json | 4 ++-- cogs/commands/set.py | 28 ++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 47e3829..1dc3b48 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,8 +10,8 @@ "ondelete" ], "python.analysis.diagnosticSeverityOverrides": { - "reportOptionalMemberAccess": false, + "reportOptionalMemberAccess": "warning", "reportArgumentType": "information", "reportAttributeAccessIssue": "warning" } -} +} \ No newline at end of file diff --git a/cogs/commands/set.py b/cogs/commands/set.py index 7b4bdd5..47e8d26 100644 --- a/cogs/commands/set.py +++ b/cogs/commands/set.py @@ -52,8 +52,10 @@ class SetCog(commands.Cog, name="Set command."): .where(col(CategoryGroup.guild_id) == interaction.guild_id) .where(col(CategoryGroup.id) == int(group_id)) ) - group = session.scalar(stmt) - group.ChannelId = channel_id + if (group := session.scalar(stmt)) is None: + await interaction.response.send_message("Group or Category was not found.", ephemeral=True) + return + group.channel_id = channel_id session.commit() await interaction.send(f"Registered <#{channel_id}>.", ephemeral=True) @@ -88,8 +90,11 @@ class SetCog(commands.Cog, name="Set command."): .where(col(CategoryGroup.guild_id) == interaction.guild_id) .where(col(CategoryGroup.id) == int(group_id)) ) - group = session.scalar(stmt) - group.PadLength = length + if (group := session.scalar(stmt)) is None: + await interaction.response.send_message("Group or Category was not found.", ephemeral=True) + return + + group.pad_length = length session.commit() await interaction.response.send_message( f"Category pad length set to {length} for {group.name}", ephemeral=True @@ -120,7 +125,9 @@ class SetCog(commands.Cog, name="Set command."): with Session(engine) as session: stmt = select(Guild).where(col(Guild.id) == interaction.guild_id) - guild = session.scalar(stmt) + if (guild := session.scalar(stmt)) is None: + await interaction.response.send_message("Guild was not found.", ephemeral=True) + return guild.submission_channel = channel_id session.commit() await interaction.send(f"Registered <#{channel_id}>.", ephemeral=True) @@ -150,8 +157,11 @@ class SetCog(commands.Cog, name="Set command."): .where(col(CategoryGroup.guild_id) == interaction.guild_id) .where(col(CategoryGroup.id) == int(group_id)) ) - group = session.scalar(stmt) - group.UsernameLength = length + if (group := session.scalar(stmt)) is None: + await interaction.response.send_message("Group or Category was not found.", ephemeral=True) + return + + group.username_length = length session.commit() await interaction.response.send_message(f"Username length set to {length}.", ephemeral=True) @@ -178,7 +188,9 @@ class SetCog(commands.Cog, name="Set command."): with Session(engine) as session: stmt = select(Guild).where(col(Guild.id) == interaction.guild_id) - guild = session.scalar(stmt) + if (guild := session.scalar(stmt)) is None: + await interaction.response.send_message("Guild was not found.", ephemeral=True) + return guild.verification_channel = channel_id session.commit() await interaction.send(f"Registered <#{channel_id}>.", ephemeral=True)