Run ruff linter with autofixing.

This commit is contained in:
2024-05-22 15:18:44 -04:00
parent 796b052bfc
commit ccbe86623a
22 changed files with 22 additions and 72 deletions
+4 -14
View File
@@ -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 "
+7 -9
View File
@@ -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
-4
View File
@@ -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(
-2
View File
@@ -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(
+1 -4
View File
@@ -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(
+1 -3
View File
@@ -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(
-1
View File
@@ -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
-1
View File
@@ -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())
-2
View File
@@ -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]
-2
View File
@@ -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)}
-3
View File
@@ -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()
-1
View File
@@ -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)
-1
View File
@@ -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),
-4
View File
@@ -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)
-1
View File
@@ -12,6 +12,5 @@ def setup(bot: commands.Bot):
"""
Adds functions to bot.
"""
bot.add_cog(BotPresence(bot))
bot.add_cog(DailyLoop(bot))
-1
View File
@@ -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()))
-4
View File
@@ -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)
-1
View File
@@ -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("🎂")
+1 -1
View File
@@ -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()
+1 -1
View File
@@ -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)
+4 -1
View File
@@ -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"
+3 -11
View File
@@ -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,