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")
# Setup pool
pool = await connection2(db.loop)
# 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 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 = (?), marriedDate = (?) WHERE discordID = (?) AND guildID = (?)""" 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, proposer = member.id, message_time, ctx.author.id, guild.id,
proposee = ctx.author.id, message_time, member.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,18 +162,19 @@ 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
@ -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)
# Setup pool connection and cursor
async with pool.acquire() as conn:
async with conn.cursor() as cur:
# 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 with the user that they are marrying along with the time of the accepted proposal
update_query = """UPDATE members SET married = null, marriedDate = null WHERE discordID = (?) and guildID = (?)""" update_query = """UPDATE members SET married = null, marriedDate = null WHERE discordID = (%s) and guildID = (%s)"""
divorcer = ctx.author.id, guild.id, divorcer = ctx.author.id, guild.id,
divorcee = member.id, guild.id, divorcee = member.id, guild.id,
with closing(conn.cursor()) as cursor:
# 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,17 +266,19 @@ 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)
# Setup pool connection and cursor
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 = member.id, guild.id, val = member.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()
user = result[1] user = result[1]
marriage_date = result[2] marriage_date = result[2]

Loading…
Cancel
Save