diff --git a/utilities/utils.py b/utilities/utils.py index 93e41c9..0a04d94 100644 --- a/utilities/utils.py +++ b/utilities/utils.py @@ -137,6 +137,32 @@ def autofill_users_in_group_category( return users +def autofill_users_in_guild(guild_id: int, user_begin: str) -> "list[str]": + """ + Obtains and returns a list of all the users who have submitted to the guild's highscores. + + Args: + guild_id (int): Id of the guild. + user_begin (str): Beginning of user name to search with. Can be empty. + + Returns: + list[str]: All users with submissions in the guild. + """ + group_ids = select(CategoryGroup.GroupId).where(CategoryGroup.GuildId == guild_id).subquery() + category_ids = select(Category.CategoryId).where(Category.GroupId.in_(group_ids)).subquery() + stmt = select(distinct(Score.UserName)).where(Score.CategoryId.in_(category_ids)) + users_list: "list[str]" = list(session.scalars(stmt)) + + # if string empty, return all users + if not user_begin: + return users_list + + # else find all users that start with user_begin + users = [user for user in users_list if user_begin.lower() in user.lower()] + + return users + + def format_highscore_message(group: CategoryGroup) -> str: """ Creates a formatted string for a boss's highscore discord message.