Rename module and add init file to conform to ruff.
build_develop / build (push) Successful in 21s

This commit is contained in:
2025-03-18 10:03:51 -04:00
parent 6af2476339
commit 34cd2f9975
14 changed files with 2 additions and 1 deletions
+12
View File
@@ -0,0 +1,12 @@
"""Initializing code for all loops."""
from nextcord.ext import commands
from cogs.loops.bot_presence import BotPresence
from cogs.loops.daily_loop import DailyLoop
def setup(bot: commands.Bot) -> None:
"""Add functions to bot."""
bot.add_cog(DailyLoop(bot))
bot.add_cog(BotPresence(bot))
@@ -0,0 +1,44 @@
"""Functions for controlling the bot's presence. Also contains the loop."""
import random
import nextcord
from nextcord.ext import commands, tasks
class BotPresence(commands.Cog, name="Bot Presence"):
"""Class for bot presence functions."""
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot
@commands.Cog.listener()
async def on_ready(self) -> None:
"""Start the loop when bot is ready."""
await self.bot.wait_until_ready()
self.periodic_change_presence.start()
@tasks.loop(minutes=30)
async def periodic_change_presence(self) -> None:
"""Loops every 30 minutes and changes presence."""
await self.change_presence()
async def change_presence(self) -> None:
"""Randomly chooses a presence and updates bot's presence."""
dungeons = [
"Cinderbrew Meadery",
"Darkflame Cleft",
"The Rookery",
"Priory of the Sacred Flame",
"Operation: Floodgate",
"The MOTHERLODE!!",
"Theater of Pain",
"Operation: Mechagon - Workshop",
]
dungeon = random.choice(dungeons) # noqa: S311
plus = random.randint(2, 12) # noqa: S311
presence = f"Running +{plus} {dungeon}"
await self.bot.change_presence(activity=nextcord.CustomActivity(presence))
+81
View File
@@ -0,0 +1,81 @@
"""Defines the daily loop that runs the Off Cooldown bot functions."""
from datetime import UTC, datetime, time
import nextcord
from nextcord.ext import commands, tasks
from pytz import timezone
from sqlalchemy import select
from sqlalchemy.orm import Session
from sqlmodel import col
from database.db import engine
from database.models import UserBirthday
from logger import ocd_logger
from main import birthday_list
from settings import settings
class DailyLoop(commands.Cog, name="Daily Loop"):
"""Class for performing daily functions."""
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot
self.current_election = None
self.update_birthdays()
self.daily_loop.start()
@tasks.loop(time=time(hour=0, tzinfo=timezone("UTC")))
async def daily_loop(self) -> None:
"""Run daily repeated functions."""
self.update_birthdays()
await self.cakedays()
@daily_loop.before_loop
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()
def update_birthdays(self) -> None:
"""Goes through database to collect all birthdays that are today. Adds user's id to birthday list."""
birthday_list.clear()
today = datetime.now(tz=UTC).date()
stmt = (
select(col(UserBirthday.user_id))
.where(col(UserBirthday.month) == today.month)
.where(col(UserBirthday.day) == today.day)
)
with Session(engine) as session:
to_add = session.scalars(stmt)
birthday_list.extend(to_add.all())
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.
"""
today = datetime.now(tz=UTC).date()
if (guild := self.bot.get_guild(settings.params.guild_id)) is None:
ocd_logger.warning("Guild not found in bot.")
return
if (announce_channel := guild.get_channel(settings.channel_ids.general)) is None or not isinstance(
announce_channel, nextcord.TextChannel
):
ocd_logger.warning("Set general channel isn't found or isn't a text channel.")
return
cakedays: list[nextcord.Member] = [
human
for human in guild.humans
if human.joined_at is not None and human.joined_at.month == today.month and human.joined_at.day == today.day
]
for member in cakedays:
years = today.year - member.joined_at.year # type: ignore reportOptionalMemberAccess
year_str = "years" if years > 1 else "year"
msg = f"Happy Cake day! <@{member.id}> joined the discord {years} {year_str} ago!"
await announce_channel.send(msg)