From 7919a63e04396146736f53de3116890b4d0d9b57 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Sat, 5 Oct 2024 12:27:14 -0400 Subject: [PATCH] run ruff check --fix --- cogs/commands/create.py | 6 +-- cogs/commands/delete.py | 12 ++---- cogs/commands/help.py | 1 - cogs/commands/refresh.py | 3 -- cogs/commands/set.py | 12 ++---- cogs/commands/update.py | 1 - cogs/commands/view.py | 2 +- cogs/commands/views/delete_confirm.py | 3 +- cogs/commands/views/update_username_verify.py | 3 +- cogs/guild_listener/__init__.py | 2 - cogs/message_listener/__init__.py | 1 - cogs/submission/submission_views.py | 6 +-- main.py | 3 +- utilities/utils.py | 40 +++++++------------ 14 files changed, 29 insertions(+), 66 deletions(-) diff --git a/cogs/commands/create.py b/cogs/commands/create.py index 6a0c03a..d2a8144 100644 --- a/cogs/commands/create.py +++ b/cogs/commands/create.py @@ -2,7 +2,6 @@ Commands for creating items in bot. """ -from typing import Optional import nextcord from nextcord.ext import commands @@ -64,7 +63,6 @@ class CreateCog(commands.Cog, name="Create Cog"): is_validated (bool): If the scores should be validated or not. Is SlashOption. size (int): How many sores to show in the category message. Is SlashOption. """ - new_category = Category( CategoryName=category_name, GroupId=int(group_id), @@ -113,7 +111,7 @@ class CreateCog(commands.Cog, name="Create Cog"): min_value=1, max_value=30, ), - channel: Optional[nextcord.abc.GuildChannel] = nextcord.SlashOption( + channel: nextcord.abc.GuildChannel | None = nextcord.SlashOption( description="Channel to post Group in. If left empty, defaults to current channel.", required=False, channel_types=[nextcord.ChannelType.text], @@ -132,7 +130,6 @@ class CreateCog(commands.Cog, name="Create Cog"): channel (Optional[nextcord.abc.GuildChannel]): Channel to post group in. If None, defaults to current channel. Is SlashOption. """ - # if no channel provided, use current channel. if channel is None: channel = interaction.channel @@ -168,5 +165,4 @@ class CreateCog(commands.Cog, name="Create Cog"): interaction (nextcord.Interaction): Interaction with command. group_id (str): String to run autocomplete compared to. """ - await interaction.response.send_autocomplete(autofill_groups(interaction.guild_id, group_id)) diff --git a/cogs/commands/delete.py b/cogs/commands/delete.py index 2c7af4d..bc3945e 100644 --- a/cogs/commands/delete.py +++ b/cogs/commands/delete.py @@ -2,7 +2,6 @@ Commands for deleting groups/categories/scores in database and messages in discord.. """ -from typing import Optional import nextcord from nextcord.ext import commands @@ -48,7 +47,7 @@ class DeleteCog(commands.Cog, name="Table Management"): async def channel_messages( self, interaction: nextcord.Interaction, - channel: Optional[nextcord.abc.GuildChannel] = nextcord.SlashOption( + channel: nextcord.abc.GuildChannel | None = nextcord.SlashOption( description="Channel to clear of messages.", required=False, channel_types=[nextcord.ChannelType.text], @@ -64,7 +63,6 @@ class DeleteCog(commands.Cog, name="Table Management"): channel (Optional[nextcord.abc.GuildChannel]): Channel to clear of messages. Is a SlashOption. Limited to text channels. Defaults to None. """ - if not channel: channel = interaction.channel @@ -107,10 +105,9 @@ class DeleteCog(commands.Cog, name="Table Management"): interaction (nextcord.Interaction): Interaction that invoked the command. user_name (str): User to remove from highscores """ - with Session(engine) as session: stmt = select(distinct(Score.CategoryId)).where(Score.UserName == user_name) - category_ids: "list[int]" = list(session.scalars(stmt)) + category_ids: list[int] = list(session.scalars(stmt)) stmt = select(Score).where(Score.UserName == user_name) for score in session.scalars(stmt): @@ -185,7 +182,6 @@ class DeleteCog(commands.Cog, name="Table Management"): category_id (str): String representation of Category id to remove user from. Is SlashOption. user_name (str): The user to remove. Is SlashOption. """ - with Session(engine) as session: stmt = select(CategoryGroup).where(CategoryGroup.GroupId == int(group_id)) group = session.scalar(stmt) @@ -219,7 +215,6 @@ class DeleteCog(commands.Cog, name="Table Management"): group_id (str): String representation of Group id to remove user from. Is SlashOption. user_name (str): The user to remove. Is SlashOption. """ - with Session(engine) as session: stmt = select(CategoryGroup).where(CategoryGroup.GroupId == int(group_id)) group = session.scalar(stmt) @@ -267,7 +262,7 @@ class DeleteCog(commands.Cog, name="Table Management"): @from_category.on_autocomplete("user_name") @from_group.on_autocomplete("user_name") async def user_autocomplete( - self, interaction: nextcord.Interaction, user_name: str, group_id: str, category_id: Optional[str] = None + self, interaction: nextcord.Interaction, user_name: str, group_id: str, category_id: str | None = None ): """ Autocomplete functions for user names. @@ -278,7 +273,6 @@ class DeleteCog(commands.Cog, name="Table Management"): group_id (str): String representation of Group id to find category from. category_id (Optional[str]): String representation of Category id to find User from, can be None. """ - await interaction.response.send_autocomplete( autofill_users_in_group_category( interaction.guild_id, int(group_id), category_id if category_id else 0, user_name diff --git a/cogs/commands/help.py b/cogs/commands/help.py index 52f82e2..db6cef7 100644 --- a/cogs/commands/help.py +++ b/cogs/commands/help.py @@ -30,7 +30,6 @@ class HelpCog(commands.Cog, name="Help commands."): Args: interaction (nextcord.Interaction): Interaction that invoked the command. """ - # I hate this but don't want indents in the string. # pylint: disable=line-too-long help_str: str = """Welcome to Agon! Three simple steps are needed to get started. diff --git a/cogs/commands/refresh.py b/cogs/commands/refresh.py index aae54a6..f7d3a91 100644 --- a/cogs/commands/refresh.py +++ b/cogs/commands/refresh.py @@ -46,7 +46,6 @@ class RefreshCog(commands.Cog, name="Refresh Cog"): interaction (nextcord.Interaction): Interaction that invoked the command. group_id (str): Str representation of Group id to refresh. Is SlashOption. """ - with Session(engine) as session: stmt = ( select(CategoryGroup) @@ -78,7 +77,6 @@ class RefreshCog(commands.Cog, name="Refresh Cog"): Args: interaction (nextcord.Interaction): Interaction that invoked the command. """ - with Session(engine) as session: stmt = select(CategoryGroup).where(CategoryGroup.GuildId == interaction.guild_id) @@ -100,5 +98,4 @@ class RefreshCog(commands.Cog, name="Refresh Cog"): interaction (nextcord.Interaction): Interaction with command. group_id (str): String to run autocomplete compared to. """ - await interaction.response.send_autocomplete(autofill_groups(interaction.guild_id, group_id)) diff --git a/cogs/commands/set.py b/cogs/commands/set.py index 946d0a5..21311ba 100644 --- a/cogs/commands/set.py +++ b/cogs/commands/set.py @@ -2,7 +2,6 @@ Set command heirarchy. """ -from typing import Optional import nextcord from nextcord.ext import commands @@ -37,7 +36,7 @@ class SetCog(commands.Cog, name="Set command."): group_id: str = nextcord.SlashOption( name="group", description="Category Group to assign to channel.", required=True ), - channel: Optional[nextcord.abc.GuildChannel] = nextcord.SlashOption( + channel: nextcord.abc.GuildChannel | None = nextcord.SlashOption( description="Channel to register", required=False, channel_types=[nextcord.ChannelType.text], @@ -53,7 +52,6 @@ class SetCog(commands.Cog, name="Set command."): channel (nextcord.abc.GuildChannel, optional): Channel to register Is a SlashOption. Limited to text channels, Defaults to None. """ - if not channel: channel = interaction.channel channel_id = channel.id @@ -93,7 +91,6 @@ class SetCog(commands.Cog, name="Set command."): group_id (str): String representation of Group id to set pad length in. Is SlashOption. length (int): Number of characters. Is SlashOption. Defaults to 26. """ - with Session(engine) as session: stmt = ( select(CategoryGroup) @@ -111,7 +108,7 @@ class SetCog(commands.Cog, name="Set command."): async def submission_channel( self, interaction: nextcord.Interaction, - channel: Optional[nextcord.abc.GuildChannel] = nextcord.SlashOption( + channel: nextcord.abc.GuildChannel | None = nextcord.SlashOption( description="Channel to register", required=False, channel_types=[nextcord.ChannelType.text], @@ -126,7 +123,6 @@ class SetCog(commands.Cog, name="Set command."): channel Optional[nextcord.abc.GuildChannel], optional): Channel to register Is a SlashOption. Limited to text channels, Defaults to None. """ - if not channel: channel = interaction.channel @@ -156,7 +152,6 @@ class SetCog(commands.Cog, name="Set command."): interaction (nextcord.Interaction): Interaction that invoked the command length (int): Number of characters. Is SlashOption. Defaults to 12. """ - with Session(engine) as session: stmt = ( select(CategoryGroup) @@ -172,7 +167,7 @@ class SetCog(commands.Cog, name="Set command."): async def verify_channel( self, interaction: nextcord.Interaction, - channel: Optional[nextcord.abc.GuildChannel] = nextcord.SlashOption( + channel: nextcord.abc.GuildChannel | None = nextcord.SlashOption( description="Channel to register", required=False, channel_types=[nextcord.ChannelType.text], @@ -187,7 +182,6 @@ class SetCog(commands.Cog, name="Set command."): channel (Optional[nextcord.abc.GuildChannel]): Channel to register Is a SlashOption. Limited to text channels, Defaults to None. """ - if not channel: channel = interaction.channel channel_id = channel.id diff --git a/cogs/commands/update.py b/cogs/commands/update.py index 201de09..2f8ac0a 100644 --- a/cogs/commands/update.py +++ b/cogs/commands/update.py @@ -57,7 +57,6 @@ class UpdateCog(commands.Cog, name="Update commands."): 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)) diff --git a/cogs/commands/view.py b/cogs/commands/view.py index 148abe9..497956d 100644 --- a/cogs/commands/view.py +++ b/cogs/commands/view.py @@ -49,7 +49,7 @@ class ViewCog(commands.Cog, name="View Command"): group_id (str): String representation of Group id to search from. Is SlashOption. category_id (str): String representation of Category id to show leaderboard. Is SlashOption. """ - str_list: "list[str]" = [] + str_list: list[str] = [] # get group, category, and all scores sorted correctly. with Session(engine) as session: diff --git a/cogs/commands/views/delete_confirm.py b/cogs/commands/views/delete_confirm.py index 0296013..ffde399 100644 --- a/cogs/commands/views/delete_confirm.py +++ b/cogs/commands/views/delete_confirm.py @@ -2,7 +2,6 @@ Contains view and helper functions for confirming and processing deletes. """ -from typing import Optional import nextcord from sqlalchemy import delete, select @@ -61,7 +60,7 @@ class DeleteConfirmView(nextcord.ui.Modal): await interaction.response.send_message("Deletion complete.", ephemeral=True) -def create_delete_id(group_id: int, category_id: Optional[int] = None) -> str: +def create_delete_id(group_id: int, category_id: int | None = None) -> str: """ Creates a delete id string to tell the view what to delete. diff --git a/cogs/commands/views/update_username_verify.py b/cogs/commands/views/update_username_verify.py index 84b2fcf..44ea5bf 100644 --- a/cogs/commands/views/update_username_verify.py +++ b/cogs/commands/views/update_username_verify.py @@ -43,7 +43,6 @@ class UpdateUsernameVerifyView(nextcord.ui.View): button (nextcord.ui.Button): unused interaction (nextcord.Interaction): Interaction with the button """ - with Session(engine) as session: try: embed_content = interaction.message.embeds[0].to_dict() @@ -51,7 +50,7 @@ class UpdateUsernameVerifyView(nextcord.ui.View): 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)) + category_ids: list[int] = list(session.scalars(stmt)) stmt = select(Score).where(Score.UserName == old_username) for score in session.scalars(stmt): diff --git a/cogs/guild_listener/__init__.py b/cogs/guild_listener/__init__.py index 5e4f2fa..f51f447 100644 --- a/cogs/guild_listener/__init__.py +++ b/cogs/guild_listener/__init__.py @@ -25,7 +25,6 @@ class GuildListener(commands.Cog, name="Guild Listener"): Args: guild (nextcord.Guild): Guild that was joined. """ - with Session(engine) as session: stmt = select(Guild).where(Guild.GuildId == guild.id) guild_data = session.scalar(stmt) @@ -53,7 +52,6 @@ class GuildListener(commands.Cog, name="Guild Listener"): Args: guild (nextcord.Guild): Guild that was left. """ - with Session(engine) as session: stmt = select(Guild).where(Guild.GuildId == guild.id) guild_data = session.scalar(stmt) diff --git a/cogs/message_listener/__init__.py b/cogs/message_listener/__init__.py index fad03c1..1abb3b9 100644 --- a/cogs/message_listener/__init__.py +++ b/cogs/message_listener/__init__.py @@ -113,7 +113,6 @@ async def birthday_action(message: nextcord.Message) -> None: Args: message (nextcord.Message): The message to check for birthday thing. """ - today = datetime.today() birthday = datetime(today.year, 7, 31) chris = 642330132958281750 diff --git a/cogs/submission/submission_views.py b/cogs/submission/submission_views.py index 81c3bed..83711e0 100644 --- a/cogs/submission/submission_views.py +++ b/cogs/submission/submission_views.py @@ -1,6 +1,5 @@ """Views used for submission process.""" -from typing import Optional import nextcord from nextcord.ext import commands @@ -17,7 +16,7 @@ from .verification_view import VerificationView def category_select_options(group_id: int) -> "list[nextcord.SelectOption]": """Returns list of categories for a given boss.""" - options: "list[nextcord.SelectOption]" = [] + options: list[nextcord.SelectOption] = [] with Session(engine) as session: subq = select(CategoryGroup).where(CategoryGroup.GroupId == group_id).subquery() @@ -149,7 +148,6 @@ class SubmissionCreateButton(nextcord.ui.View): @nextcord.ui.button(label="Submit", style=nextcord.ButtonStyle.primary, custom_id="submitcreate") async def submitcreate_button(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): # pylint: disable=unused-argument """Button for score submission.""" - # Don't allow a user to start a new submission if one already exists if submission_objects.has_user(interaction.user.id): await interaction.send("Please finish in progress submission first.", ephemeral=True) @@ -196,7 +194,7 @@ class SubmissionView(nextcord.ui.View): bot: commands.Bot, submission_user: int, *, - timeout: Optional[float] = 300, + timeout: float | None = 300, ): super().__init__(timeout=timeout) self._submission_user = submission_user diff --git a/main.py b/main.py index 224f7f5..f936260 100644 --- a/main.py +++ b/main.py @@ -9,9 +9,10 @@ import sys import nextcord from nextcord.ext import commands +from classes.submission import SubmissionMap + # pylint: disable=import-error from utilities import config -from classes.submission import SubmissionMap logger = logging.getLogger("nextcord") logger.setLevel(logging.DEBUG) diff --git a/utilities/utils.py b/utilities/utils.py index 2c4306d..5082112 100644 --- a/utilities/utils.py +++ b/utilities/utils.py @@ -31,7 +31,6 @@ def autofill_categories( Returns: dict[str, str]: List of all category names that start with category_begin. """ - with Session(engine) as session: # search database for all category names subq = ( @@ -42,7 +41,7 @@ def autofill_categories( ) cat_group_alias = aliased(CategoryGroup, subq) stmt = select(Category).where(Category.GroupId == cat_group_alias.GroupId) - categories_dict: "dict[str, str]" = { + categories_dict: dict[str, str] = { cat.CategoryName: str(cat.CategoryId) for cat in session.scalars(stmt) } @@ -52,7 +51,7 @@ def autofill_categories( # else find all categories that start with category_begin # TODO is it better to have sql handle the starts with or python? - categories: "dict[str, str]" = { + categories: dict[str, str] = { cat_name: cat_id for cat_name, cat_id in categories_dict.items() if category_begin.lower() in cat_name.lower() @@ -71,13 +70,12 @@ def autofill_groups(guild_id: int, group_begin: str) -> "dict[str, str]": Returns: dict[str, str]: Dict of group names and ids where group name start with group_begin. Key is name, value is id. """ - with Session(engine) as session: # Select all groups in guild. stmt = select(CategoryGroup).where(CategoryGroup.GuildId == guild_id) # dict has to have key values of same type for discord. - group_dict: "dict[str, str]" = { + group_dict: dict[str, str] = { group.GroupName: str(group.GroupId) for group in session.scalars(stmt) } @@ -86,7 +84,7 @@ def autofill_groups(guild_id: int, group_begin: str) -> "dict[str, str]": return dict(list(group_dict.items())[:25]) # else find all groups that start with group_begin - groups: "dict[str, str]" = { + groups: dict[str, str] = { group_name: group_id for group_name, group_id in group_dict.items() if group_begin.lower() in group_name.lower() @@ -111,7 +109,6 @@ def autofill_users_in_group_category( Returns: list[str]: List of users that start with user_begin. """ - with Session(engine) as session: # get all categories in the group subq = ( @@ -142,14 +139,14 @@ def autofill_users_in_group_category( Score.CategoryId == category.CategoryId ) - users_list: "list[str]" = list(session.scalars(stmt)) + users_list: list[str] = list(session.scalars(stmt)) # if string empty, return all users if not user_begin: return users_list[:25] # else find all users that start with user_begin - users: "list[str]" = [ + users: list[str] = [ user for user in users_list if user_begin.lower() in user.lower() ] @@ -167,7 +164,6 @@ def autofill_users_in_guild(guild_id: int, user_begin: str) -> "list[str]": Returns: list[str]: All users with submissions in the guild. """ - with Session(engine) as session: group_ids = select(CategoryGroup.GroupId).where( CategoryGroup.GuildId == guild_id @@ -178,14 +174,14 @@ def autofill_users_in_guild(guild_id: int, user_begin: str) -> "list[str]": stmt = select(distinct(Score.UserName)).where( Score.CategoryId.in_(category_ids) ) - users_list: "list[str]" = list(session.scalars(stmt)) + users_list: list[str] = list(session.scalars(stmt)) # if string empty, return all users if not user_begin: return users_list[:25] # else find all users that start with user_begin - users: "list[str]" = [ + users: list[str] = [ user for user in users_list if user_begin.lower() in user.lower() ] @@ -205,7 +201,6 @@ def format_highscore_message(session: Session, group: CategoryGroup) -> str: Returns: str: Formatted string for discord message """ - ret_string = f"**{group.GroupName}**\n```" stmt = select(Guild).where(Guild.GuildId == group.GuildId) @@ -227,8 +222,7 @@ def format_highscore_message(session: Session, group: CategoryGroup) -> str: max_cat_size: int = 0 pad_length = group.PadLength for cat in session.scalars(stmt): - if cat.Size > max_cat_size: - max_cat_size = cat.Size + max_cat_size = max(cat.Size, max_cat_size) categories.append(cat) category_ids.append(cat.CategoryId) @@ -240,7 +234,7 @@ def format_highscore_message(session: Session, group: CategoryGroup) -> str: # this list is what will be printed in the discord message # first string will be category list, others will be score printouts - str_list: "list[str]" = ["" for i in range(max_cat_size + 1)] + str_list: list[str] = ["" for i in range(max_cat_size + 1)] groups_df = d_f.groupby("CategoryId") # TODO figure out how to have less nests. @@ -275,12 +269,11 @@ def format_highscore_message(session: Session, group: CategoryGroup) -> str: ind + 1, pad_length, ) + # if not enough entries, pad out + elif ind + 1 <= category.Size: + str_list[ind + 1] += f"{ind + 1}: ".ljust(pad_length, " ") else: - # if not enough entries, pad out - if ind + 1 <= category.Size: - str_list[ind + 1] += f"{ind + 1}: ".ljust(pad_length, " ") - else: - str_list[ind + 1] += "".ljust(pad_length, " ") + str_list[ind + 1] += "".ljust(pad_length, " ") else: for ind in range(max_cat_size): if ind + 1 <= category.Size: @@ -379,7 +372,6 @@ async def send_highscore_message( Returns: nextcord.Message: Message object that was posted with group contents. """ - highscore_string = format_highscore_message(session, group) channel = guild.get_channel(group.ChannelId) @@ -413,7 +405,6 @@ async def submit_score( username (str): The user who submitted the score. score (str): Score to submit. Need to convert to correct type. """ - with Session(engine) as session: stmt = select(Category.IsTimeRecord).where(Category.CategoryId == category_id) is_time_record = session.scalar(stmt) @@ -454,10 +445,9 @@ def add_preset_configuration(guild_id: int, config_str: str) -> None: guild_id (int): Guild id to add config to config_str (str): Config name to add """ - with Session(engine) as session: with open( - f"example_config/{config_str}.json", encoding="utf-8", mode="r" + f"example_config/{config_str}.json", encoding="utf-8" ) as file: config = json.load(file)