85 lines
3.2 KiB
Python
85 lines
3.2 KiB
Python
"""
|
|
Commands for updating information/variables held in database.
|
|
"""
|
|
|
|
import nextcord
|
|
from nextcord.ext import commands
|
|
from sqlalchemy import select
|
|
from sqlalchemy.orm import Session
|
|
|
|
# pylint: disable=import-error
|
|
from utilities.sql_data_classes import Guild, engine
|
|
from utilities.utils import autofill_users_in_guild
|
|
|
|
from .views.update_username_verify import UpdateUsernameVerifyView
|
|
|
|
|
|
class UpdateCog(commands.Cog, name="Update commands."):
|
|
"""
|
|
Class holds commands and views needed for updating things in bot.
|
|
"""
|
|
|
|
def __init__(self, bot: commands.Bot):
|
|
"""
|
|
Default constructor for Update class. Sets internal bot variable.
|
|
|
|
Args:
|
|
bot (commands.Bot): Bot object. Required to register update username button view.
|
|
"""
|
|
self._bot: commands.Bot = bot
|
|
|
|
@commands.Cog.listener()
|
|
async def on_ready(self):
|
|
"""
|
|
Called when cog is loaded. Adds views required for DataManagement commands.
|
|
"""
|
|
self._bot.add_view(UpdateUsernameVerifyView())
|
|
|
|
@nextcord.slash_command()
|
|
async def update(self, interaction: nextcord.Interaction):
|
|
"""
|
|
Parent slash command for updating things. Never called.
|
|
"""
|
|
|
|
@update.subcommand(description="Request a username update. Admins must approve.")
|
|
async def username(
|
|
self,
|
|
interaction: nextcord.Interaction,
|
|
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),
|
|
):
|
|
"""
|
|
Creates a request to send to admins for changing a username. Uses
|
|
UpdateUsernameVerifyView to do the work.
|
|
|
|
Args:
|
|
interaction (nextcord.Interaction): Interaction that invoked the command
|
|
old_username (str): Username to replace.
|
|
new_username (str): Username to replace old with.
|
|
"""
|
|
|
|
with Session(engine) as session:
|
|
stmt = select(Guild.VerificationChannel).where(Guild.GuildId == interaction.guild_id)
|
|
verify_channel = interaction.guild.get_channel(session.scalar(stmt))
|
|
|
|
embed: nextcord.Embed = nextcord.Embed(title="Username change request.")
|
|
embed.add_field(name="Old Username", value=old_username)
|
|
embed.add_field(name="New Username", value=new_username)
|
|
await verify_channel.send(
|
|
content=f"Username change request from <@{interaction.user.id}>.",
|
|
embed=embed,
|
|
view=UpdateUsernameVerifyView(),
|
|
)
|
|
await interaction.send("Request sent to admins for verification.", ephemeral=True)
|
|
|
|
@username.on_autocomplete("old_username")
|
|
async def username_autofill(self, interaction: nextcord.Interaction, username: str):
|
|
"""
|
|
Sends response from autofill_users_in_guild back to update_username for autocompletion of old_username.
|
|
|
|
Args:
|
|
interaction (nextcord.Interaction): Interaction with command.
|
|
old_username (str): String to run autocompletion against.
|
|
"""
|
|
await interaction.response.send_autocomplete(autofill_users_in_guild(interaction.guild_id, username))
|