Made more database connections Asynchronous

pull/8/head
sgoudham 4 years ago
parent 0da671f5f5
commit 902ea30c95

@ -2,7 +2,6 @@ import asyncio
import datetime import datetime
import os import os
import random import random
from contextlib import closing
import discord import discord
import mariadb import mariadb
@ -176,15 +175,19 @@ class Modmail(commands.Cog):
# Retrieve a list of channel id's in the guild # Retrieve a list of channel id's in the guild
channels = [channel.id for channel in ctx.guild.channels] channels = [channel.id for channel in ctx.guild.channels]
# Checking if the guild already exists within the database # Setup pool
with db.connection() as conn: pool = await db.connection(db.loop)
# Setup up 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 moderatormail WHERE guildID = (?)""" select_query = """SELECT * FROM moderatormail WHERE guildID = (%s)"""
val = ctx.author.guild.id, val = ctx.author.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()
# Throw error if the guild already exists and then stop the function # Throw error if the guild already exists and then stop the function
if result is not None: if result is not None:
@ -234,14 +237,16 @@ class Modmail(commands.Cog):
# Auto add the ✅ reaction # Auto add the ✅ reaction
await modmailchannelID.add_reaction('') await modmailchannelID.add_reaction('')
# Store the information within the database # Setup up pool connection and cursor
with db.connection() as conn: async with pool.acquire() as conn:
async with conn.cursor() as cur:
# Define the insert statement that will insert information about the modmail channel # Define the insert statement that will insert information about the modmail channel
insert_query = """INSERT INTO moderatormail (guildID, channelID, messageID, modmailChannelID) VALUES (?, ?, ?, ?)""" insert_query = """INSERT INTO moderatormail (guildID, channelID, messageID, modmailChannelID) VALUES (%s, %s, %s, %s)"""
vals = ctx.author.guild.id, channelID, modmailchannelID.id, int(msg.content), vals = ctx.author.guild.id, channelID, modmailchannelID.id, int(msg.content),
with closing(conn.cursor()) as cursor:
# Execute the SQL Query # Execute the SQL Query
cursor.execute(insert_query, vals) await cur.execute(insert_query, vals)
await conn.commit()
await ctx.send("Your **Modmail System** is now successfully set up!" + await ctx.send("Your **Modmail System** is now successfully set up!" +
f"\nPlease refer to **{ctx.prefix}help** for any information") f"\nPlease refer to **{ctx.prefix}help** for any information")
@ -272,15 +277,19 @@ class Modmail(commands.Cog):
# Retrieve a list of channel id's in the guild # Retrieve a list of channel id's in the guild
channels = [channel.id for channel in ctx.guild.channels] channels = [channel.id for channel in ctx.guild.channels]
# Checking if the guild already exists within the database # Setup pool
with db.connection() as conn: pool = await db.connection(db.loop)
# Setup up 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 moderatormail WHERE guildID = (?)""" select_query = """SELECT * FROM moderatormail WHERE guildID = (%s)"""
vals = ctx.author.guild.id, vals = ctx.author.guild.id,
with closing(conn.cursor()) as cursor:
# Execute the SQL Query # Execute the SQL Query
cursor.execute(select_query, vals) await cur.execute(select_query, vals)
result = cursor.fetchone() result = await cur.fetchone()
# Throw error if the guild already exists and then stop the function # Throw error if the guild already exists and then stop the function
if result is None: if result is None:
@ -292,17 +301,17 @@ class Modmail(commands.Cog):
if channelID in channels: if channelID in channels:
try: try:
# Store the information within the database
with db.connection() as conn:
# Setup up pool connection and cursor
async with pool.acquire() as conn:
async with conn.cursor() as cur:
# Define the update statement that will insert information about the modmail channel # Define the update statement that will insert information about the modmail channel
update_query = """UPDATE moderatormail SET modmailChannelID = (?) WHERE guildID = (?)""" update_query = """UPDATE moderatormail SET modmailChannelID = (%s) WHERE guildID = (%s)"""
vals = channelID, ctx.author.guild.id vals = channelID, ctx.author.guild.id
with closing(conn.cursor()) as cursor:
# Execute the SQL Query # Execute the SQL Query
cursor.execute(update_query, vals) await cur.execute(update_query, vals)
conn.commit() await conn.commit()
except mariadb.Error as err: except mariadb.Error as err:
print(err) print(err)
@ -324,15 +333,19 @@ class Modmail(commands.Cog):
async def delete(self, ctx): async def delete(self, ctx):
"""Delete the Entire Modmail System from the Guild""" """Delete the Entire Modmail System from the Guild"""
# Checking if the guild already exists within the database # Setup pool
with db.connection() as conn: pool = await db.connection(db.loop)
# Setup up 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 moderatormail WHERE guildID = (?)""" select_query = """SELECT * FROM moderatormail WHERE guildID = (%s)"""
vals = ctx.author.guild.id, vals = ctx.author.guild.id,
with closing(conn.cursor()) as cursor:
# Execute the SQL Query # Execute the SQL Query
cursor.execute(select_query, vals) await cur.execute(select_query, vals)
result = cursor.fetchone() result = await cur.fetchone()
# Throw error if the guild already exists and then stop the function # Throw error if the guild already exists and then stop the function
if result is None: if result is None:
@ -341,15 +354,17 @@ class Modmail(commands.Cog):
return return
try: try:
# Store the information within the database
with db.connection() as conn: # Setup up pool connection and cursor
async with pool.acquire() as conn:
async with conn.cursor() as cur:
# Define the delete statement to remove all information about the guild # Define the delete statement to remove all information about the guild
delete_query = """DELETE FROM moderatormail WHERE guildID = (?)""" delete_query = """DELETE FROM moderatormail WHERE guildID = (%s)"""
vals = ctx.author.guild.id, vals = ctx.author.guild.id,
with closing(conn.cursor()) as cursor:
# Execute the SQL Query # Execute the SQL Query
cursor.execute(delete_query, vals) await cur.execute(delete_query, vals)
conn.commit() await conn.commit()
except mariadb.Error as err: except mariadb.Error as err:
print(err) print(err)
@ -375,16 +390,19 @@ class Modmail(commands.Cog):
# Find a role corresponding to the Emoji name. # Find a role corresponding to the Emoji name.
guildid = payload.guild_id guildid = payload.guild_id
# Use database connection # Setup pool
with db.connection() as conn: pool = await db.connection(db.loop)
# Setup up 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 moderatormail WHERE guildID = (?)""" select_query = """SELECT * FROM moderatormail WHERE guildID = (%s)"""
val = guildid, val = guildid,
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()
# Adding error handling # Adding error handling
if result is None: if result is None:

Loading…
Cancel
Save