As user joins Enso, add them to a database

As user leaves Enso, delete them from a database
pull/8/head
sgoudham 4 years ago
parent a9503bd8c4
commit 6270ceb6d4

@ -113,53 +113,53 @@ async def on_member_join(member):
# Make sure the guild is Enso # Make sure the guild is Enso
if guild.id != enso_guild_ID: if guild.id != enso_guild_ID:
return return
else:
try: try:
with db.connection() as conn: # Set up connection to database
name = f"{member.name}#{member.discriminator}" with db.connection() as conn:
# Define the Insert Into Statement inserting into the database name = f"{member.name}#{member.discriminator}"
insert_query = """INSERT INTO members (discordID, discordUser) VALUES (?, ?)""" # Define the Insert Into Statement inserting into the database
vals = member.id, name insert_query = """INSERT INTO members (discordUser, discordID) VALUES (?, ?)"""
cursor = conn.cursor() vals = name, member.id
cursor = conn.cursor()
# Execute the SQL Query # Execute the SQL Query
cursor.execute(insert_query, vals) cursor.execute(insert_query, vals)
conn.commit() conn.commit()
print(cursor.rowcount, "Record inserted successfully into Members") print(cursor.rowcount, "Record inserted successfully into Members")
except mariadb.Error as ex: except mariadb.Error as ex:
print("Parameterized Query Failed: {}".format(ex)) print("Parameterized Query Failed: {}".format(ex))
# Set the channel id to "newpeople" # Set the channel id to "newpeople"
new_people = guild.get_channel(enso_newpeople_ID) new_people = guild.get_channel(enso_newpeople_ID)
# Set the enso server icon and the welcoming gif # Set the enso server icon and the welcoming gif
server_icon = guild.icon_url server_icon = guild.icon_url
welcome_gif = "https://cdn.discordapp.com/attachments/669808733337157662/730186321913446521/NewPeople.gif" welcome_gif = "https://cdn.discordapp.com/attachments/669808733337157662/730186321913446521/NewPeople.gif"
# Set up embed for the #newpeople channel # Set up embed for the #newpeople channel
embed = Embed(title="\n**Welcome To Ensō!**", embed = Embed(title="\n**Welcome To Ensō!**",
colour=enso_embedmod_colours, colour=enso_embedmod_colours,
timestamp=time) timestamp=time)
embed.set_thumbnail(url=server_icon) embed.set_thumbnail(url=server_icon)
embed.set_image(url=welcome_gif) embed.set_image(url=welcome_gif)
embed.add_field( embed.add_field(
name=blank_space, name=blank_space,
value=f"Hello {member.mention}! We hope you enjoy your stay in this server! ", value=f"Hello {member.mention}! We hope you enjoy your stay in this server! ",
inline=False) inline=False)
embed.add_field( embed.add_field(
name=blank_space, name=blank_space,
value=f"Be sure to check out our <#669815048658747392> channel to read the rules and <#683490529862090814> channel to get caught up with any changes! ", value=f"Be sure to check out our <#669815048658747392> channel to read the rules and <#683490529862090814> channel to get caught up with any changes! ",
inline=False) inline=False)
embed.add_field( embed.add_field(
name=blank_space, name=blank_space,
value=f"Last but not least, feel free to go into <#669775971297132556> to introduce yourself!", value=f"Last but not least, feel free to go into <#669775971297132556> to introduce yourself!",
inline=False) inline=False)
# Send embed to #newpeople # Send embed to #newpeople
await new_people.send(embed=embed) await new_people.send(embed=embed)
# Bot event for new member joining, sending an embed introducing them to the server # Bot event for new member joining, sending an embed introducing them to the server
@ -168,21 +168,23 @@ async def on_member_remove(member):
# Get the guild # Get the guild
guild = member.guild guild = member.guild
# Make sure the guild is Enso
if guild.id != enso_guild_ID:
return
try: try:
# Make sure the guild is Enso # With the database connection
if guild.id != enso_guild_ID: with db.connection() as conn:
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 # Delete the record of the member as they leave the server
cursor.execute(insert_query, val) insert_query = """DELETE FROM members WHERE discordID = (?)"""
conn.commit() val = member.id,
print(cursor.rowcount, "Record Deleted successfully from Members") 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: except mariadb.Error as ex:
print("Parameterized Query Failed: {}".format(ex)) print("Parameterized Query Failed: {}".format(ex))
@ -250,16 +252,4 @@ def write_to_dm_file(time, author, content):
msg_time = time.strftime('%Y-%m-%dT%H:%M:%S') msg_time = time.strftime('%Y-%m-%dT%H:%M:%S')
msg_author = message.author msg_author = message.author
msg_content = message.content msg_content = message.content
""" """

Loading…
Cancel
Save