Change autofill functions to return dicts of name id pairs.
This commit is contained in:
+22
-11
@@ -37,20 +37,24 @@ def autofill_categories(guild_id: int, group_name: str, category_begin: str) ->
|
|||||||
.subquery()
|
.subquery()
|
||||||
)
|
)
|
||||||
cat_group_alias = aliased(CategoryGroup, subq)
|
cat_group_alias = aliased(CategoryGroup, subq)
|
||||||
stmt = select(Category.CategoryName).where(Category.GroupId == cat_group_alias.GroupId)
|
stmt = select(Category).where(Category.GroupId == cat_group_alias.GroupId)
|
||||||
categories_list: "list[str]" = list(session.scalars(stmt))
|
categories_dict: dict = {cat.CategoryName: cat.CategoryId for cat in session.scalars(stmt)}
|
||||||
|
|
||||||
# if string empty, return all category names
|
# if string empty, return all category names
|
||||||
if not category_begin:
|
if not category_begin:
|
||||||
return categories_list
|
return categories_dict
|
||||||
|
|
||||||
# else find all categories that start with category_begin
|
# else find all categories that start with category_begin
|
||||||
# TODO is it better to have sql handle the starts with or python?
|
# TODO is it better to have sql handle the starts with or python?
|
||||||
categories: "list[str]" = [category for category in categories_list if category_begin.lower() in category.lower()]
|
categories: dict = {
|
||||||
|
cat.CategoryName: cat.CategoryId
|
||||||
|
for cat in categories_dict.items()
|
||||||
|
if category_begin.lower() in cat.CategoryName.lower()
|
||||||
|
}
|
||||||
return categories
|
return categories
|
||||||
|
|
||||||
|
|
||||||
def autofill_groups(guild_id: int, group_begin: str) -> "list[str]":
|
def autofill_groups(guild_id: int, group_begin: str) -> dict:
|
||||||
"""
|
"""
|
||||||
Returns all group names that start with group_begin. If group_begin is empty, returns list of all group names.
|
Returns all group names that start with group_begin. If group_begin is empty, returns list of all group names.
|
||||||
|
|
||||||
@@ -59,19 +63,23 @@ def autofill_groups(guild_id: int, group_begin: str) -> "list[str]":
|
|||||||
group_begin (str): The beginning of a group name to search with. Can be empty.
|
group_begin (str): The beginning of a group name to search with. Can be empty.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
list[str]: List of group names that start with group_begin.
|
dict: Dict of group names and ids where group name start with group_begin. Key is name, value is id.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Select all groups in guild.
|
# Select all groups in guild.
|
||||||
stmt = select(CategoryGroup.GroupName).where(CategoryGroup.GuildId == guild_id)
|
stmt = select(CategoryGroup).where(CategoryGroup.GuildId == guild_id)
|
||||||
group_list: "list[str]" = list(session.scalars(stmt))
|
|
||||||
|
group_dict: dict = {group.GroupName: group.GroupId for group in session.scalars(stmt)}
|
||||||
|
|
||||||
# if string empty, return all group names
|
# if string empty, return all group names
|
||||||
if not group_begin:
|
if not group_begin:
|
||||||
return group_list
|
return group_dict
|
||||||
|
|
||||||
# else find all groups that start with group_begin
|
# else find all groups that start with group_begin
|
||||||
groups: "list[str]" = [group for group in group_list if group_begin.lower() in group.lower()]
|
groups: dict = {
|
||||||
|
group_name: group_id for group_name, group_id in group_dict.items() if group_begin.lower() in group_name.lower()
|
||||||
|
}
|
||||||
|
|
||||||
return groups
|
return groups
|
||||||
|
|
||||||
|
|
||||||
@@ -281,7 +289,7 @@ async def send_highscore_message(channel: nextcord.TextChannel, group: CategoryG
|
|||||||
|
|
||||||
|
|
||||||
async def submit_score(
|
async def submit_score(
|
||||||
interaction: nextcord.Interaction, group: CategoryGroup, category_id: int, username: str, score: float
|
interaction: nextcord.Interaction, group_id: int, category_id: int, username: str, score: float
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
Adds passed score to group. Then updates group message by calling send_highscore_message.
|
Adds passed score to group. Then updates group message by calling send_highscore_message.
|
||||||
@@ -299,6 +307,9 @@ async def submit_score(
|
|||||||
session.add(score_obj)
|
session.add(score_obj)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
stmt = select(CategoryGroup).where(CategoryGroup.GroupId == group_id)
|
||||||
|
group = session.scalar(stmt)
|
||||||
|
|
||||||
# edit message
|
# edit message
|
||||||
print(f"Editting message {group.MessageId}")
|
print(f"Editting message {group.MessageId}")
|
||||||
channel = interaction.guild.get_channel(group.ChannelId)
|
channel = interaction.guild.get_channel(group.ChannelId)
|
||||||
|
|||||||
Reference in New Issue
Block a user