Started fixing smells and updating model calls.

This commit is contained in:
2025-03-03 23:29:46 -05:00
parent 528af14e00
commit f6ae34c9e6
3 changed files with 15 additions and 24 deletions
+3 -5
View File
@@ -1,6 +1,4 @@
""" """Initializing code for all loops."""
Initializing code for all loops.
"""
from nextcord.ext import commands from nextcord.ext import commands
@@ -10,7 +8,7 @@ from .guild_data import GuildData
# This function will be called when this extension is loaded. # This function will be called when this extension is loaded.
# It is necessary to add these functions to the bot. # It is necessary to add these functions to the bot.
def setup(bot: commands.Bot): def setup(bot: commands.Bot) -> None:
"""Adds function to bot""" """Adds function to bot."""
bot.add_cog(BotPresence(bot)) bot.add_cog(BotPresence(bot))
bot.add_cog(GuildData(bot)) bot.add_cog(GuildData(bot))
+2 -4
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 random import random
@@ -11,7 +9,7 @@ from nextcord.ext import commands, tasks
class BotPresence(commands.Cog, name="Bot Presence"): class BotPresence(commands.Cog, name="Bot Presence"):
"""Class for bot presence functions.""" """Class for bot presence functions."""
def __init__(self, bot: commands.Bot): def __init__(self, bot: commands.Bot) -> None:
self.bot = bot self.bot = bot
@commands.Cog.listener() @commands.Cog.listener()
+10 -15
View File
@@ -1,13 +1,11 @@
""" """Contains a loop that updates guild data in the database periodically."""
Contains a loop that updates guild data in the database periodically.
"""
from nextcord.ext import commands, tasks from nextcord.ext import commands, tasks
from sqlalchemy import select from sqlalchemy import select
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
# pylint: disable=import-error from database.db import engine
from database.db import Guild, engine from database.models import Guild
class GuildData(commands.Cog, name="Guild Data"): class GuildData(commands.Cog, name="Guild Data"):
@@ -17,30 +15,27 @@ class GuildData(commands.Cog, name="Guild Data"):
self.bot = bot self.bot = bot
@commands.Cog.listener() @commands.Cog.listener()
async def on_ready(self): async def on_ready(self) -> None:
"""Starts the loop when bot is ready.""" """Starts the loop when bot is ready."""
await self.bot.wait_until_ready() await self.bot.wait_until_ready()
# pylint doesn't recognize that periodic_update_guild_data is subclassed from tasks.loop. await self.periodic_update_guild_data.start()
await self.periodic_update_guild_data.start() # pylint: disable=no-member
@tasks.loop(hours=24) @tasks.loop(hours=24)
async def periodic_update_guild_data(self): async def periodic_update_guild_data(self) -> None:
"""Loops every day, calls update_guild_data.""" """Loops every day, calls update_guild_data."""
self.update_guild_data() self.update_guild_data()
def update_guild_data(self): def update_guild_data(self) -> None:
""" """Updates all known guild's data in the database."""
Updates all known guild's data in the database.
"""
with Session(engine) as session: with Session(engine) as session:
for guild in self.bot.guilds: for guild in self.bot.guilds:
# update given guild name for guild id # update given guild name for guild id
stmt = select(Guild).where(Guild.GuildId == guild.id) stmt = select(Guild).where(Guild.id == guild.id)
guild_data = session.scalar(stmt) guild_data = session.scalar(stmt)
# just in case checking if the guild doesn't exist. # just in case checking if the guild doesn't exist.
if guild_data is None: if guild_data is None:
new_guild = Guild(GuildId=guild.id, GuildName=guild.name) new_guild = Guild(id=guild.id, name=guild.name)
session.add(new_guild) session.add(new_guild)
else: else:
guild_data.GuildName = guild.name guild_data.GuildName = guild.name