Fix lint smells and update model usage in update.

This commit is contained in:
2025-03-04 13:20:14 -05:00
parent c8becb8203
commit 43df7aeda7
+20 -21
View File
@@ -1,22 +1,20 @@
""" """Commands for updating information/variables held in database."""
Commands for updating information/variables held in database.
"""
import nextcord import nextcord
from nextcord.ext import commands from nextcord.ext import commands
from sqlalchemy import select from sqlalchemy import select
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from sqlmodel import col
from database.db import Guild, engine from database.db import engine
from database.models import Guild
from utilities.utils import autofill_users_in_guild from utilities.utils import autofill_users_in_guild
from .views.update_username_verify import UpdateUsernameVerifyView from .views.update_username_verify import UpdateUsernameVerifyView
class UpdateCog(commands.Cog, name="Update commands."): class UpdateCog(commands.Cog, name="Update commands."):
""" """Class holds commands and views needed for updating things in bot."""
Class holds commands and views needed for updating things in bot.
"""
def __init__(self, bot: commands.Bot): def __init__(self, bot: commands.Bot):
""" """
@@ -28,17 +26,13 @@ class UpdateCog(commands.Cog, name="Update commands."):
self._bot: commands.Bot = bot self._bot: commands.Bot = bot
@commands.Cog.listener() @commands.Cog.listener()
async def on_ready(self): async def on_ready(self) -> None:
""" """Called when cog is loaded. Adds views required for DataManagement commands."""
Called when cog is loaded. Adds views required for DataManagement commands.
"""
self._bot.add_view(UpdateUsernameVerifyView()) self._bot.add_view(UpdateUsernameVerifyView())
@nextcord.slash_command() @nextcord.slash_command()
async def update(self, interaction: nextcord.Interaction): async def update(self, interaction: nextcord.Interaction) -> None:
""" """Parent slash command for updating things. Never called."""
Parent slash command for updating things. Never called.
"""
@update.subcommand(description="Request a username update. Admins must approve.") @update.subcommand(description="Request a username update. Admins must approve.")
async def username( async def username(
@@ -46,10 +40,11 @@ class UpdateCog(commands.Cog, name="Update commands."):
interaction: nextcord.Interaction, interaction: nextcord.Interaction,
old_username: str = nextcord.SlashOption(description="Username currently stored in highscores.", required=True), old_username: str = nextcord.SlashOption(description="Username currently stored in highscores.", required=True),
new_username: str = nextcord.SlashOption(description="Username to update to.", required=True), new_username: str = nextcord.SlashOption(description="Username to update to.", required=True),
): ) -> None:
""" """
Creates a request to send to admins for changing a username. Uses Creates a request to send to admins for changing a username.
UpdateUsernameVerifyView to do the work.
Uses UpdateUsernameVerifyView to do the work.
Args: Args:
interaction (nextcord.Interaction): Interaction that invoked the command interaction (nextcord.Interaction): Interaction that invoked the command
@@ -57,9 +52,13 @@ class UpdateCog(commands.Cog, name="Update commands."):
new_username (str): Username to replace old with. new_username (str): Username to replace old with.
""" """
with Session(engine) as session: with Session(engine) as session:
stmt = select(Guild.VerificationChannel).where(Guild.GuildId == interaction.guild_id) stmt = select(col(Guild.verification_channel)).where(col(Guild.id) == interaction.guild_id)
verify_channel = interaction.guild.get_channel(session.scalar(stmt)) verify_channel = interaction.guild.get_channel(session.scalar(stmt))
if not isinstance(verify_channel, nextcord.TextChannel):
await interaction.send("Verification channel not set. Cannot process request.", ephemeral=True)
return
embed: nextcord.Embed = nextcord.Embed(title="Username change request.") embed: nextcord.Embed = nextcord.Embed(title="Username change request.")
embed.add_field(name="Old Username", value=old_username) embed.add_field(name="Old Username", value=old_username)
embed.add_field(name="New Username", value=new_username) embed.add_field(name="New Username", value=new_username)
@@ -71,12 +70,12 @@ class UpdateCog(commands.Cog, name="Update commands."):
await interaction.send("Request sent to admins for verification.", ephemeral=True) await interaction.send("Request sent to admins for verification.", ephemeral=True)
@username.on_autocomplete("old_username") @username.on_autocomplete("old_username")
async def all_users_autocomplete(self, interaction: nextcord.Interaction, username: str): async def all_users_autocomplete(self, interaction: nextcord.Interaction, username: str) -> None:
""" """
Sends response from autofill_users_in_guild back to update_username for autocompletion of old_username. Sends response from autofill_users_in_guild back to update_username for autocompletion of old_username.
Args: Args:
interaction (nextcord.Interaction): Interaction with command. interaction (nextcord.Interaction): Interaction with command.
old_username (str): String to run autocompletion against. username (str): String to run autocompletion against.
""" """
await interaction.response.send_autocomplete(autofill_users_in_guild(interaction.guild_id, username)) await interaction.response.send_autocomplete(autofill_users_in_guild(interaction.guild_id, username))