From ccbe86623af9c51b2f480bbb3bb2f11e246f868a Mon Sep 17 00:00:00 2001 From: imcompiling Date: Wed, 22 May 2024 15:18:44 -0400 Subject: [PATCH] Run ruff linter with autofixing. --- classes/election_cycle.py | 18 ++++-------------- classes/gitea_webhook.py | 16 +++++++--------- classes/nominate_highcouncil_views.py | 4 ---- classes/nominate_views.py | 2 -- classes/voting_highcouncil_views.py | 5 +---- classes/voting_views.py | 4 +--- cogs/clan_messages/__init__.py | 1 - cogs/commands/__init__.py | 1 - cogs/commands/record.py | 2 -- cogs/commands/remove.py | 2 -- cogs/commands/request.py | 3 --- cogs/commands/set.py | 1 - cogs/commands/test.py | 1 - cogs/commands/view.py | 4 ---- cogs/loops/__init__.py | 1 - cogs/loops/bot_presence.py | 1 - cogs/loops/daily_loop.py | 4 ---- cogs/message_listener/__init__.py | 1 - database_setup/create_database.py | 2 +- main.py | 2 +- pyproject.toml | 5 ++++- utils.py | 14 +++----------- 22 files changed, 22 insertions(+), 72 deletions(-) diff --git a/classes/election_cycle.py b/classes/election_cycle.py index c10b672..cdda7c2 100644 --- a/classes/election_cycle.py +++ b/classes/election_cycle.py @@ -46,7 +46,6 @@ class ElectionCycle: config (DakinyConfig): The entire config object. guild (nextcord.Guild): The guild that this election is running in. """ - self.config: DakinyConfig = config self.cycle = cycle self.election_cycle = self.config.election_cycles[self.cycle] @@ -69,8 +68,7 @@ class ElectionCycle: list[str]: A list of message contents in the correct order. Must be a string because the message is too long. """ - - with open(f"{TEMPLATES_DIR}/election_announcement.txt", mode="r", encoding="utf8") as file: + with open(f"{TEMPLATES_DIR}/election_announcement.txt", encoding="utf8") as file: message = file.read() next_election = get_datetime( @@ -98,8 +96,7 @@ class ElectionCycle: Returns: str: Message to send. """ - - with open(f"{TEMPLATES_DIR}/highcouncil_election.txt", mode="r", encoding="utf8") as file: + with open(f"{TEMPLATES_DIR}/highcouncil_election.txt", encoding="utf8") as file: message = file.read() return message.format( @@ -117,8 +114,7 @@ class ElectionCycle: Returns: str: Message to send. """ - - with open(f"{TEMPLATES_DIR}/highcouncil_vote.txt", mode="r", encoding="utf8") as file: + with open(f"{TEMPLATES_DIR}/highcouncil_vote.txt", encoding="utf8") as file: message = file.read() return message.format( @@ -133,8 +129,7 @@ class ElectionCycle: Returns: str: Message to send """ - - with open(f"{TEMPLATES_DIR}/vote_start.txt", mode="r", encoding="utf8") as file: + with open(f"{TEMPLATES_DIR}/vote_start.txt", encoding="utf8") as file: message = file.read() vote_end: datetime = get_datetime(self.config.election_cycles[self.cycle].voting_end) @@ -226,7 +221,6 @@ class ElectionCycle: """ Performs required actions for when voting time has ended. """ - message = self.build_highcouncil_election_announce() council_elect_role: nextcord.Role = self.guild.get_role(self.config.params.clan_council_elect_role_id) election_thread: nextcord.Thread = self.guild.get_thread(self.get_election_info().ThreadId) @@ -278,7 +272,6 @@ class ElectionCycle: """ Performs required actions for when the highcouncil is elected. """ - message = self.build_highcouncil_vote_start() election_thread: nextcord.Thread = self.guild.get_thread(self.get_election_info().ThreadId) await election_thread.send(message) @@ -303,7 +296,6 @@ class ElectionCycle: """ Marks the election as over. """ - self.election_over = True elect_role: nextcord.Role = self.guild.get_role(self.config.params.clan_council_elect_role_id) @@ -396,7 +388,6 @@ class ElectionCycle: Returns: ElectionInformation: The information from the database about the current election. """ - with Session(engine) as session: stmt = select(ElectionInformation).where( ElectionInformation.ElectionCycle == get_election_name(self.config) @@ -407,7 +398,6 @@ class ElectionCycle: """ Sends a message to general chat reminder clan about election. """ - general: nextcord.TextChannel = self.guild.get_channel(self.config.params.general_channel_id) message = f"Reminder to <@&{self.config.params.clan_member_role_id}>! Clan elections are happening! Head over " diff --git a/classes/gitea_webhook.py b/classes/gitea_webhook.py index 2e587ab..ce0541a 100644 --- a/classes/gitea_webhook.py +++ b/classes/gitea_webhook.py @@ -4,8 +4,6 @@ from __future__ import annotations -from typing import List - from pydantic import BaseModel @@ -29,9 +27,9 @@ class Commit(BaseModel): committer: Committer verification: None timestamp: str - added: List[str] - removed: List[str] - modified: List[str] + added: list[str] + removed: list[str] + modified: list[str] class HeadCommit(BaseModel): @@ -42,9 +40,9 @@ class HeadCommit(BaseModel): committer: Committer verification: None timestamp: str - added: List[str] - removed: List[str] - modified: List[str] + added: list[str] + removed: list[str] + modified: list[str] class Owner(BaseModel): @@ -187,7 +185,7 @@ class GiteaWebhook(BaseModel): before: str after: str compare_url: str - commits: List[Commit] + commits: list[Commit] total_commits: int head_commit: HeadCommit repository: Repository diff --git a/classes/nominate_highcouncil_views.py b/classes/nominate_highcouncil_views.py index 8d7d363..49e829f 100644 --- a/classes/nominate_highcouncil_views.py +++ b/classes/nominate_highcouncil_views.py @@ -55,7 +55,6 @@ class NominateDMModal(nextcord.ui.Modal): interaction (Interaction): Interaction with user. """ - speech = decorate_dm_nomination(interaction.user.id, self.speech.value) with Session(engine) as session: @@ -116,7 +115,6 @@ class NominateCLModal(nextcord.ui.Modal): interaction (Interaction): Interaction with user. """ - speech = decorate_cl_nomination(interaction.user.id, self.speech.value) with Session(engine) as session: @@ -163,7 +161,6 @@ class NominateHighCouncilView(nextcord.ui.View): button (nextcord.ui.Button): Unused. idk why its needed in the decorator interaction (nextcord.Interaction): Interaction of user who pressed the button """ - # enforce council elect only if self.guild.get_role(self.config.params.clan_council_elect_role_id) not in interaction.user.roles: await interaction.response.send_message( @@ -207,7 +204,6 @@ class NominateHighCouncilView(nextcord.ui.View): button (nextcord.ui.Button): Unused. idk why its needed in the decorator interaction (nextcord.Interaction): Interaction of user who pressed the button """ - # enforce council elect only if self.guild.get_role(self.config.params.clan_council_elect_role_id) not in interaction.user.roles: await interaction.response.send_message( diff --git a/classes/nominate_views.py b/classes/nominate_views.py index 1b253af..8211288 100644 --- a/classes/nominate_views.py +++ b/classes/nominate_views.py @@ -50,7 +50,6 @@ class NominateModal(nextcord.ui.Modal): interaction (Interaction): Interaction with user. """ - speech = decorate_nomination(interaction.user.id, self.speech.value) with Session(engine) as session: @@ -102,7 +101,6 @@ class NominateView(nextcord.ui.View): button (nextcord.ui.Button): Unused. idk why its needed in the decorator interaction (nextcord.Interaction): Interaction of user who pressed the button """ - # enforce members only if self.guild.get_role(self.config.params.clan_member_role_id) not in interaction.user.roles: await interaction.response.send_message( diff --git a/classes/voting_highcouncil_views.py b/classes/voting_highcouncil_views.py index bf74387..a0e9350 100644 --- a/classes/voting_highcouncil_views.py +++ b/classes/voting_highcouncil_views.py @@ -2,7 +2,6 @@ Contains various views to be used while voting for high council. Controls the logic when performing the vote. """ -from typing import List import nextcord from nextcord import SelectOption @@ -21,7 +20,7 @@ from utils import VoteType, build_highcouncil_votes_str, get_election_name class VoteSelect(nextcord.ui.StringSelect): - def __init__(self, options: List["SelectOption"], config: DakinyConfig, election_type: str) -> None: + def __init__(self, options: list["SelectOption"], config: DakinyConfig, election_type: str) -> None: super().__init__( placeholder="Choose 1", min_values=1, @@ -105,7 +104,6 @@ class VoteHighcouncilView(nextcord.ui.View): button (nextcord.ui.Button): Unused. idk why its needed in the decorator interaction (nextcord.Interaction): Interaction of user who pressed the button """ - # enforce council elect only if self.guild.get_role(self.config.params.clan_council_elect_role_id) not in interaction.user.roles: await interaction.response.send_message( @@ -176,7 +174,6 @@ class VoteHighcouncilView(nextcord.ui.View): button (nextcord.ui.Button): Unused. idk why its needed in the decorator interaction (nextcord.Interaction): Interaction of user who pressed the button """ - # enforce council elect only if self.guild.get_role(self.config.params.clan_council_elect_role_id) not in interaction.user.roles: await interaction.response.send_message( diff --git a/classes/voting_views.py b/classes/voting_views.py index 7c9420a..4e8491a 100644 --- a/classes/voting_views.py +++ b/classes/voting_views.py @@ -2,7 +2,6 @@ Contains various views to be used while voting. Controls the logic when performing the vote. """ -from typing import List import nextcord from nextcord import SelectOption @@ -21,7 +20,7 @@ from utils import VoteType, build_votes_str, get_election_name class VoteSelect(nextcord.ui.StringSelect): - def __init__(self, options: List["SelectOption"], config: DakinyConfig) -> None: + def __init__(self, options: list["SelectOption"], config: DakinyConfig) -> None: super().__init__( placeholder="Choose 1", min_values=1, @@ -98,7 +97,6 @@ class VoteView(nextcord.ui.View): button (nextcord.ui.Button): Unused. idk why its needed in the decorator interaction (nextcord.Interaction): Interaction of user who pressed the button """ - # enforce members only if self.guild.get_role(self.config.params.clan_member_role_id) not in interaction.user.roles: await interaction.response.send_message( diff --git a/cogs/clan_messages/__init__.py b/cogs/clan_messages/__init__.py index 619685b..33326a2 100644 --- a/cogs/clan_messages/__init__.py +++ b/cogs/clan_messages/__init__.py @@ -44,7 +44,6 @@ class ClanMessages(commands.Cog, name="Clan Messages"): Pulls from git and then calls method to update all the discord messages as described by the files in the git repo. """ - # Get all modified/added files modified_files: list[str] = [ file for list in [commit.modified + commit.added for commit in hook_info.commits] for file in list diff --git a/cogs/commands/__init__.py b/cogs/commands/__init__.py index df6a0c2..6dab456 100644 --- a/cogs/commands/__init__.py +++ b/cogs/commands/__init__.py @@ -19,7 +19,6 @@ def setup(bot: commands.Bot): """ Adds all command classes to bot. """ - bot.add_cog(RecordCog(bot)) bot.add_cog(RefreshCog(bot)) bot.add_cog(RemoveCog()) diff --git a/cogs/commands/record.py b/cogs/commands/record.py index 435eab9..93fa223 100644 --- a/cogs/commands/record.py +++ b/cogs/commands/record.py @@ -47,7 +47,6 @@ class RecordCog(commands.Cog, name="Record Commands"): name (str): Name of the event. host (nextcord.Member, optional): Alternative host to user using command. """ - # check role if interaction.guild.get_role(self.bot.config.params.event_host_role_id) not in interaction.user.roles: await interaction.response.send_message( @@ -110,7 +109,6 @@ class EventView(nextcord.ui.View): Submits the event embed to the event channel. Removes view and embed from recording message. Sends point information to database. """ - discord_users = self.event_participants.values.members if discord_users: discord_users_list: list[str] = [str(user.id) for user in discord_users if user.id is not self.host_id] diff --git a/cogs/commands/remove.py b/cogs/commands/remove.py index 43715ba..9dde841 100644 --- a/cogs/commands/remove.py +++ b/cogs/commands/remove.py @@ -41,7 +41,6 @@ class RemoveCog(commands.Cog, name="Remove Commands"): interaction (nextcord.Interaction): Interaction that invoked the command. presence (str): Is SlashOption. The presence to remove. Is the ID of the presence. """ - presence_id = int(presence_str) with Session(engine) as session: stmt = delete(Presences).where(Presences.Id == presence_id) @@ -60,7 +59,6 @@ class RemoveCog(commands.Cog, name="Remove Commands"): interaction (nextcord.Interaction): Interaction with command. presence_str (str): String to run autocomplete on. """ - with Session(engine) as session: stmt = select(Presences) presences_dict: "dict[str,str]" = {pres.Presence: str(pres.Id) for pres in session.scalars(stmt)} diff --git a/cogs/commands/request.py b/cogs/commands/request.py index b1b0401..2e2638b 100644 --- a/cogs/commands/request.py +++ b/cogs/commands/request.py @@ -45,7 +45,6 @@ class RequestCog(commands.Cog, name="Request Commands"): interaction (nextcord.Interaction): Interaction that invoked the command. presence (str): The presence to add. """ - config: DakinyConfig = self.bot.config content = f'<@{interaction.user.id}> would like to add presence "{presence}."' view = PresenceChange(presence) @@ -70,7 +69,6 @@ class RequestCog(commands.Cog, name="Request Commands"): interaction (nextcord.Interaction): Interaction that invoked the command. new_username(str): The username to change to. """ - config: DakinyConfig = self.bot.config content = f'<@{interaction.user.id}> would like to change their username to "{new_nickname}."' view = UsernameRequest(interaction.user.id, new_nickname) @@ -160,7 +158,6 @@ class PresenceChange(nextcord.ui.View): button (nextcord.ui.Button): Unused. interaction (nextcord.Interaction): The interaction of the user clicking the button. """ - with Session(engine) as session: session.add(Presences(Presence=self.presence)) session.commit() diff --git a/cogs/commands/set.py b/cogs/commands/set.py index 4331e41..f938ff4 100644 --- a/cogs/commands/set.py +++ b/cogs/commands/set.py @@ -45,7 +45,6 @@ class setCog(commands.Cog, name="Set Commands"): month (int, optional): The month a user was born. Is SlashOption. day (int, optional): The day a user was born. Is SlashOption. """ - # Must be deferred, apparently datetime takes a little bit on invalid dates. await interaction.response.defer(ephemeral=True) diff --git a/cogs/commands/test.py b/cogs/commands/test.py index d1a4a8b..51fee65 100644 --- a/cogs/commands/test.py +++ b/cogs/commands/test.py @@ -47,7 +47,6 @@ class TestCog(commands.Cog, name="Test commands."): Args: interaction (nextcord.Interaction): _description_ """ - config: DakinyConfig = self.bot.config cycle = ElectionCycle( get_current_election_cycle(config), diff --git a/cogs/commands/view.py b/cogs/commands/view.py index fe37d04..9887081 100644 --- a/cogs/commands/view.py +++ b/cogs/commands/view.py @@ -104,7 +104,6 @@ class ViewCog(commands.Cog, name="View Commands"): Args: interaction (nextcord.Integration): User's interaction with the command. """ - # might take a while depending on number of presences await interaction.response.defer(ephemeral=True) @@ -141,7 +140,6 @@ class ViewCog(commands.Cog, name="View Commands"): interaction (nextcord.Interaction): User's interaction with the command. election_name (str): Is SlashOption. The name of the election to view votes for. """ - stmt = select(Votes).where(Votes.ElectionCycle == election_name).where(Votes.Type == election_type) with Session(engine) as session: @@ -170,7 +168,6 @@ class ViewCog(commands.Cog, name="View Commands"): interaction (nextcord.Interaction): Interaction with command. election_name (str): String to run autocomplete on. """ - with Session(engine) as session: stmt = select(Votes.ElectionCycle).distinct() elections = session.scalars(stmt) @@ -194,7 +191,6 @@ class ViewCog(commands.Cog, name="View Commands"): interaction (nextcord.Interaction): Interaction with command. quarter_name (str): String to run autocomplete on. """ - with Session(engine) as session: stmt = select(ParticipationPoints.Cycle).distinct() quarters = session.scalars(stmt) diff --git a/cogs/loops/__init__.py b/cogs/loops/__init__.py index dc7c70d..3b1e442 100644 --- a/cogs/loops/__init__.py +++ b/cogs/loops/__init__.py @@ -12,6 +12,5 @@ def setup(bot: commands.Bot): """ Adds functions to bot. """ - bot.add_cog(BotPresence(bot)) bot.add_cog(DailyLoop(bot)) diff --git a/cogs/loops/bot_presence.py b/cogs/loops/bot_presence.py index ada7f3d..6dc3f30 100644 --- a/cogs/loops/bot_presence.py +++ b/cogs/loops/bot_presence.py @@ -35,7 +35,6 @@ class BotPresence(commands.Cog, name="Bot Presence"): async def change_presence(self): """Randomly chooses a presence and updates bot's presence.""" - with Session(engine) as session: presence = session.scalar(select(Presences.Presence).order_by(func.random())) diff --git a/cogs/loops/daily_loop.py b/cogs/loops/daily_loop.py index 4dd1dd0..ccc19c4 100644 --- a/cogs/loops/daily_loop.py +++ b/cogs/loops/daily_loop.py @@ -38,7 +38,6 @@ class DailyLoop(commands.Cog, name="Daily Loop"): an active election, runs the check_date() function for the election to perform actions. Finally checks if the election is over to delete the current election object. """ - if self.current_election is None: self.current_election = self.get_election_cycle() @@ -61,7 +60,6 @@ class DailyLoop(commands.Cog, name="Daily Loop"): """ Actions run before the loop starts. Want to wait for bot to be ready and get the current cycle. """ - await self.bot.wait_until_ready() # Bot needs to know guilds to get the current cycle. @@ -75,7 +73,6 @@ class DailyLoop(commands.Cog, name="Daily Loop"): Returns: ElectionCycle: Current election cycle, None if there is no election happening. """ - config: DakinyConfig = self.bot.config cycle = get_current_election_cycle(config) @@ -108,7 +105,6 @@ class DailyLoop(commands.Cog, name="Daily Loop"): Loops through the guild's humans checking for joined_at dates that match today's date. Sends a message saying happy cakeday. """ - today = datetime.today() guild: nextcord.Guild = self.bot.get_guild(self.bot.config.params.guild_id) announce_channel: nextcord.TextChannel = guild.get_channel(self.bot.config.params.general_channel_id) diff --git a/cogs/message_listener/__init__.py b/cogs/message_listener/__init__.py index 38d745e..c073f17 100644 --- a/cogs/message_listener/__init__.py +++ b/cogs/message_listener/__init__.py @@ -38,6 +38,5 @@ async def birthday_action(message: nextcord.Message) -> None: Args: message (nextcord.Message): The message to check for a birthday person. """ - if message.author.id in birthday_list or bool(set(message.raw_mentions) & set(birthday_list)): await message.add_reaction("🎂") diff --git a/database_setup/create_database.py b/database_setup/create_database.py index 7b975ca..912f729 100644 --- a/database_setup/create_database.py +++ b/database_setup/create_database.py @@ -9,7 +9,7 @@ if not exists("database/dakiny.db"): with sqlite3.connect("database/dakiny.db") as con: cur = con.cursor() - with open("database_setup/database.sql", mode="r", encoding="utf-8") as file: + with open("database_setup/database.sql", encoding="utf-8") as file: database_script = file.read() cur.executescript(database_script) con.commit() diff --git a/main.py b/main.py index 49cd47e..36e4d99 100644 --- a/main.py +++ b/main.py @@ -7,12 +7,12 @@ import nextcord from hydra.core.config_store import ConfigStore from nextcord.ext import commands -import database_setup.create_database as create_database from classes.nominate_highcouncil_views import NominateHighCouncilView from classes.nominate_views import NominateView from classes.voting_highcouncil_views import VoteHighcouncilView from classes.voting_views import VoteView from config import DakinyConfig +from database_setup import create_database cs = ConfigStore.instance() cs.store(name="dakiny_config", node=DakinyConfig) diff --git a/pyproject.toml b/pyproject.toml index 7cdc9f1..fd86fb0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -57,7 +57,7 @@ line-length = 120 indent-width = 4 # Assume Python 3.8 -target-version = "py38" +target-version = "py311" [tool.ruff.lint] # Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default. @@ -91,6 +91,9 @@ unfixable = [] # Allow unused variables when underscore-prefixed. dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" +[tool.ruff.lint.pydocstyle] +convention = "google" + [tool.ruff.format] # Like Black, use double quotes for strings. quote-style = "double" diff --git a/utils.py b/utils.py index 389a478..a0f5aa5 100644 --- a/utils.py +++ b/utils.py @@ -3,9 +3,9 @@ File contains util functions. """ import os +from collections.abc import Sequence from datetime import datetime from enum import StrEnum -from typing import Sequence import nextcord from sqlalchemy import select, true, update @@ -63,7 +63,6 @@ def get_election_name(config: DakinyConfig) -> str: Returns: str: the name of the election cycle """ - cycle = get_current_election_cycle(config) current_cycle = config.election_cycles[cycle] return f"vote-{get_datetime(current_cycle.term_start).year}-{current_cycle.name}" @@ -79,7 +78,6 @@ def get_quarter_name(config: DakinyConfig) -> str: Returns: str: the name of the cycle. """ - cycle = get_quarter_cycle(config) current_cycle = config.election_cycles[cycle] @@ -96,7 +94,6 @@ def get_current_election_cycle(config: DakinyConfig) -> int: Returns: int: cycle index for active election. None for no active cycle """ - today = datetime.today() for cycle, election in enumerate(config.election_cycles): @@ -120,7 +117,6 @@ def get_quarter_cycle(config: DakinyConfig) -> int: Returns: int: cycle index for current cycle. """ - today = datetime.today() for cycle, election in enumerate(config.election_cycles): if ( @@ -143,7 +139,6 @@ def build_votes_str(config: DakinyConfig, guild: nextcord.Guild) -> str: Returns: str: String that contains all nominees and their votes. """ - votes: dict[int, int] = {} election_name = get_election_name(config) @@ -180,7 +175,6 @@ def build_highcouncil_votes_str(config: DakinyConfig, guild: nextcord.Guild) -> str: String that contains all nominees and their votes. guild (nextcord.Guild): The guild object. Used to get display names for sorting. """ - cl_votes: dict[int, int] = {} dm_votes: dict[int, int] = {} @@ -315,7 +309,6 @@ def record_event_points(config: DakinyConfig, users: Sequence[str], is_member: b is_member (bool): whether the users are a discord member or not points (int): number of points to add """ - quarter_name = get_quarter_name(config) with Session(engine) as session: for user in users: @@ -395,7 +388,6 @@ async def update_clan_messages(guild: nextcord.Guild, channel_updates: Sequence[ guild (nextcord.Guild): Guild object that all channels and messages belong to. channel_updates: (Sequence[str]): a sequence of the channels to update. """ - dirs = [dir_name for dir_name in channel_updates if os.path.isdir(f"{MESSAGES_DIR}/{dir_name}")] with Session(engine) as session: @@ -410,7 +402,7 @@ async def update_clan_messages(guild: nextcord.Guild, channel_updates: Sequence[ for file_name in sorted_files: message_name = f"{dir_name}/{file_name}" file_path = f"{dir_path}/{file_name}" - with open(file_path, "r", encoding="utf8") as reader: + with open(file_path, encoding="utf8") as reader: await edit_clan_message(session, channel, message_name, reader.read(2000)) @@ -536,7 +528,7 @@ async def edit_clan_council_info(guild: nextcord.Guild, config: DakinyConfig): quarter = get_quarter_name(config) - with open(f"{TEMPLATES_DIR}/council_members.txt", mode="r", encoding="utf8") as file: + with open(f"{TEMPLATES_DIR}/council_members.txt", encoding="utf8") as file: message = file.read() message = message.format( quarter=quarter,