From 36ba4d35e0ed1da695f957320c9500762c1f9e39 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Wed, 29 Mar 2023 20:13:39 -0400 Subject: [PATCH] Update table definition. Add on_guild_remove listener. --- cogs/guild_listener/__init__.py | 28 +++++++++++++++++++++++++++- highscores_database.sql | 1 + 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/cogs/guild_listener/__init__.py b/cogs/guild_listener/__init__.py index a7d6e05..5e4f2fa 100644 --- a/cogs/guild_listener/__init__.py +++ b/cogs/guild_listener/__init__.py @@ -19,7 +19,12 @@ class GuildListener(commands.Cog, name="Guild Listener"): @commands.Cog.listener() async def on_guild_join(self, guild: nextcord.Guild): - """Listener for Guild Join. Adds guild to database.""" + """ + Listener for Guild Join. Adds guild to database. + + Args: + guild (nextcord.Guild): Guild that was joined. + """ with Session(engine) as session: stmt = select(Guild).where(Guild.GuildId == guild.id) @@ -31,6 +36,7 @@ class GuildListener(commands.Cog, name="Guild Listener"): session.add(new_guild) else: guild_data.GuildName = guild.name + guild_data.Active = True session.commit() @@ -39,6 +45,26 @@ class GuildListener(commands.Cog, name="Guild Listener"): imcompiling: nextcord.User = await self.bot.fetch_user(206602713511886849) await imcompiling.send(f'Agon has joined server "{guild.name}"!') + @commands.Cog.listener() + async def on_guild_remove(self, guild: nextcord.Guild): + """ + Listens to when the bot has been removed from a guild. Update's the database to record the active flag as false. + + Args: + guild (nextcord.Guild): Guild that was left. + """ + + with Session(engine) as session: + stmt = select(Guild).where(Guild.GuildId == guild.id) + guild_data = session.scalar(stmt) + guild_data.Active = False + session.commit() + + # let imcompiling#2272 know about leaving a server. + # staying with fetch due to intents not being set up correctly. Maybe change to get eventually. + imcompiling: nextcord.User = await self.bot.fetch_user(206602713511886849) + await imcompiling.send(f'Agon has been removed from "{guild.name}".') + # This function will be called when this extension is loaded. # It is necessary to add these functions to the bot. diff --git a/highscores_database.sql b/highscores_database.sql index 992f43f..4453b35 100644 --- a/highscores_database.sql +++ b/highscores_database.sql @@ -6,6 +6,7 @@ CREATE TABLE Guilds( GuildName varchar(95) not null, SubmissionChannel bigint unsigned, VerificationChannel bigint unsigned, + Active boolean DEFAULT true, PRIMARY KEY (GuildId) );