Compare commits

..
Author SHA1 Message Date
renovate-bot 3f37887345 Update dependency setuptools to v80 2025-05-18 04:01:56 +00:00
renovate-bot 886ac2f7ee Update ghcr.io/astral-sh/uv Docker tag to v0.7.5
build_latest / build (push) Successful in 47s
2025-05-18 04:01:21 +00:00
renovate-bot f06033d2ec Update ghcr.io/astral-sh/uv Docker tag to v0.7.3
build_latest / build (push) Successful in 52s
2025-05-11 04:01:29 +00:00
renovate-bot 2ffc4126d2 Update ghcr.io/astral-sh/uv Docker tag to v0.7.2
build_latest / build (push) Successful in 52s
2025-05-04 04:01:47 +00:00
renovate-bot 4280ddd841 Update ghcr.io/astral-sh/uv Docker tag to v0.6.17
build_latest / build (push) Successful in 39s
2025-04-27 04:01:16 +00:00
23 changed files with 470 additions and 565 deletions
+1 -1
View File
@@ -4,7 +4,7 @@ FROM python:3.13.3-slim
RUN apt-get update && apt-get -y upgrade && apt-get -y install git sqlite3 RUN apt-get update && apt-get -y upgrade && apt-get -y install git sqlite3
# install uv # install uv
COPY --from=ghcr.io/astral-sh/uv:0.6.14 /uv /uvx /bin/ COPY --from=ghcr.io/astral-sh/uv:0.7.5 /uv /uvx /bin/
COPY . /home COPY . /home
@@ -83,4 +83,4 @@ class DakinySettings(YAMLWizard):
self.to_yaml_file(CONFIG_LOCATION) self.to_yaml_file(CONFIG_LOCATION)
settings: DakinySettings | None = None settings = DakinySettings.from_yaml_file(CONFIG_LOCATION)
-6
View File
@@ -1,7 +1 @@
"""Empty init file for dakiny_bot module.""" """Empty init file for dakiny_bot module."""
from dakiny_bot.dakiny_settings import CONFIG_LOCATION, DakinySettings, settings
if settings is None:
tmp = DakinySettings.from_yaml_file(CONFIG_LOCATION)
settings = tmp[0] if isinstance(tmp, list) else tmp
+1 -1
View File
@@ -12,7 +12,7 @@ from dakiny_bot.classes.voting_highcouncil_views import VoteHighcouncilView
from dakiny_bot.classes.voting_views import VoteView from dakiny_bot.classes.voting_views import VoteView
from dakiny_bot.database import create_database from dakiny_bot.database import create_database
from .dakiny_settings import settings from .DakinySettings import settings
birthday_list: list[int] = [] birthday_list: list[int] = []
+9 -8
View File
@@ -5,28 +5,29 @@ from datetime import UTC, datetime, timedelta
from pathlib import Path from pathlib import Path
import nextcord import nextcord
from sqlalchemy import select, true, update
from sqlalchemy.orm import Session
from dakiny_bot.classes.nominate_highcouncil_views import NominateHighCouncilView from dakiny_bot.classes.nominate_highcouncil_views import NominateHighCouncilView
from dakiny_bot.classes.nominate_views import NominateView from dakiny_bot.classes.nominate_views import NominateView
from dakiny_bot.classes.voting_highcouncil_views import VoteHighcouncilView from dakiny_bot.classes.voting_highcouncil_views import VoteHighcouncilView
from dakiny_bot.classes.voting_views import VoteView from dakiny_bot.classes.voting_views import VoteView
from dakiny_bot.dakiny_settings import settings from dakiny_bot.DakinySettings import settings
from dakiny_bot.database.sql_data_classes import ( from dakiny_bot.database.sql_data_classes import (
CouncilMembers, CouncilMembers,
ElectionInformation, ElectionInformation,
Votes, Votes,
engine, engine,
) )
from dakiny_bot.election_utils import get_datetime
from dakiny_bot.utils import ( from dakiny_bot.utils import (
TEMPLATES_DIR, TEMPLATES_DIR,
VoteType, VoteType,
build_highcouncil_votes_str, build_highcouncil_votes_str,
build_votes_str, build_votes_str,
edit_clan_council_info, edit_clan_council_info,
get_datetime,
get_election_name, get_election_name,
) )
from sqlalchemy import select, true, update
from sqlalchemy.orm import Session
REQUIRED_VOTES_TO_BE_ELECTED = 3 REQUIRED_VOTES_TO_BE_ELECTED = 3
@@ -69,7 +70,7 @@ class Election:
list[str]: A list of message contents in the correct order. Must be a string because the message is too list[str]: A list of message contents in the correct order. Must be a string because the message is too
long. long.
""" """
with Path(f"{TEMPLATES_DIR}/election_announcement.txt").open() as file: with Path(f"{TEMPLATES_DIR}/election_announcement.txt", encoding="utf8").open() as file:
message = file.read() message = file.read()
next_election = get_datetime( next_election = get_datetime(
@@ -97,7 +98,7 @@ class Election:
Returns: Returns:
str: Message to send. str: Message to send.
""" """
with Path(f"{TEMPLATES_DIR}/highcouncil_election.txt").open() as file: with Path(f"{TEMPLATES_DIR}/highcouncil_election.txt", encoding="utf8").open() as file:
message = file.read() message = file.read()
return message.format( return message.format(
@@ -115,7 +116,7 @@ class Election:
Returns: Returns:
str: Message to send. str: Message to send.
""" """
with Path(f"{TEMPLATES_DIR}/highcouncil_vote.txt").open() as file: with Path(f"{TEMPLATES_DIR}/highcouncil_vote.txt", encoding="utf8").open() as file:
message = file.read() message = file.read()
return message.format( return message.format(
@@ -130,7 +131,7 @@ class Election:
Returns: Returns:
str: Message to send str: Message to send
""" """
with Path(f"{TEMPLATES_DIR}/vote_start.txt").open() as file: with Path(f"{TEMPLATES_DIR}/vote_start.txt", encoding="utf8").open() as file:
message = file.read() message = file.read()
vote_end: datetime = get_datetime(settings.election_cycles[self.cycle].voting_end) vote_end: datetime = get_datetime(settings.election_cycles[self.cycle].voting_end)
@@ -1,7 +1,11 @@
"""Contains various views to be used in the high council nomination process.""" """Contains various views to be used in the high council nomination process."""
import nextcord import nextcord
from dakiny_bot.dakiny_settings import settings from nextcord.interactions import Interaction
from sqlalchemy import select
from sqlalchemy.orm import Session
from dakiny_bot.DakinySettings import settings
from dakiny_bot.database.sql_data_classes import CouncilMembers, engine from dakiny_bot.database.sql_data_classes import CouncilMembers, engine
from dakiny_bot.utils import ( from dakiny_bot.utils import (
NOMINEE_SPLITTER, NOMINEE_SPLITTER,
@@ -9,9 +13,6 @@ from dakiny_bot.utils import (
decorate_dm_nomination, decorate_dm_nomination,
get_election_name, get_election_name,
) )
from nextcord.interactions import Interaction
from sqlalchemy import select
from sqlalchemy.orm import Session
class NominateDMModal(nextcord.ui.Modal): class NominateDMModal(nextcord.ui.Modal):
+4 -3
View File
@@ -1,13 +1,14 @@
"""Contains various views to be used in the nomination process.""" """Contains various views to be used in the nomination process."""
import nextcord import nextcord
from dakiny_bot.dakiny_settings import settings
from dakiny_bot.database.sql_data_classes import CouncilMembers, engine
from dakiny_bot.utils import NOMINEE_SPLITTER, decorate_nomination, get_election_name
from nextcord.interactions import Interaction from nextcord.interactions import Interaction
from sqlalchemy import select from sqlalchemy import select
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from dakiny_bot.DakinySettings import settings
from dakiny_bot.database.sql_data_classes import CouncilMembers, engine
from dakiny_bot.utils import NOMINEE_SPLITTER, decorate_nomination, get_election_name
class NominateModal(nextcord.ui.Modal): class NominateModal(nextcord.ui.Modal):
""" """
@@ -1,7 +1,12 @@
"""Contains various views to be used while voting for high council. Controls the logic when performing the vote.""" """Contains various views to be used while voting for high council. Controls the logic when performing the vote."""
import nextcord import nextcord
from dakiny_bot.dakiny_settings import settings from nextcord import SelectOption
from nextcord.interactions import Interaction
from sqlalchemy import exc, select, update
from sqlalchemy.orm import Session
from dakiny_bot.DakinySettings import settings
from dakiny_bot.database.sql_data_classes import ( from dakiny_bot.database.sql_data_classes import (
CouncilMembers, CouncilMembers,
ElectionInformation, ElectionInformation,
@@ -9,10 +14,6 @@ from dakiny_bot.database.sql_data_classes import (
engine, engine,
) )
from dakiny_bot.utils import VoteType, build_highcouncil_votes_str, get_election_name from dakiny_bot.utils import VoteType, build_highcouncil_votes_str, get_election_name
from nextcord import SelectOption
from nextcord.interactions import Interaction
from sqlalchemy import exc, select, update
from sqlalchemy.orm import Session
class VoteSelect(nextcord.ui.StringSelect): class VoteSelect(nextcord.ui.StringSelect):
+6 -5
View File
@@ -1,7 +1,12 @@
"""Contains various views to be used while voting. Controls the logic when performing the vote.""" """Contains various views to be used while voting. Controls the logic when performing the vote."""
import nextcord import nextcord
from dakiny_bot.dakiny_settings import settings from nextcord import SelectOption
from nextcord.interactions import Interaction
from sqlalchemy import exc, select, update
from sqlalchemy.orm import Session
from dakiny_bot.DakinySettings import settings
from dakiny_bot.database.sql_data_classes import ( from dakiny_bot.database.sql_data_classes import (
CouncilMembers, CouncilMembers,
ElectionInformation, ElectionInformation,
@@ -9,10 +14,6 @@ from dakiny_bot.database.sql_data_classes import (
engine, engine,
) )
from dakiny_bot.utils import VoteType, build_votes_str, get_election_name from dakiny_bot.utils import VoteType, build_votes_str, get_election_name
from nextcord import SelectOption
from nextcord.interactions import Interaction
from sqlalchemy import exc, select, update
from sqlalchemy.orm import Session
class VoteSelect(nextcord.ui.StringSelect): class VoteSelect(nextcord.ui.StringSelect):
+1 -1
View File
@@ -9,7 +9,7 @@ from git import Repo
from nextcord.ext import commands from nextcord.ext import commands
from dakiny_bot.classes.gitea_webhook import GiteaWebhook from dakiny_bot.classes.gitea_webhook import GiteaWebhook
from dakiny_bot.dakiny_settings import settings from dakiny_bot.DakinySettings import settings
from dakiny_bot.utils import REPO_DIR, edit_clan_council_info, update_clan_messages from dakiny_bot.utils import REPO_DIR, edit_clan_council_info, update_clan_messages
if TYPE_CHECKING: if TYPE_CHECKING:
+1 -1
View File
@@ -4,7 +4,7 @@ import nextcord
from discord import TextInputStyle from discord import TextInputStyle
from nextcord.ext import commands from nextcord.ext import commands
from dakiny_bot.dakiny_settings import settings from dakiny_bot.DakinySettings import settings
from dakiny_bot.utils import GUILDS, MAX_DISCORD_MESSAGE_LENGTH, PointType, record_points from dakiny_bot.utils import GUILDS, MAX_DISCORD_MESSAGE_LENGTH, PointType, record_points
+1 -1
View File
@@ -4,7 +4,7 @@ import nextcord
from nextcord.ext import commands from nextcord.ext import commands
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from dakiny_bot.dakiny_settings import settings from dakiny_bot.DakinySettings import settings
from dakiny_bot.database.sql_data_classes import Presences, engine from dakiny_bot.database.sql_data_classes import Presences, engine
+1 -1
View File
@@ -3,7 +3,7 @@
import nextcord import nextcord
from nextcord.ext import commands from nextcord.ext import commands
from dakiny_bot.dakiny_settings import settings from dakiny_bot.DakinySettings import settings
from dakiny_bot.utils import GUILDS from dakiny_bot.utils import GUILDS
+1 -1
View File
@@ -4,7 +4,7 @@ import nextcord
from nextcord.ext import application_checks, commands from nextcord.ext import application_checks, commands
from dakiny_bot.classes.election import Election from dakiny_bot.classes.election import Election
from dakiny_bot.dakiny_settings import settings from dakiny_bot.DakinySettings import settings
from dakiny_bot.utils import GUILDS, get_active_election_cycle, get_quarter_name from dakiny_bot.utils import GUILDS, get_active_election_cycle, get_quarter_name
ME = 206602713511886849 ME = 206602713511886849
+1 -1
View File
@@ -11,7 +11,7 @@ from sqlalchemy.orm import Session
from dakiny_bot.app import birthday_list from dakiny_bot.app import birthday_list
from dakiny_bot.classes.election import Election from dakiny_bot.classes.election import Election
from dakiny_bot.dakiny_settings import settings from dakiny_bot.DakinySettings import settings
from dakiny_bot.database.sql_data_classes import UserBirthday, engine from dakiny_bot.database.sql_data_classes import UserBirthday, engine
from dakiny_bot.utils import get_active_election_cycle from dakiny_bot.utils import get_active_election_cycle
-51
View File
@@ -1,51 +0,0 @@
"""Broken out util functions to avoid importing sql during testing."""
import calendar
from datetime import UTC, datetime
from dakiny_bot.dakiny_settings import settings
def get_datetime(month_day: str) -> datetime:
"""
Takes in a string month and day and returns a datetime object of that Month/Day this year.
Args:
month_day (str): Month/Day in that format.
Returns:
datetime: Datetime representation of Month/Day.
"""
today = datetime.now(tz=UTC).date()
year = today.year
month = int(month_day.split("/")[0])
day = int(month_day.split("/")[1])
# ensure that year rolls over if current month is December and the next month is less than 12.
if today.month == calendar.DECEMBER and month < calendar.OCTOBER:
year += 1
if today.month == calendar.JANUARY and month > calendar.MARCH:
year -= 1
return datetime(year, month, day, tzinfo=UTC)
def get_quarter_cycle() -> int | None:
"""
Returns the index for the current cycle/quarter. Defined as between term start and end.
Can return None if entire year isn't covered by cycles.
Returns:
int: cycle index for current cycle.
"""
today = datetime.now(tz=UTC)
for cycle, election in enumerate(settings.election_cycles):
if (
today.date() >= get_datetime(election.term_start).date()
and today.date() <= get_datetime(election.term_end).date()
):
return cycle
return None
-1
View File
@@ -1 +0,0 @@
"""Empty init for testing util functions."""
-1
View File
@@ -1 +0,0 @@
"""Empty init for testing util functions."""
@@ -1,41 +0,0 @@
"""Tests for get_datetime function."""
import datetime
from freezegun import freeze_time
from dakiny_bot.election_utils import get_datetime
@freeze_time("2025-6-16")
def test_regular_get_datetime() -> None:
"""
Test a regular get_datetime.
Expect the returned datetime to be the frozen date.
Have two different days to ensure that the date is being returned correctly.
"""
date_time = get_datetime("6/15")
assert date_time == datetime.datetime(2025, 6, 15, tzinfo=datetime.UTC)
@freeze_time("2025-1-16")
def test_jan_get_datetime() -> None:
"""
Test previous year get_datetime.
Frozen day is in January 2025, datetime string is in December. Expect datetime to be from 2024.
"""
date_time = get_datetime("12/15")
assert date_time == datetime.datetime(2024, 12, 15, tzinfo=datetime.UTC)
@freeze_time("2025-12-16")
def test_dec_get_datetime() -> None:
"""
Test next year get_datetime.
Frozen day is in December 2025, datetime string is in January. Expect datetime to be from 2026.
"""
date_time = get_datetime("1/15")
assert date_time == datetime.datetime(2026, 1, 15, tzinfo=datetime.UTC)
@@ -1,31 +0,0 @@
"""Test functions for get_quarter_cycle."""
from unittest.mock import mock_open, patch
from freezegun import freeze_time
from dakiny_bot.dakiny_settings import ChannelIds, DakinySettings, Election, Params, PointSettings, RoleIds
from dakiny_bot.election_utils import get_quarter_cycle
# I only care about term start and term end for this test
q1 = Election(True, "", "", "", "", "q1", "4/30", "2/1", "", "") # noqa: FBT003
q2 = Election(True, "", "", "", "", "q2", "7/31", "5/1", "", "") # noqa: FBT003
q3 = Election(True, "", "", "", "", "q3", "10/31", "8/1", "", "") # noqa: FBT003
q4 = Election(True, "", "", "", "", "q4", "1/31", "11/1", "", "") # noqa: FBT003
# Dummy all the values I don't care about.
settings = DakinySettings(
ChannelIds(1, 1, 1, 1, 1, 1),
[q1, q2, q3, q4],
Params("", "", 1),
PointSettings(1, 1, 1, 1, 1, True, True), # noqa: FBT003
RoleIds(1, 1, 1, 1, 1),
)
@patch("election_utils.get_quarter_cycle.settings", settings)
@patch("builtins.open", new_callable=mock_open)
class TestGetQuarterCycle:
@freeze_time("2025-2-25")
def test_regular_q1(self) -> None:
assert get_quarter_cycle() == 0
+50 -11
View File
@@ -1,5 +1,6 @@
"""File contains util functions.""" """File contains util functions."""
import calendar
import os import os
from collections.abc import Sequence from collections.abc import Sequence
from datetime import UTC, datetime from datetime import UTC, datetime
@@ -20,8 +21,7 @@ from dakiny_bot.database.sql_data_classes import (
engine, engine,
) )
from .dakiny_settings import settings from .DakinySettings import settings
from .election_utils import get_datetime, get_quarter_cycle
NOMINEE_SPLITTER = "========================================\n\n" NOMINEE_SPLITTER = "========================================\n\n"
REPO_DIR = "./dakiny-messages" REPO_DIR = "./dakiny-messages"
@@ -34,6 +34,31 @@ VoteType = StrEnum("VoteType", ["COUNCIL", "DISCORDMANAGER", "CLANLEADER"])
PointType = StrEnum("PointType", ["CLAN", "EVENT"]) PointType = StrEnum("PointType", ["CLAN", "EVENT"])
def get_datetime(day: str) -> datetime:
"""
Takes in a string month and day and returns a datetime object of that Month/Day this year.
Args:
day (str): Month/Day in that format.
Returns:
datetime: Datetime representation of Month/Day.
"""
today = datetime.now(tz=UTC).date()
year = today.year
month = int(day.split("/")[0])
day = int(day.split("/")[1])
# ensure that year rolls over if current month is December and the next month is less than 12.
if today.month == calendar.DECEMBER and month < calendar.OCTOBER:
year += 1
if today.month == calendar.JANUARY and month > calendar.MARCH:
year -= 1
return datetime(year, month, day, tzinfo=UTC)
def get_election_name() -> str: def get_election_name() -> str:
""" """
Builds the unique name of the current election cycle. Builds the unique name of the current election cycle.
@@ -41,10 +66,7 @@ def get_election_name() -> str:
Returns: Returns:
str: the name of the election cycle str: the name of the election cycle
""" """
if cycle := get_active_election_cycle() is None: cycle = get_active_election_cycle()
# undefined behavior
print("Did not find an active election cycle. Undefined behavior with return string.") # noqa: T201
return "No-active-election-cycle"
current_cycle = settings.election_cycles[cycle] current_cycle = settings.election_cycles[cycle]
return f"vote-{get_datetime(current_cycle.term_start).year}-{current_cycle.name}" return f"vote-{get_datetime(current_cycle.term_start).year}-{current_cycle.name}"
@@ -56,16 +78,13 @@ def get_quarter_name() -> str:
Returns: Returns:
str: the name of the cycle. str: the name of the cycle.
""" """
if cycle := get_quarter_cycle() is None: cycle = get_quarter_cycle()
# undefined behavior
print("Did not find a quarter cycle. Undefined behavior with return string.") # noqa: T201
return "no-quarter-cycle"
current_cycle = settings.election_cycles[cycle] current_cycle = settings.election_cycles[cycle]
return f"{get_datetime(current_cycle.term_start).year}-{current_cycle.name}" return f"{get_datetime(current_cycle.term_start).year}-{current_cycle.name}"
def get_active_election_cycle() -> int | None: def get_active_election_cycle() -> int:
""" """
Returns index for active election. An election is considered active between the election start and the term start. Returns index for active election. An election is considered active between the election start and the term start.
@@ -85,6 +104,26 @@ def get_active_election_cycle() -> int | None:
return get_quarter_cycle() return get_quarter_cycle()
def get_quarter_cycle() -> int:
"""
Returns the index for the current cycle/quarter. Defined as between term start and end.
Can return None if entire year isn't covered by cycles.
Returns:
int: cycle index for current cycle.
"""
today = datetime.now(tz=UTC)
for cycle, election in enumerate(settings.election_cycles):
if (
today.date() >= get_datetime(election.term_start).date()
and today.date() <= get_datetime(election.term_end).date()
):
return cycle
return None
def build_votes_str(guild: nextcord.Guild) -> str: def build_votes_str(guild: nextcord.Guild) -> str:
""" """
Creates a string that contains all nominees and their votes to be sent as a discord message. Creates a string that contains all nominees and their votes to be sent as a discord message.
+2 -10
View File
@@ -9,7 +9,7 @@ dependencies = [
"pytz<2025.3,>=2025.2", "pytz<2025.3,>=2025.2",
"sqlalchemy<3.0.0,>=2.0.30", "sqlalchemy<3.0.0,>=2.0.30",
"aiofiles<24.0.0,>=23.2.1", "aiofiles<24.0.0,>=23.2.1",
"setuptools<71.0.0,>=70.0.0", "setuptools<80.7.2,>=80.7.1",
"dataclass-wizard[yaml]>=0.22.3,<1.0.0", "dataclass-wizard[yaml]>=0.22.3,<1.0.0",
"uvicorn>=0.34.0", "uvicorn>=0.34.0",
] ]
@@ -19,12 +19,7 @@ description = "Dakiny's Discord Bot."
readme = "README.md" readme = "README.md"
[dependency-groups] [dependency-groups]
dev = [ dev = ["ruff<1.0.0,>=0.6.9"]
"freezegun>=1.5.1",
"pytest>=8.3.5",
"pytest-cov>=6.1.1",
"ruff<1.0.0,>=0.6.9",
]
[tool.ruff] [tool.ruff]
# Exclude a variety of commonly ignored directories. # Exclude a variety of commonly ignored directories.
@@ -131,6 +126,3 @@ docstring-code-format = true
# This only has an effect when the `docstring-code-format` setting is # This only has an effect when the `docstring-code-format` setting is
# enabled. # enabled.
docstring-code-line-length = "dynamic" docstring-code-line-length = "dynamic"
[tool.ruff.lint.per-file-ignores]
"**/{tests}/*" = ["ANN001", "ANN201", "S101", "PLR2004"]
Generated
+379 -379
View File
File diff suppressed because it is too large Load Diff