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,22 +85,25 @@ 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
select_query = """SELECT * FROM guilds WHERE guildID = (%s)"""
val = ctx.guild.id,
# Get the row of the guild
select_query = """SELECT * FROM guilds WHERE guildID = (?)"""
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:
await ctx.send("Looks like this guild already has a **Modlogs Channel** set up!" + await ctx.send("Looks like this guild already has a **Modlogs Channel** 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
# Abort the process if the channel does not exist within the guild # Abort the process if the channel does not exist within the guild
if channelID not in channels: if channelID not in channels:
@ -121,21 +124,25 @@ 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)
# Get the guilds row from the guilds table
select_query = """SELECT * FROM guilds WHERE guildID = (?)""" # Setup up pool connection and cursor
vals = ctx.guild.id, async with pool.acquire() as conn:
with closing(conn.cursor()) as cursor: async with conn.cursor() as cur:
# Get the guilds row from the guilds table
select_query = """SELECT * FROM guilds WHERE guildID = (%s)"""
vals = ctx.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 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:
await ctx.send("Looks like this guild has not setup a **Modlogs Channel**" + await ctx.send("Looks like this guild has not setup a **Modlogs Channel**" +
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
# Abort the process if the channel does not exist within the guild # Abort the process if the channel does not exist within the guild
if channelID not in channels: if channelID not in channels:
@ -153,32 +160,36 @@ 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)
# Get the guilds row from the guilds table
select_query = """SELECT * FROM guilds WHERE guildID = (?)""" # Setup up pool connection and cursor
vals = ctx.guild.id, async with pool.acquire() as conn:
with closing(conn.cursor()) as cursor: async with conn.cursor() as cur:
# Get the guilds row from the guilds table
select_query = """SELECT * FROM guilds WHERE guildID = (%s)"""
vals = ctx.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 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:
await ctx.send("Looks like this guild has not setup a **Modlogs Channel**" + await ctx.send("Looks like this guild has not setup a **Modlogs Channel**" +
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:
# Update the existing prefix within the database async with conn.cursor() as cur:
update_query = """UPDATE guilds SET modlogs = NULL WHERE guildID = (?)""" # Update the existing prefix within the database
update_vals = ctx.guild.id, update_query = """UPDATE guilds SET modlogs = NULL WHERE guildID = (%s)"""
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