Heavy refactoring, using cache and updating that instead of directly to the database

pull/8/head
sgoudham 4 years ago
parent 262a350262
commit a5cef11a2b

@ -72,9 +72,7 @@ class Relationship(Cog):
"""Wed your Lover!""" """Wed your Lover!"""
# Getting the guild of the user # Getting the guild of the user
guild = ctx.author.guild guild = ctx.guild
# Setup pool
pool = self.bot.db
# Make sure that the user cannot marry themselves # Make sure that the user cannot marry themselves
if member.id == ctx.author.id: if member.id == ctx.author.id:
@ -83,33 +81,24 @@ class Relationship(Cog):
# TODO: Probably will need to get rid of this in favour of getting from cache # TODO: Probably will need to get rid of this in favour of getting from cache
# Setup pool connection and cursor # Get the author from the cache
async with pool.acquire() as conn: db_author = await self.bot.check_cache(ctx.author.id, ctx.guild.id)
# Get the author's/members row from the Members Table married_user = db_author["married"]
select_query = """SELECT * FROM members WHERE member_id = $1 and guild_id = $2"""
# Execute the Author SQL Query
db_author = await conn.fetchrow(select_query, ctx.author.id, guild.id)
married_user = db_author["married"]
# Make sure that the person is not already married to someone else within the server # Make sure that the person is not already married to someone else within the server
if married_user: if married_user:
member = guild.get_member(married_user) member = guild.get_member(married_user)
await self.bot.generate_embed(ctx, desc=f"**((╬◣﹏◢)) You're already married to {member.mention}!**") await self.bot.generate_embed(ctx, desc=f"**((╬◣﹏◢)) You're already married to {member.mention}!**")
return return
# Execute the Member SQL Query
db_member = await conn.fetchrow(select_query, member.id, guild.id)
target_user = db_member["married"]
if target_user: # Get the member mentioned from the cache
member = guild.get_member(target_user) db_member = await self.bot.check_cache(member.id, ctx.guild.id)
await self.bot.generate_embed(ctx, target_user = db_member["married"]
desc=f"**Sorry! That user is already married to {member.mention}**")
return
# Release connection back to pool if target_user:
await pool.release(conn) member = guild.get_member(target_user)
await self.bot.generate_embed(ctx, desc=f"**Sorry! That user is already married to {member.mention}**")
return
# Send a message to the channel mentioning the author and the person they want to wed. # Send a message to the channel mentioning the author and the person they want to wed.
await self.bot.generate_embed(ctx, desc=f"{ctx.author.mention} **Proposes To** {member.mention}" await self.bot.generate_embed(ctx, desc=f"{ctx.author.mention} **Proposes To** {member.mention}"
@ -121,7 +110,6 @@ class Relationship(Cog):
def check(m): def check(m):
return m.author == member and m.channel == ctx.channel return m.author == member and m.channel == ctx.channel
# Surround with try/except to catch any exceptions that may occur
try: try:
# Wait for the message from the mentioned user # Wait for the message from the mentioned user
msg = await self.bot.wait_for('message', check=check, timeout=90.0) msg = await self.bot.wait_for('message', check=check, timeout=90.0)
@ -129,22 +117,35 @@ class Relationship(Cog):
# if the person says yes # if the person says yes
if msg.content.lower() in ['y', 'yes', 'yea']: if msg.content.lower() in ['y', 'yes', 'yea']:
# Setup pool connection and cursor # Setup pool connection
pool = self.bot.db
async with pool.acquire() as conn: async with pool.acquire() as conn:
# Get the time of marriage
message_time = msg.created_at.strftime("%a, %b %d, %Y") message_time = msg.created_at.strftime("%a, %b %d, %Y")
# Update the existing records in the database with the user that they are marrying along with the time of the accepted proposal
update_query = """UPDATE members SET married = $1, married_date = $2 WHERE member_id = $3 AND guild_id = $4"""
# Execute the SQL Query's # Update the existing records in the database with the user that they are marrying along with the time of the accepted proposal
try: try:
update_query = """UPDATE members SET married = $1, married_date = $2 WHERE member_id = $3 AND guild_id = $4"""
await conn.execute(update_query, member.id, message_time, ctx.author.id, guild.id) await conn.execute(update_query, member.id, message_time, ctx.author.id, guild.id)
await conn.execute(update_query, ctx.author.id, message_time, member.id, guild.id) await conn.execute(update_query, ctx.author.id, message_time, member.id, guild.id)
# Catch errors
except asyncpg.PostgresError as e: except asyncpg.PostgresError as e:
print(f"PostGres Error: {member.id} and {ctx.author.id} Could Not Be Married", e) print(f"PostGres Error: {member.id} and {ctx.author.id} Could Not Be Married", e)
# Update cache new details
else: else:
# TODO: Update the cache that the users have been married # TODO: Update the cache that the users have been married
pass db_author["married"] = member.id
db_author["married_date"] = message_time
db_member["married"] = ctx.author.id
db_member["married_date"] = message_time
# Release connection back to pool
finally:
await pool.release(conn)
# Congratulate them! # Congratulate them!
desc = f"**Congratulations! 。゚( ゚^∀^゚)゚。 {ctx.author.mention} and {member.mention} are now married to each other!**" desc = f"**Congratulations! 。゚( ゚^∀^゚)゚。 {ctx.author.mention} and {member.mention} are now married to each other!**"
@ -172,9 +173,7 @@ class Relationship(Cog):
"""Divorce your Partner!""" """Divorce your Partner!"""
# Getting the guild of the user # Getting the guild of the user
guild = ctx.author.guild guild = ctx.guild
# Setup pool
pool = self.bot.db
# Make sure that the user cannot divorce themselves # Make sure that the user cannot divorce themselves
if member.id == ctx.author.id: if member.id == ctx.author.id:
@ -184,31 +183,24 @@ class Relationship(Cog):
# TODO: Probably will need to get rid of this in favour of getting from cache # TODO: Probably will need to get rid of this in favour of getting from cache
# TODO: Not gonna bother refactoring this db statement # TODO: Not gonna bother refactoring this db statement
# Setup pool connection and cursor # Get the author from the cache
async with pool.acquire() as conn: db_author = await self.bot.check_cache(ctx.author.id, ctx.guild.id)
async with conn.cursor() as cur: married_user = db_author["married"]
# Get the author's row from the Members Table
select_query = """SELECT * FROM members WHERE member_id = $1 and guild_id = $1"""
# Execute the SQL Query
result = await cur.fetchrow(select_query, ctx.author.id, guild.id)
married_user = result["married"]
# Make sure that the person trying to divorce is actually married to the user # Make sure that the person trying to divorce is actually married to the user
if married_user is None: if married_user is None:
desc = "**((╬◣﹏◢)) You must be married in order to divorce someone! Baka!**"
desc = "**((╬◣﹏◢)) You must be married in order to divorce someone! Baka!**" await self.bot.generate_embed(ctx, desc=desc)
await self.bot.generate_embed(ctx, desc=desc) return
return
# Make sure the person is married to the person that they're trying to divorce # Make sure the person is married to the person that they're trying to divorce
elif married_user != member.id: elif married_user != member.id:
member = guild.get_member(married_user) member = guild.get_member(married_user)
desc = f"**( ゜口゜) You can only divorce the person that you're married!" \ desc = f"**( ゜口゜) You can only divorce the person that you're married!" \
f"\n That person is {member.mention}**" f"\n That person is {member.mention}**"
await self.bot.generate_embed(ctx, desc=desc) await self.bot.generate_embed(ctx, desc=desc)
return return
# Send a message to the channel mentioning the author and the person they want to wed. # Send a message to the channel mentioning the author and the person they want to wed.
await self.bot.generate_embed(ctx, desc=f"{ctx.author.mention} **Wishes to Divorce** {member.mention}" await self.bot.generate_embed(ctx, desc=f"{ctx.author.mention} **Wishes to Divorce** {member.mention}"
@ -228,21 +220,33 @@ class Relationship(Cog):
# if the person says yes # if the person says yes
if msg.content.lower() in ['y', 'yes', 'yea']: if msg.content.lower() in ['y', 'yes', 'yea']:
# Setup pool connection and cursor # Setup pool connection
pool = self.bot.db
async with pool.acquire() as conn: async with pool.acquire() as conn:
# Update the existing records in the database with the user that they are marrying along with the time of the accepted proposal # Update the existing records in the database that the author and member are divorced
update_query = """UPDATE members SET married = NULL, married_date = NULL WHERE member_id = $1 and guild_id = $2"""
try: try:
# Execute the SQL Query's update_query = """UPDATE members SET married = NULL, married_date = NULL WHERE member_id = $1 and guild_id = $2"""
await conn.execute(update_query, ctx.author.id, guild.id) await conn.execute(update_query, ctx.author.id, guild.id)
await conn.execute(update_query, member.id, guild.id) await conn.execute(update_query, member.id, guild.id)
# Catch errors
except asyncpg.PostgresError as e: except asyncpg.PostgresError as e:
print(f"PostGres Error: {member.id} and {ctx.author.id} Could Not Be Divorced", e) print(f"PostGres Error: {member.id} and {ctx.author.id} Could Not Be Divorced", e)
# Update cache with new details
else: else:
# TODO: Update the cache that the users have been divorced # TODO: Update the cache that the users have been divorced
pass db_author["married"] = None
db_author["married_date"] = None
db_member = await self.bot.check_cache(member.id, ctx.guild.id)
db_member["married"] = None
db_member["married_date"] = None
# Release connection back to pool
finally:
await pool.release(conn)
# Congratulate them! # Congratulate them!
desc = f"**૮( ´⁰▱๋⁰ )ა {ctx.author.mention} and {member.mention} are now divorced." \ desc = f"**૮( ´⁰▱๋⁰ )ა {ctx.author.mention} and {member.mention} are now divorced." \

Loading…
Cancel
Save