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,21 +175,25 @@ 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)
# Get the author's row from the Members Table
select_query = """SELECT * FROM moderatormail WHERE guildID = (?)""" # Setup up pool connection and cursor
val = ctx.author.guild.id, async with pool.acquire() as conn:
with closing(conn.cursor()) as cursor: async with conn.cursor() as cur:
# Get the author's row from the Members Table
select_query = """SELECT * FROM moderatormail WHERE guildID = (%s)"""
val = ctx.author.guild.id,
# 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:
await ctx.send("Looks like this guild already has a **Modmail System** set up!" + await ctx.send("Looks like this guild already has a **Modmail System** set up!" +
f"\nPlease check **{ctx.prefix}help** for information on how to update/delete existing information") f"\nPlease check **{ctx.prefix}help** for information on how to update/delete existing information")
return return
# As long as the channel exists within the guild # As long as the channel exists within the guild
if channelID in channels: if channelID in channels:
@ -234,18 +237,20 @@ 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:
# Define the insert statement that will insert information about the modmail channel async with conn.cursor() as cur:
insert_query = """INSERT INTO moderatormail (guildID, channelID, messageID, modmailChannelID) VALUES (?, ?, ?, ?)""" # Define the insert statement that will insert information about the modmail channel
vals = ctx.author.guild.id, channelID, modmailchannelID.id, int(msg.content), insert_query = """INSERT INTO moderatormail (guildID, channelID, messageID, modmailChannelID) VALUES (%s, %s, %s, %s)"""
with closing(conn.cursor()) as cursor: vals = ctx.author.guild.id, channelID, modmailchannelID.id, int(msg.content),
# 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")
return return
except mariadb.IntegrityError as err: except mariadb.IntegrityError as err:
print(err) print(err)
@ -272,37 +277,41 @@ 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)
# Get the author's row from the Members Table
select_query = """SELECT * FROM moderatormail WHERE guildID = (?)""" # Setup up pool connection and cursor
vals = ctx.author.guild.id, async with pool.acquire() as conn:
with closing(conn.cursor()) as cursor: async with conn.cursor() as cur:
# Get the author's row from the Members Table
select_query = """SELECT * FROM moderatormail WHERE guildID = (%s)"""
vals = ctx.author.guild.id,
# 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:
await ctx.send("Looks like this guild does not have a **Modmail System** setup!" + await ctx.send("Looks like this guild does not have a **Modmail System** setup!" +
f"\nPlease check **{ctx.prefix}help** for information on how to update/delete existing information") f"\nPlease check **{ctx.prefix}help** for information on how to update/delete existing information")
return return
# As long as the channel exists within the guild # As long as the channel exists within the guild
if channelID in channels: if channelID in channels:
try: try:
# Store the information within the database
with db.connection() as conn:
# Define the update statement that will insert information about the modmail channel # Setup up pool connection and cursor
update_query = """UPDATE moderatormail SET modmailChannelID = (?) WHERE guildID = (?)""" async with pool.acquire() as conn:
vals = channelID, ctx.author.guild.id async with conn.cursor() as cur:
# Define the update statement that will insert information about the modmail channel
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 # 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)
# Get the author's row from the Members Table
select_query = """SELECT * FROM moderatormail WHERE guildID = (?)""" # Setup up pool connection and cursor
vals = ctx.author.guild.id, async with pool.acquire() as conn:
with closing(conn.cursor()) as cursor: async with conn.cursor() as cur:
# Get the author's row from the Members Table
select_query = """SELECT * FROM moderatormail WHERE guildID = (%s)"""
vals = ctx.author.guild.id,
# 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
# Define the delete statement to remove all information about the guild async with pool.acquire() as conn:
delete_query = """DELETE FROM moderatormail WHERE guildID = (?)""" async with conn.cursor() as cur:
vals = ctx.author.guild.id, # Define the delete statement to remove all information about the guild
with closing(conn.cursor()) as cursor: delete_query = """DELETE FROM moderatormail WHERE guildID = (%s)"""
vals = ctx.author.guild.id,
# 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,26 +390,29 @@ 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
select_query = """SELECT * FROM moderatormail WHERE guildID = (%s)"""
val = guildid,
# Get the author's row from the Members Table
select_query = """SELECT * FROM moderatormail WHERE 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:
return return
# Define variables # Define variables
guild_id = int(result[0]) guild_id = int(result[0])
channel_id = int(result[1]) channel_id = int(result[1])
message_id = int(result[2]) message_id = int(result[2])
modmail_channel_id = int(result[3]) modmail_channel_id = int(result[3])
# Bunch of checks to make sure it has the right guild, channel, message and reaction # Bunch of checks to make sure it has the right guild, channel, message and reaction
if payload.guild_id == guild_id and payload.channel_id == channel_id and payload.message_id == message_id and payload.emoji.name == "": if payload.guild_id == guild_id and payload.channel_id == channel_id and payload.message_id == message_id and payload.emoji.name == "":

Loading…
Cancel
Save