Fixed Database connections

This commit is contained in:
Dusty.P 2018-05-05 20:51:20 -08:00
parent 83f5bb7476
commit 411c1fc125
2 changed files with 62 additions and 29 deletions

View File

@ -1,9 +1,9 @@
import discord
from discord.ext import commands
import os
from .imports import checks, utils
import json
import logging
import inspect
admin_log = logging.getLogger('admin')
config_dir = 'config/'
@ -32,7 +32,7 @@ class Admin:
@commands.command(hidden=True)
@commands.check(checks.is_guild_owner)
async def get_guild_config(self, ctx):
config = self.bot.con.one('select * from guild_config where guild_id = %(id)s', {'id': ctx.guild.id})
config = self.bot.db_con.fetchval('select * from guild_config where guild_id = $1', ctx.guild.id)
configs = [str(config)[i:i+1990] for i in range(0, len(config), 1990)]
await ctx.message.author.send(f'The current config for the {ctx.guild.name} guild is:\n')
admin_log.info(configs)
@ -58,26 +58,23 @@ class Admin:
del self.bot.bot_config['db_con']
await ctx.send('Config reloaded.')
@set.command(name='channel_lockdown', aliases=['lockdown', 'restrict_access', 'cl'])
async def _channel_lockdown(self, ctx, config='true'):
if ctx.guild:
if checks.is_admin(self.bot, ctx):
if str(config).lower() == 'true':
if self.bot.con.one('select allowed_channels from guild_config where guild_id = %(id)s',
{'id': ctx.guild.id}) is []:
if self.bot.db_con.fetchval('select allowed_channels from guild_config where guild_id = $1',
ctx.guild.id) is []:
await ctx.send('Please set at least one allowed channel before running this command.')
else:
self.bot.con.run('update guild_config set channel_lockdown = True where guild_id = %(id)s',
{'id': ctx.guild.id})
self.bot.db_con.execute('update guild_config set channel_lockdown = True where guild_id = $1',
ctx.guild.id)
await ctx.send('Channel Lockdown is now active.')
elif str(config).lower() == 'false':
if self.bot.con.one('select channel_lockdown from guild_config where guild_id = %(id)s',
{'id': ctx.guild.id}):
self.bot.con.run('update guild_config set channel_lockdown = False where guild_id = %(id)s',
{'id': ctx.guild.id})
if self.bot.db_con.fetchval('select channel_lockdown from guild_config where guild_id = $1',
ctx.guild.id):
self.bot.db_con.execute('update guild_config set channel_lockdown = False where guild_id = $1',
ctx.guild.id)
await ctx.send('Channel Lockdown has been deactivated.')
else:
await ctx.send('Channel Lockdown is already deactivated.')
@ -98,28 +95,28 @@ class Admin:
await ctx.send(f'{channel} is not a valid text channel in this guild.')
else:
admin_log.info('Chan found')
if self.bot.con.one('select allowed_channels from guild_config where guild_id = %(id)s',
{'id': ctx.guild.id}):
if chnl.id in json.loads(self.bot.con.one('select allowed_channels from guild_config '
'where guild_id = %(id)s',
{'id': ctx.guild.id})):
if self.bot.db_con.fetchval('select allowed_channels from guild_config where guild_id = $1',
ctx.guild.id):
if chnl.id in self.bot.con.fetchval('select allowed_channels from guild_config '
'where guild_id = $1',
ctx.guild.id):
admin_log.info('Chan found in config')
await ctx.send(f'{channel} is already in the list of allowed channels. Skipping...')
else:
admin_log.info('Chan not found in config')
allowed_channels = json.loads(self.bot.con.one('select allowed_channels from '
'guild_config where guild_id = %(id)s',
{'id': ctx.guild.id})).append(chnl.id)
self.bot.con.run('update guild_config set allowed_channels = %(channels)s '
'where guild_id = %(id)s',
{'id': ctx.guild.id, 'channels': allowed_channels})
allowed_channels = self.bot.db_con.fetchval('select allowed_channels from '
'guild_config where guild_id = $1',
ctx.guild.id).append(chnl.id)
self.bot.db_con.execute('update guild_config set allowed_channels = $2 '
'where guild_id = $1',
ctx.guild.id, allowed_channels)
added = f'{added}\n{channel}'
else:
admin_log.info('Chan not found in config')
allowed_channels = [chnl.id]
self.bot.con.run('update guild_config set allowed_channels = %(channels)s '
'where guild_id = %(id)s',
{'id': ctx.guild.id, 'channels': allowed_channels})
self.bot.db_con.execute('update guild_config set allowed_channels = $2 '
'where guild_id = $1',
ctx.guild.id, allowed_channels)
added = f'{added}\n{channel}'
if added != '':
await ctx.send(f'The following channels have been added to the allowed channel list: {added}')
@ -136,6 +133,42 @@ class Admin:
for page in pages:
await ctx.send(page)
@add.command(name='admin_role', aliases=['admin'])
@commands.cooldown(1, 5, type=commands.BucketType.guild)
@commands.check(checks.is_guild_owner)
async def _add_admin_role(self, ctx, role=None):
role = discord.utils.get(ctx.guild.roles, name=role)
if role is not None:
roles = self.bot.db_con.fetchval('select admin_roles from guild_config where guild_id = $1',
ctx.guild.id)
if role.id in roles:
await ctx.send(f'{role.name} is already registered as an admin role in this guild.')
else:
roles.append(role.id)
self.bot.db_con.execute('update guild_config set admin_roles = $2 where guild_id = $1',
ctx.guild.id, roles)
await ctx.send(f'{role.name} has been added to the list of admin roles for this guild.')
else:
await ctx.send('You must include a valid role name with this command.')
@remove.command(name='admin_role', aliases=['admin'])
@commands.cooldown(1, 5, type=commands.BucketType.guild)
@commands.check(checks.is_guild_owner)
async def _remove_admin_role(self, ctx, role=None):
role = discord.utils.get(ctx.guild.roles, name=role)
if role is not None:
roles = self.bot.db_con.fetchval('select admin_roles from guild_config where guild_id = $1',
ctx.guild.id)
if role.id in roles:
roles.remove(role.id)
self.bot.db_con.execute('update guild_config set admin_roles = $2 where guild_id = $1',
ctx.guild.id, roles)
await ctx.send(f'{role.name} has been removed from the list of admin roles for this guild.')
else:
await ctx.send(f'{role.name} is not registered as an admin role in this guild.')
else:
await ctx.send('You must include a valid role name with this command.')
def setup(bot):
bot.add_cog(Admin(bot))

View File

@ -9,8 +9,8 @@ class BotEvents:
self.bot = bot
async def on_guild_join(self, guild):
await self.bot.db_con.execute("insert into guild_config(guild_id, channel_lockdown) values ($1, $2)",
guild.id, False)
await self.bot.db_con.execute("insert into guild_config(guild_id, channel_lockdown, admin_roles) "
"values ($1, $2, $3)", guild.id, False, guild.role_hierarchy[0])
events_log.info(f'Entry Created for {guild.name}')
await guild.me.edit(nick='[!] Submitter')