Made more database connections Asynchronous

Addded listener for bulk deletion of messages and to log them in the modlogs channel if it was setup
pull/8/head
sgoudham 4 years ago
parent ee41ce03b7
commit 0da671f5f5

@ -1,6 +1,5 @@
import asyncio import asyncio
import datetime import datetime
from contextlib import closing
from datetime import timedelta from datetime import timedelta
from typing import Optional from typing import Optional
@ -10,6 +9,7 @@ from discord.ext.commands import command, guild_only, has_guild_permissions, bot
has_permissions, bot_has_permissions, cooldown, BucketType has_permissions, bot_has_permissions, cooldown, BucketType
import db import db
from db import connection
from settings import enso_embedmod_colours, get_modlog_for_guild, storage_modlog_for_guild, remove_modlog_channel from settings import enso_embedmod_colours, get_modlog_for_guild, storage_modlog_for_guild, remove_modlog_channel
@ -85,16 +85,19 @@ class Moderation(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 modlogs channel already exists within the database # Setup pool
with db.connection() as conn: pool = await connection(db.loop)
# Setup pool connection and cursor
async with pool.acquire() as conn:
async with conn.cursor() as cur:
# Get the row of the guild # Get the row of the guild
select_query = """SELECT * FROM guilds WHERE guildID = (?)""" select_query = """SELECT * FROM guilds WHERE guildID = (%s)"""
val = ctx.guild.id, val = ctx.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 modlog channel already exists and then stop the function # Throw error if the modlog channel already exists and then stop the function
if result[2] is not None: if result[2] is not None:
@ -121,15 +124,19 @@ class Moderation(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 modlogs does not exist 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 guilds row from the guilds table # Get the guilds row from the guilds table
select_query = """SELECT * FROM guilds WHERE guildID = (?)""" select_query = """SELECT * FROM guilds WHERE guildID = (%s)"""
vals = ctx.guild.id, vals = ctx.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 modlog channel already exists and then stop the function # Throw error if the modlog channel already exists and then stop the function
if result[2] is None: if result[2] is None:
@ -153,15 +160,19 @@ class Moderation(commands.Cog):
async def delete(self, ctx): async def delete(self, ctx):
"""Delete the Existing Modlogs System""" """Delete the Existing Modlogs System"""
# Checking if the modlogs does not exist 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 guilds row from the guilds table # Get the guilds row from the guilds table
select_query = """SELECT * FROM guilds WHERE guildID = (?)""" select_query = """SELECT * FROM guilds WHERE guildID = (%s)"""
vals = ctx.guild.id, vals = ctx.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 modlog channel already exists and then stop the function # Throw error if the modlog channel already exists and then stop the function
if result[2] is None: if result[2] is None:
@ -169,16 +180,16 @@ class Moderation(commands.Cog):
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
# Update the row to get rid of modlogs # Setup up pool connection and cursor
with db.connection() as connection: async with pool.acquire() as conn:
async with conn.cursor() as cur:
# Update the existing prefix within the database # Update the existing prefix within the database
update_query = """UPDATE guilds SET modlogs = NULL WHERE guildID = (?)""" update_query = """UPDATE guilds SET modlogs = NULL WHERE guildID = (%s)"""
update_vals = ctx.guild.id, update_vals = ctx.guild.id,
# Using the connection cursor
with closing(connection.cursor()) as cur:
# Execute the query # Execute the query
cur.execute(update_query, update_vals) await cur.execute(update_query, update_vals)
await conn.commit()
# Delete channel from cache # Delete channel from cache
remove_modlog_channel(str(ctx.guild.id)) remove_modlog_channel(str(ctx.guild.id))

Loading…
Cancel
Save