Start working on files in cogs.
build_develop / build (push) Successful in 1m24s

This commit is contained in:
2024-05-22 17:35:43 -04:00
parent d9914529ca
commit 6500f68d86
8 changed files with 95 additions and 109 deletions
+27 -25
View File
@@ -1,27 +1,29 @@
"""
Defines the daily loop that runs the Dakiny bot functions.
"""
"""Defines the daily loop that runs the Dakiny bot functions."""
import random
from datetime import UTC, datetime, time
from typing import TYPE_CHECKING
import nextcord
from nextcord.ext import commands, tasks
from pytz import timezone
from sqlalchemy import select
from sqlalchemy.orm import Session
from classes.election_cycle import ElectionCycle
from config import DakinyConfig
from database_setup.sql_data_classes import UserBirthday, engine
from main import birthday_list
from utils import get_current_election_cycle
if TYPE_CHECKING:
import nextcord
from config import DakinyConfig
KANDY = 239547993106284544
class DailyLoop(commands.Cog, name="Daily Loop"):
"""
Class for performing daily functions.
"""
"""Class for performing daily functions."""
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot
@@ -31,10 +33,12 @@ class DailyLoop(commands.Cog, name="Daily Loop"):
# pylint doesn't recognize that daily_loop is subclassed from tasks.loop.
self.daily_loop.start() # pylint: disable=no-member
@tasks.loop(time=time(hour=0, tzinfo=timezone("GMT")))
async def daily_loop(self):
@tasks.loop(time=time(hour=0, tzinfo=timezone("UTC")))
async def daily_loop(self) -> None:
"""
Runs daily. Checks if the current election is None. If so, checks if there is an active election. If there is
Runs daily.
Checks if the current election is None. If so, checks if there is an active election. If there is
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.
"""
@@ -48,18 +52,16 @@ class DailyLoop(commands.Cog, name="Daily Loop"):
self.update_birthdays()
if 239547993106284544 in birthday_list:
if KANDY in birthday_list:
# inform kandy that it is her "birthday"
kandy: nextcord.User = await self.bot.fetch_user(239547993106284544)
kandy: nextcord.User = await self.bot.fetch_user(KANDY)
await kandy.send('Happy "Birthday"!')
await self.cakedays()
@daily_loop.before_loop
async def before_loop(self):
"""
Actions run before the loop starts. Want to wait for bot to be ready and get the current cycle.
"""
async def before_loop(self) -> None:
"""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.
@@ -67,8 +69,9 @@ class DailyLoop(commands.Cog, name="Daily Loop"):
def get_election_cycle(self) -> ElectionCycle:
"""
Creates an object for the current election cycle if one is active. An election is considered active
between the election start and the term start.
Creates an object for the current election cycle if one is active.
An election is considered active between the election start and the term start.
Returns:
ElectionCycle: Current election cycle, None if there is no election happening.
@@ -83,13 +86,11 @@ class DailyLoop(commands.Cog, name="Daily Loop"):
return ElectionCycle(cycle, config, self.bot.get_guild(config.params.guild_id))
def update_birthdays(self) -> None:
"""
Goes through database to collect all birthdays that are today. Adds user's id to birthday list.
"""
"""Goes through database to collect all birthdays that are today. Adds user's id to birthday list."""
birthday_list.clear()
# kandy's 1% chance
if random.randint(0, 99) == 0:
if random.randint(0, 99) == 0: # noqa: S311 isn't cryptography related.
birthday_list.append(239547993106284544)
today = datetime.now(tz=UTC).date()
@@ -102,8 +103,9 @@ class DailyLoop(commands.Cog, name="Daily Loop"):
async def cakedays(self) -> None:
"""
Loops through the guild's humans checking for joined_at dates that match today's date. Sends a message
saying happy cakeday.
Loops through the guild's humans checking for joined_at dates that match today's date.
Sends a message saying happy cakeday.
"""
today = datetime.now(tz=UTC).date()
guild: nextcord.Guild = self.bot.get_guild(self.bot.config.params.guild_id)