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 os
import random
from contextlib import closing
import discord
import mariadb
@ -176,15 +175,19 @@ class Modmail(commands.Cog):
# Retrieve a list of channel id's in the guild
channels = [channel.id for channel in ctx.guild.channels]
# Checking if the guild already exists within the database
with db.connection() as conn:
# Setup pool
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
select_query = """SELECT * FROM moderatormail WHERE guildID = (?)"""
select_query = """SELECT * FROM moderatormail WHERE guildID = (%s)"""
val = ctx.author.guild.id,
with closing(conn.cursor()) as cursor:
# Execute the SQL Query
cursor.execute(select_query, val)
result = cursor.fetchone()
await cur.execute(select_query, val)
result = await cur.fetchone()
# Throw error if the guild already exists and then stop the function
if result is not None:
@ -234,14 +237,16 @@ class Modmail(commands.Cog):
# Auto add the ✅ reaction
await modmailchannelID.add_reaction('')
# 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 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),
with closing(conn.cursor()) as cursor:
# 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!" +
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
channels = [channel.id for channel in ctx.guild.channels]
# Checking if the guild already exists within the database
with db.connection() as conn:
# Setup pool
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
select_query = """SELECT * FROM moderatormail WHERE guildID = (?)"""
select_query = """SELECT * FROM moderatormail WHERE guildID = (%s)"""
vals = ctx.author.guild.id,
with closing(conn.cursor()) as cursor:
# Execute the SQL Query
cursor.execute(select_query, vals)
result = cursor.fetchone()
await cur.execute(select_query, vals)
result = await cur.fetchone()
# Throw error if the guild already exists and then stop the function
if result is None:
@ -292,17 +301,17 @@ class Modmail(commands.Cog):
if channelID in channels:
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
update_query = """UPDATE moderatormail SET modmailChannelID = (?) WHERE guildID = (?)"""
update_query = """UPDATE moderatormail SET modmailChannelID = (%s) WHERE guildID = (%s)"""
vals = channelID, ctx.author.guild.id
with closing(conn.cursor()) as cursor:
# Execute the SQL Query
cursor.execute(update_query, vals)
conn.commit()
await cur.execute(update_query, vals)
await conn.commit()
except mariadb.Error as err:
print(err)
@ -324,15 +333,19 @@ class Modmail(commands.Cog):
async def delete(self, ctx):
"""Delete the Entire Modmail System from the Guild"""
# Checking if the guild already exists within the database
with db.connection() as conn:
# Setup pool
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
select_query = """SELECT * FROM moderatormail WHERE guildID = (?)"""
select_query = """SELECT * FROM moderatormail WHERE guildID = (%s)"""
vals = ctx.author.guild.id,
with closing(conn.cursor()) as cursor:
# Execute the SQL Query
cursor.execute(select_query, vals)
result = cursor.fetchone()
await cur.execute(select_query, vals)
result = await cur.fetchone()
# Throw error if the guild already exists and then stop the function
if result is None:
@ -341,15 +354,17 @@ class Modmail(commands.Cog):
return
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
delete_query = """DELETE FROM moderatormail WHERE guildID = (?)"""
delete_query = """DELETE FROM moderatormail WHERE guildID = (%s)"""
vals = ctx.author.guild.id,
with closing(conn.cursor()) as cursor:
# Execute the SQL Query
cursor.execute(delete_query, vals)
conn.commit()
await cur.execute(delete_query, vals)
await conn.commit()
except mariadb.Error as err:
print(err)
@ -375,16 +390,19 @@ class Modmail(commands.Cog):
# Find a role corresponding to the Emoji name.
guildid = payload.guild_id
# Use database connection
with db.connection() as conn:
# Setup pool
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
select_query = """SELECT * FROM moderatormail WHERE guildID = (?)"""
select_query = """SELECT * FROM moderatormail WHERE guildID = (%s)"""
val = guildid,
with closing(conn.cursor()) as cursor:
# Execute the SQL Query
cursor.execute(select_query, val)
result = cursor.fetchone()
await cur.execute(select_query, val)
result = await cur.fetchone()
# Adding error handling
if result is None:

Loading…
Cancel
Save