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
+3 -7
View File
@@ -1,6 +1,4 @@
"""
Initializing code for all loops.
"""
"""Initializing code for all loops."""
from nextcord.ext import commands
@@ -8,9 +6,7 @@ from cogs.loops.bot_presence import BotPresence
from cogs.loops.daily_loop import DailyLoop
def setup(bot: commands.Bot):
"""
Adds functions to bot.
"""
def setup(bot: commands.Bot) -> None:
"""Adds functions to bot."""
bot.add_cog(BotPresence(bot))
bot.add_cog(DailyLoop(bot))
+6 -9
View File
@@ -1,6 +1,4 @@
"""
Functions for controlling the bot's presence. Also contains the loop.
"""
"""Functions for controlling the bot's presence. Also contains the loop."""
import nextcord
from nextcord.ext import commands, tasks
@@ -17,23 +15,22 @@ class BotPresence(commands.Cog, name="Bot Presence"):
self.bot = bot
@commands.Cog.listener()
async def on_guild_join(self, guild: nextcord.Guild): # pylint: disable=unused-argument
async def on_guild_join(self, guild: nextcord.Guild) -> None: # noqa: ARG002
"""Listener for Guild Join. Updated presence on guild join."""
await self.change_presence()
@commands.Cog.listener()
async def on_ready(self):
async def on_ready(self) -> None:
"""Starts the loop when bot is ready."""
await self.bot.wait_until_ready()
# pylint doesn't recognize that periodic_change_presense is subclassed from tasks.loop.
self.periodic_change_presence.start() # pylint: disable=no-member
self.periodic_change_presence.start()
@tasks.loop(minutes=5)
async def periodic_change_presence(self):
async def periodic_change_presence(self) -> None:
"""Loops every 5 minutes and changes presence."""
await self.change_presence()
async def change_presence(self):
async def change_presence(self) -> None:
"""Randomly chooses a presence and updates bot's presence."""
with Session(engine) as session:
presence = session.scalar(select(Presences.Presence).order_by(func.random()))
+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)