From 90131f6395cf21ec36628f12849b9c634fb5068b Mon Sep 17 00:00:00 2001 From: imcompiling Date: Tue, 28 Mar 2023 15:06:48 -0400 Subject: [PATCH] Add message to myself on join. Edit adding guild just in case they already exist in database. --- cogs/guild_listener/__init__.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/cogs/guild_listener/__init__.py b/cogs/guild_listener/__init__.py index 25e5197..8954d4f 100644 --- a/cogs/guild_listener/__init__.py +++ b/cogs/guild_listener/__init__.py @@ -4,6 +4,7 @@ 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 # pylint: disable=import-error @@ -20,12 +21,23 @@ class GuildListener(commands.Cog, name="Guild Listener"): async def on_guild_join(self, guild: nextcord.Guild): """Listener for Guild Join. Adds guild to database.""" - new_guild = Guild(GuildId=guild.id, GuildName=guild.name) - with Session(engine) as session: - session.add(new_guild) + stmt = select(Guild).where(Guild.GuildId == 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) + session.add(new_guild) + else: + guild_data.GuildName = guild.name + session.commit() + # let imcompiling#2272 know about a new server joining! + imcompiling: nextcord.User = await self.bot.fetch_user(206602713511886849) + await imcompiling.send(f'Agon has joined server "{guild.name}"!') + # This function will be called when this extension is loaded. # It is necessary to add these functions to the bot.