Made database connections Asynchronous

Changing authors name to display name
pull/8/head
sgoudham 4 years ago
parent f7185eeff6
commit 529494bc54

@ -1,6 +1,5 @@
import datetime import datetime
import random import random
from contextlib import closing
from discord import Colour, Embed, Member from discord import Colour, Embed, Member
from discord.ext import commands from discord.ext import commands
@ -41,26 +40,28 @@ class Interactive(commands.Cog):
# Get the guild # Get the guild
guild = ctx.author.guild guild = ctx.author.guild
# Use database connection # Setup pool
with db.connection() as conn: pool = await db.connection(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 = 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) await cur.execute(select_query, val)
result = cursor.fetchone() result = await cur.fetchone()
married_user = result[1] married_user = result[1]
# Error handling to make sure that the user can kiss themselves # Error handling to make sure that the user can kiss themselves
if member.id == ctx.author.id: if member.id == ctx.author.id:
kiss = False kiss = False
title = f":kissing_heart: :kissing_heart: | **{ctx.author.name}** kissed **themselves**" title = f":kissing_heart: :kissing_heart: | **{ctx.author.display_name}** kissed **themselves**"
else: else:
kiss = True kiss = True
title = f":kissing_heart: :kissing_heart: | **{ctx.author.name}** kissed **{member.display_name}**" title = f":kissing_heart: :kissing_heart: | **{ctx.author.display_name}** kissed **{member.display_name}**"
try: try:
# Make sure the user isn't trying to kiss someone else besides their partner # Make sure the user isn't trying to kiss someone else besides their partner
@ -108,26 +109,28 @@ class Interactive(commands.Cog):
# Get the guild # Get the guild
guild = ctx.author.guild guild = ctx.author.guild
# Use database connection # Setup pool
with db.connection() as conn: pool = await db.connection(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 = 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) await cur.execute(select_query, val)
result = cursor.fetchone() result = await cur.fetchone()
married_user = result[1] married_user = result[1]
# Error handling to make sure that the user can cuddle themselves # Error handling to make sure that the user can cuddle themselves
if member.id == ctx.author.id: if member.id == ctx.author.id:
cuddle = False cuddle = False
title = f":blush: :blush: | **{ctx.author.name}** cuddled **themselves**" title = f":blush: :blush: | **{ctx.author.display_name}** cuddled **themselves**"
else: else:
cuddle = True cuddle = True
title = f":blush: :blush: | **{ctx.author.name}** cuddled **{member.display_name}**" title = f":blush: :blush: | **{ctx.author.display_name}** cuddled **{member.display_name}**"
try: try:
# Make sure the user isn't trying to cuddle someone else besides their partner # Make sure the user isn't trying to cuddle someone else besides their partner
@ -173,9 +176,9 @@ class Interactive(commands.Cog):
"""Kill a Member""" """Kill a Member"""
if member is ctx.author: if member is ctx.author:
title = f":scream: :scream: | **{ctx.author.name}** killed **themselves**" title = f":scream: :scream: | **{ctx.author.display_name}** killed **themselves**"
else: else:
title = f":scream: :scream: | **{ctx.author.name}** killed **{member.display_name}**" title = f":scream: :scream: | **{ctx.author.display_name}** killed **{member.display_name}**"
# Surround with try/except to catch any exceptions that may occur # Surround with try/except to catch any exceptions that may occur
try: try:
@ -209,9 +212,9 @@ class Interactive(commands.Cog):
"""Slap a Member""" """Slap a Member"""
if member is ctx.author: if member is ctx.author:
title = f":cold_sweat: :cold_sweat: | **{ctx.author.name}** slapped **themselves**" title = f":cold_sweat: :cold_sweat: | **{ctx.author.display_name}** slapped **themselves**"
else: else:
title = f":cold_sweat: :cold_sweat: | **{ctx.author.name}** slapped **{member.display_name}**" title = f":cold_sweat: :cold_sweat: | **{ctx.author.display_name}** slapped **{member.display_name}**"
# Surround with try/except to catch any exceptions that may occur # Surround with try/except to catch any exceptions that may occur
try: try:
@ -245,9 +248,9 @@ class Interactive(commands.Cog):
"""Pat a Member""" """Pat a Member"""
if member is ctx.author: if member is ctx.author:
title = f"πŸ‘‰ πŸ‘ˆ | **{ctx.author.name}** patted **themselves**" title = f"πŸ‘‰ πŸ‘ˆ | **{ctx.author.display_name}** patted **themselves**"
else: else:
title = f"πŸ‘‰ πŸ‘ˆ | **{ctx.author.name}** patted **{member.display_name}**" title = f"πŸ‘‰ πŸ‘ˆ | **{ctx.author.display_name}** patted **{member.display_name}**"
# Surround with try/except to catch any exceptions that may occur # Surround with try/except to catch any exceptions that may occur
try: try:
@ -281,9 +284,9 @@ class Interactive(commands.Cog):
"""Give Lemon to Member""" """Give Lemon to Member"""
if member is ctx.author: if member is ctx.author:
title = f":relaxed: :relaxed: | **{ctx.author.name}** gave a lemon to **themselves**" title = f":relaxed: :relaxed: | **{ctx.author.display_name}** gave a lemon to **themselves**"
else: else:
title = f":relaxed: :relaxed: | **{ctx.author.name}** gave a lemon to **{member.display_name}**" title = f":relaxed: :relaxed: | **{ctx.author.display_name}** gave a lemon to **{member.display_name}**"
lemon_array = ["https://media.discordapp.net/attachments/669812887564320769/720093589056520202/lemon.gif", lemon_array = ["https://media.discordapp.net/attachments/669812887564320769/720093589056520202/lemon.gif",
"https://media.discordapp.net/attachments/669812887564320769/720093575492272208/lemon2.gif", "https://media.discordapp.net/attachments/669812887564320769/720093575492272208/lemon2.gif",
@ -316,9 +319,9 @@ class Interactive(commands.Cog):
"""Choke a Member""" """Choke a Member"""
if member is ctx.author: if member is ctx.author:
title = f":confounded: :confounded: | **{ctx.author.name}** choked **themselves**" title = f":confounded: :confounded: | **{ctx.author.display_name}** choked **themselves**"
else: else:
title = f":confounded: :confounded: | **{ctx.author.name}** choked **{member.display_name}**" title = f":confounded: :confounded: | **{ctx.author.display_name}** choked **{member.display_name}**"
# Surround with try/except to catch any exceptions that may occur # Surround with try/except to catch any exceptions that may occur
try: try:
@ -351,9 +354,9 @@ class Interactive(commands.Cog):
"""Hug a Member""" """Hug a Member"""
if member is ctx.author: if member is ctx.author:
title = f":smiling_face_with_3_hearts: :smiling_face_with_3_hearts: | **{ctx.author.name}** hugged **themselves**" title = f":smiling_face_with_3_hearts: :smiling_face_with_3_hearts: | **{ctx.author.display_name}** hugged **themselves**"
else: else:
title = f":smiling_face_with_3_hearts: :smiling_face_with_3_hearts: | **{ctx.author.name}** hugged **{member.display_name}**" title = f":smiling_face_with_3_hearts: :smiling_face_with_3_hearts: | **{ctx.author.display_name}** hugged **{member.display_name}**"
# Surround with try/except to catch any exceptions that may occur # Surround with try/except to catch any exceptions that may occur
try: try:

Loading…
Cancel
Save