From 8739ad49d58bf234830a4dd1ab349b7f9e1ff229 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Tue, 14 Jul 2020 23:34:59 +0100 Subject: [PATCH] Delete user data when the bot leaves the guild --- main.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 8bb73f52..c20a3d4f 100644 --- a/main.py +++ b/main.py @@ -100,6 +100,25 @@ async def ping(ctx): await ctx.send(f'Pong! `{round(client.latency * 1000)}ms`') +# Bot event for the bot leaving a guild, deleted all users stored in the database +@client.event +async def on_guild_remove(guild): + try: + with db.connection() as conn: + for member in guild.members: + # Delete the record of the member as they leave the server + delete_query = """DELETE FROM members WHERE discordID = (?) AND guildID = (?)""" + vals = member.id, guild.id, + cursor = conn.cursor() + + # Execute the SQL Query + cursor.execute(delete_query, vals) + conn.commit() + print(cursor.rowcount, "Record deleted successfully from Members") + except mariadb.Error as ex: + print("Parameterized Query Failed: {}".format(ex)) + + # Bot event for the bot joining a new guild, storing all users in the database @client.event async def on_guild_join(guild): @@ -188,7 +207,7 @@ async def on_member_remove(member): # Delete the record of the member as they leave the server delete_query = """DELETE FROM members WHERE discordID = (?) AND guildID = (?)""" - vals = member.id, guild.id + vals = member.id, guild.id, cursor = conn.cursor() # Execute the SQL Query