The proposal/divorce will timeout after 90 seconds

Added commentary
Updated database connections to be asynchronous
pull/8/head
sgoudham 4 years ago
parent c62f6ec45d
commit a4cb604dec

@ -1,7 +1,6 @@
import asyncio import asyncio
import datetime import datetime
import random import random
from contextlib import closing
from discord import Member, Embed, Colour from discord import Member, Embed, Colour
from discord.ext import commands from discord.ext import commands
@ -63,12 +62,14 @@ class Relationship(commands.Cog):
# Getting the guild of the user # Getting the guild of the user
guild = ctx.author.guild guild = ctx.author.guild
# Setup pool
pool = await connection2(db.loop) pool = await connection2(db.loop)
# Setup pool connection and cursor
async with pool.acquire() as conn: async with pool.acquire() as conn:
async with conn.cursor() as author_cursor: async with conn.cursor() as author_cursor:
# Get the author's/members row from the Members Table # Get the author's/members row from the Members Table
select_query = """SELECT * FROM members WHERE discordID = %s and guildID = %s""" select_query = """SELECT * FROM members WHERE discordID = (%s) and guildID = (%s)"""
author_val = ctx.author.id, guild.id, author_val = ctx.author.id, guild.id,
member_val = member.id, guild.id, member_val = member.id, guild.id,
@ -112,25 +113,28 @@ class Relationship(commands.Cog):
# Surround with try/except to catch any exceptions that may occur # 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) msg = await self.bot.wait_for('message', check=check, timeout=90.0)
# 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']:
# Using connection to the database
with db.connection() as conn:
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 # Setup pool
update_query = """UPDATE members SET married = (?), marriedDate = (?) WHERE discordID = (?) AND guildID = (?)""" pool = await connection2(db.loop)
proposer = member.id, message_time, ctx.author.id, guild.id,
proposee = ctx.author.id, message_time, member.id, guild.id, # Setup pool connection and cursor
async with pool.acquire() as conn:
async with conn.cursor() as cur:
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 = (%s), marriedDate = (%s) WHERE discordID = (%s) AND guildID = (%s)"""
proposer = member.id, message_time, ctx.author.id, guild.id,
proposee = ctx.author.id, message_time, member.id, guild.id,
with closing(conn.cursor()) as cursor:
# Execute the SQL Query's # Execute the SQL Query's
cursor.execute(update_query, proposer) cur.execute(update_query, proposer)
cursor.execute(update_query, proposee) cur.execute(update_query, proposee)
conn.commit() conn.commit()
print(cursor.rowcount, "2 people have been married!") print(cur.rowcount, "2 people have been married!")
# Congratulate them! # Congratulate them!
await ctx.send( await ctx.send(
@ -148,10 +152,8 @@ class Relationship(commands.Cog):
except asyncio.TimeoutError as ex: except asyncio.TimeoutError as ex:
print(ex) print(ex)
# Delete the "proposal"
await msg.delete()
# Send out an error message if the user waited too long # Send out an error message if the user waited too long
await ctx.send("**(。T ω T。) They waited too long**") await ctx.send("**(。T ω T。) {} waited too long**".format(member.mention))
@command(name="divorce", aliases=["Divorce"]) @command(name="divorce", aliases=["Divorce"])
@cooldown(1, 1, BucketType.user) @cooldown(1, 1, BucketType.user)
@ -160,34 +162,35 @@ class Relationship(commands.Cog):
# Getting the guild of the user # Getting the guild of the user
guild = ctx.author.guild guild = ctx.author.guild
# Setup pool
pool = await connection2(db.loop)
# Use database connection # Setup pool connection and cursor
with db.connection() as conn: async with pool.acquire() as conn:
async with conn.cursor() as cur:
# Get the author's row from the Members Table # Get the author's row from the Members Table
select_query = """SELECT * FROM members WHERE discordID = (?) and guildID = (?)""" select_query = """SELECT * FROM members WHERE discordID = (%s) and guildID = (%s)"""
val = ctx.author.id, guild.id, val = ctx.author.id, guild.id,
with closing(conn.cursor()) as cursor:
# Execute the SQL Query # Execute the SQL Query
cursor.execute(select_query, val) cur.execute(select_query, val)
result = cursor.fetchone() result = cur.fetchone()
married_user = result[1] married_user = result[1]
# 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:
await ctx.send("**Senpaii! ˭̡̞(◞⁎˃ᆺ˂)◞*✰ You can't possibly divorce yourself!**") await ctx.send("**Senpaii! ˭̡̞(◞⁎˃ᆺ˂)◞*✰ You can't possibly divorce yourself!**")
return return
# 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
elif married_user is None: elif married_user is None:
await ctx.send(f"**((╬◣﹏◢)) You must be married in order to divorce someone! Baka!**") await ctx.send(f"**((╬◣﹏◢)) You must be married in order to divorce someone! Baka!**")
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 != str(member.id): elif married_user != str(member.id):
member = guild.get_member(int(married_user)) member = guild.get_member(int(married_user))
await ctx.send(f"**( ゜口゜) You can only divorce the person that you're married!" await ctx.send(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}**")
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 ctx.send( await ctx.send(
@ -203,23 +206,26 @@ class Relationship(commands.Cog):
# Surround with try/except to catch any exceptions that may occur # 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) msg = await self.bot.wait_for('message', check=check, timeout=90.0)
# 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']:
# Using connection to the database # Setup pool
with db.connection() as conn: pool = await connection2(db.loop)
# Update the existing records in the database with the user that they are marrying along with the time of the accepted proposal # Setup pool connection and cursor
update_query = """UPDATE members SET married = null, marriedDate = null WHERE discordID = (?) and guildID = (?)""" async with pool.acquire() as conn:
divorcer = ctx.author.id, guild.id, async with conn.cursor() as cur:
divorcee = member.id, guild.id, # Update the existing records in the database with the user that they are marrying along with the time of the accepted proposal
with closing(conn.cursor()) as cursor: update_query = """UPDATE members SET married = null, marriedDate = null WHERE discordID = (%s) and guildID = (%s)"""
divorcer = ctx.author.id, guild.id,
divorcee = member.id, guild.id,
# Execute the SQL Query's # Execute the SQL Query's
cursor.execute(update_query, divorcer) cur.execute(update_query, divorcer)
cursor.execute(update_query, divorcee) cur.execute(update_query, divorcee)
conn.commit() conn.commit()
print(cursor.rowcount, "2 Members have been divorced :(!") print(cur.rowcount, "2 Members have been divorced :(!")
# Congratulate them! # Congratulate them!
await ctx.send( await ctx.send(
@ -260,37 +266,39 @@ class Relationship(commands.Cog):
# Getting the guild of the user # Getting the guild of the user
guild = member.guild guild = member.guild
# Use database connection # Setup pool
with db.connection() as conn: pool = await connection2(db.loop)
# Get the author's row from the Members Table # Setup pool connection and cursor
select_query = """SELECT * FROM members WHERE discordID = (?) and guildID = (?)""" async with pool.acquire() as conn:
val = member.id, guild.id, async with conn.cursor() as cur:
with closing(conn.cursor()) as cursor: # Get the author's row from the Members Table
select_query = """SELECT * FROM members WHERE discordID = (%s) and guildID = (%s)"""
val = member.id, guild.id,
# Execute the SQL Query # Execute the SQL Query
cursor.execute(select_query, val) cur.execute(select_query, val)
result = cursor.fetchone() result = cur.fetchone()
user = result[1] user = result[1]
marriage_date = result[2] marriage_date = result[2]
# Set empty values for non-married users # Set empty values for non-married users
if user is None: if user is None:
married = False married = False
marriedUser = "" marriedUser = ""
marriedDate = "" marriedDate = ""
# Set the member, date married and setting married status # Set the member, date married and setting married status
else: else:
marriedUser = guild.get_member(int(user)) marriedUser = guild.get_member(int(user))
marriedDate = marriage_date marriedDate = marriage_date
married = True married = True
# Get the current date of the message sent by the user # Get the current date of the message sent by the user
currentDate = ctx.message.created_at.strftime("%a, %b %d, %Y") currentDate = ctx.message.created_at.strftime("%a, %b %d, %Y")
# Get the marriage info embed and then send it to the display # Get the marriage info embed and then send it to the display
embed = marriageInfo(member, marriedUser, marriedDate, currentDate, married) embed = marriageInfo(member, marriedUser, marriedDate, currentDate, married)
await ctx.send(embed=embed) await ctx.send(embed=embed)
def setup(bot): def setup(bot):

Loading…
Cancel
Save