Fix smells and model usage in guild listener.

This commit is contained in:
2025-03-03 23:40:49 -05:00
parent f43dccb12d
commit b04819aa36
+11 -12
View File
@@ -1,14 +1,13 @@
""" """Defines cog for guild listeners. Contains functions for various guild events."""
Defines cog for guild listeners. Contains functions for various guild events.
"""
import nextcord import nextcord
from nextcord.ext import commands from nextcord.ext import commands
from sqlalchemy import select from sqlalchemy import select
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from sqlmodel import col
# pylint: disable=import-error from database.db import engine
from database.db import Guild, engine from database.models import Guild
class GuildListener(commands.Cog, name="Guild Listener"): class GuildListener(commands.Cog, name="Guild Listener"):
@@ -18,7 +17,7 @@ class GuildListener(commands.Cog, name="Guild Listener"):
self.bot = bot self.bot = bot
@commands.Cog.listener() @commands.Cog.listener()
async def on_guild_join(self, guild: nextcord.Guild): async def on_guild_join(self, guild: nextcord.Guild) -> None:
""" """
Listener for Guild Join. Adds guild to database. Listener for Guild Join. Adds guild to database.
@@ -26,12 +25,12 @@ class GuildListener(commands.Cog, name="Guild Listener"):
guild (nextcord.Guild): Guild that was joined. guild (nextcord.Guild): Guild that was joined.
""" """
with Session(engine) as session: with Session(engine) as session:
stmt = select(Guild).where(Guild.GuildId == guild.id) stmt = select(Guild).where(col(Guild.id == guild.id))
guild_data = session.scalar(stmt) guild_data = session.scalar(stmt)
# just in case check if the guild already exists # just in case check if the guild already exists
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
@@ -45,7 +44,7 @@ class GuildListener(commands.Cog, name="Guild Listener"):
await imcompiling.send(f'Agon has joined server "{guild.name}"!') await imcompiling.send(f'Agon has joined server "{guild.name}"!')
@commands.Cog.listener() @commands.Cog.listener()
async def on_guild_remove(self, guild: nextcord.Guild): async def on_guild_remove(self, guild: nextcord.Guild) -> None:
""" """
Listens to when the bot has been removed from a guild. Update's the database to record the active flag as false. Listens to when the bot has been removed from a guild. Update's the database to record the active flag as false.
@@ -53,7 +52,7 @@ class GuildListener(commands.Cog, name="Guild Listener"):
guild (nextcord.Guild): Guild that was left. guild (nextcord.Guild): Guild that was left.
""" """
with Session(engine) as session: with Session(engine) as session:
stmt = select(Guild).where(Guild.GuildId == guild.id) stmt = select(Guild).where(col(Guild.id == guild.id))
guild_data = session.scalar(stmt) guild_data = session.scalar(stmt)
guild_data.Active = False guild_data.Active = False
session.commit() session.commit()
@@ -66,6 +65,6 @@ class GuildListener(commands.Cog, name="Guild Listener"):
# 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(GuildListener(bot)) bot.add_cog(GuildListener(bot))