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
from nextcord.ext import commands
from sqlalchemy import select
from sqlalchemy.orm import Session
from sqlmodel import col
# pylint: disable=import-error
from database.db import Guild, engine
from database.db import engine
from database.models import Guild
class GuildListener(commands.Cog, name="Guild Listener"):
@@ -18,7 +17,7 @@ class GuildListener(commands.Cog, name="Guild Listener"):
self.bot = bot
@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.
@@ -26,12 +25,12 @@ class GuildListener(commands.Cog, name="Guild Listener"):
guild (nextcord.Guild): Guild that was joined.
"""
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)
# just in case check if the guild already exists
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)
else:
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}"!')
@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.
@@ -53,7 +52,7 @@ class GuildListener(commands.Cog, name="Guild Listener"):
guild (nextcord.Guild): Guild that was left.
"""
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.Active = False
session.commit()
@@ -66,6 +65,6 @@ class GuildListener(commands.Cog, name="Guild Listener"):
# This function will be called when this extension is loaded.
# It is necessary to add these functions to the bot.
def setup(bot: commands.Bot):
"""Adds function to bot"""
def setup(bot: commands.Bot) -> None:
"""Adds function to bot."""
bot.add_cog(GuildListener(bot))