From f71f81fa85251fd1ef18f9c5775c74dc6cf32459 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Sun, 12 Jul 2020 22:13:43 +0100 Subject: [PATCH] Inserting New Members into Database Deleting Members that have left from the Database --- main.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/main.py b/main.py index 32df7963..f49b3250 100644 --- a/main.py +++ b/main.py @@ -114,6 +114,23 @@ async def on_member_join(member): if guild.id != enso_guild_ID: return else: + + try: + with db.connection() as conn: + name = f"{member.name}#{member.discriminator}" + # Define the Insert Into Statement inserting into the database + insert_query = """INSERT INTO members (discordID, discordUser) VALUES (?, ?)""" + vals = member.id, name + cursor = conn.cursor() + + # Execute the SQL Query + cursor.execute(insert_query, vals) + conn.commit() + print(cursor.rowcount, "Record inserted successfully into Members") + + except mariadb.Error as ex: + print("Parameterized Query Failed: {}".format(ex)) + # Set the channel id to "newpeople" new_people = guild.get_channel(enso_newpeople_ID) @@ -145,6 +162,32 @@ async def on_member_join(member): await new_people.send(embed=embed) +# Bot event for new member joining, sending an embed introducing them to the server +@client.event +async def on_member_remove(member): + # Get the guild + guild = member.guild + + try: + # Make sure the guild is Enso + if guild.id != enso_guild_ID: + return + else: + with db.connection() as conn: + # Define the Insert Into Statement inserting into the database + insert_query = """DELETE FROM members WHERE discordID = (?)""" + val = member.id, + cursor = conn.cursor() + + # Execute the SQL Query + cursor.execute(insert_query, val) + conn.commit() + print(cursor.rowcount, "Record Deleted successfully from Members") + + except mariadb.Error as ex: + print("Parameterized Query Failed: {}".format(ex)) + + # Bot Event for handling all errors within discord.commands @client.event async def on_command_error(ctx, args2):