Update functions for username length being in group.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
"""View for allowing a username update."""
|
||||
|
||||
import nextcord
|
||||
from sqlalchemy import distinct, select
|
||||
from sqlalchemy import distinct, exc, select
|
||||
|
||||
# pylint: disable=import-error
|
||||
from utilities.sql_alchemy_connection import Category, CategoryGroup, Score, session
|
||||
@@ -42,25 +42,33 @@ class UpdateUsernameVerifyView(nextcord.ui.View):
|
||||
button (nextcord.ui.Button): unused
|
||||
interaction (nextcord.Interaction): Interaction with the button
|
||||
"""
|
||||
embed_content = interaction.message.embeds[0].to_dict()
|
||||
old_username: str = embed_content["fields"][0]["value"]
|
||||
new_username: str = embed_content["fields"][1]["value"]
|
||||
|
||||
stmt = select(distinct(Score.CategoryId)).where(Score.UserName == old_username)
|
||||
category_ids: "list[int]" = list(session.scalars(stmt))
|
||||
try:
|
||||
embed_content = interaction.message.embeds[0].to_dict()
|
||||
old_username: str = embed_content["fields"][0]["value"]
|
||||
new_username: str = embed_content["fields"][1]["value"]
|
||||
|
||||
stmt = select(Score).where(Score.UserName == old_username)
|
||||
for score in session.scalars(stmt):
|
||||
score.UserName = new_username
|
||||
session.commit()
|
||||
stmt = select(distinct(Score.CategoryId)).where(Score.UserName == old_username)
|
||||
category_ids: "list[int]" = list(session.scalars(stmt))
|
||||
|
||||
group_ids = select(Category.GroupId).where(Category.CategoryId.in_(category_ids))
|
||||
stmt = select(Score).where(Score.UserName == old_username)
|
||||
for score in session.scalars(stmt):
|
||||
score.UserName = new_username
|
||||
session.commit()
|
||||
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GroupId.in_(group_ids))
|
||||
group_ids = select(Category.GroupId).where(Category.CategoryId.in_(category_ids))
|
||||
|
||||
for group in session.scalars(stmt):
|
||||
await send_highscore_message(interaction.guild.get_channel(group.ChannelId), group)
|
||||
stmt = select(CategoryGroup).where(CategoryGroup.GroupId.in_(group_ids))
|
||||
|
||||
content = interaction.message.content
|
||||
content += f"\nApproved by <@{interaction.user.id}>."
|
||||
await interaction.message.edit(content=content, embeds=interaction.message.embeds, view=None)
|
||||
for group in session.scalars(stmt):
|
||||
await send_highscore_message(interaction.guild.get_channel(group.ChannelId), group)
|
||||
|
||||
content = interaction.message.content
|
||||
content += f"\nApproved by <@{interaction.user.id}>."
|
||||
await interaction.message.edit(content=content, embeds=interaction.message.embeds, view=None)
|
||||
except exc.DataError:
|
||||
session.rollback()
|
||||
content = interaction.message.content
|
||||
content += "\nError, UserName is too long."
|
||||
await interaction.message.edit(content=content, embeds=interaction.message.embeds, view=None)
|
||||
await interaction.response.send_message(content="User must resubmit with shorter name.", ephemeral=True)
|
||||
|
||||
Reference in New Issue
Block a user