From 289113661f0adaa21a11e914060bcc4d9652c66f Mon Sep 17 00:00:00 2001 From: imcompiling Date: Wed, 12 Mar 2025 09:50:58 -0400 Subject: [PATCH] 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 6a57008..ad53caa 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