From 76574f94a7978b719084f2b5083606955866aaf3 Mon Sep 17 00:00:00 2001 From: "Dusty.P" Date: Mon, 16 Apr 2018 01:37:53 -0800 Subject: [PATCH 1/7] Began conversion to asyncpg for DB --- geeksbot.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/geeksbot.py b/geeksbot.py index 676533b..4107ada 100644 --- a/geeksbot.py +++ b/geeksbot.py @@ -8,6 +8,8 @@ import aiohttp from postgres import Postgres from collections import deque from googleapiclient.discovery import build +import asyncpg + log_format = '{asctime}.{msecs:03.0f}|{levelname:<8}|{name}::{message}' date_format = '%Y.%m.%d %H.%M.%S' @@ -55,20 +57,20 @@ class Geeksbot(commands.Bot): self.infected = {} self.TOKEN = self.bot_secrets['token'] del self.bot_secrets['token'] - self.con = Postgres(f" host={self.bot_secrets['db_con']['host']}\ - port={self.bot_secrets['db_con']['port']}\ - dbname={self.bot_secrets['db_con']['db_name']}\ - connect_timeout=10 user={self.bot_secrets['db_con']['user']}\ - password={self.bot_secrets['db_con']['password']}") + self.db_con = asyncpg.connect(f"host={self.bot_secrets['db_con']['host']}\ + database={self.bot_secrets['db_con']['db_name']}\ + user={self.bot_secrets['db_con']['user']}\ + password={self.bot_secrets['db_con']['password']}") del self.bot_secrets['db_con'] self.default_prefix = 'g$' self.voice_chans = {} self.spam_list = {} self.gcs_service = build('customsearch', 'v1', developerKey=self.bot_secrets['google_search_key']) - async def get_custom_prefix(self, bot_inst, message): - return self.con.one('select prefix from guild_config where guild_id = %(id)s', {'id': message.guild.id})\ - or self.default_prefix + @staticmethod + async def get_custom_prefix(bot_inst, message): + return bot_inst.db_con.fetch('select prefix from guild_config where guild_id = $1', + message.guild.id) or bot_inst.default_prefix async def load_ext(self, ctx, mod=None): self.load_extension('{0}.{1}'.format(extension_dir, mod)) @@ -120,14 +122,14 @@ async def unload(ctx, mod): async def on_message(ctx): if not ctx.author.bot: if ctx.guild: - if int(bot.con.one(f"select channel_lockdown from guild_config where guild_id = %(id)s", - {'id': ctx.guild.id})): - if ctx.channel.id in json.loads(bot.con.one(f"select allowed_channels from guild_config " - f"where guild_id = %(id)s", - {'id': ctx.guild.id})): + if int(bot.db_con.fetchrow("select channel_lockdown from guild_config where guild_id = $1", + ctx.guild.id)): + if ctx.channel.id in json.loads(bot.db_con.fetchrow("select allowed_channels from guild_config " + "where guild_id = $1", + ctx.guild.id)): await bot.process_commands(ctx) elif ctx.channel.id == 418452585683484680: - prefix = bot.con.one('select prefix from guild_config where guild_id = %(id)s', {'id': ctx.guild.id}) + prefix = bot.db_con.fetchrow('select prefix from guild_config where guild_id = $1', ctx.guild.id) prefix = prefix[0] if prefix else bot.default_prefix ctx.content = f'{prefix}{ctx.content}' await bot.process_commands(ctx) From 61f632a359035335bed5aa1bb98c3a5ae0c8dae5 Mon Sep 17 00:00:00 2001 From: "Dusty.P" Date: Fri, 20 Apr 2018 00:21:29 -0800 Subject: [PATCH 2/7] Added TODOs --- exts/imports/utils.py | 2 ++ exts/utils.py | 3 +++ 2 files changed, 5 insertions(+) diff --git a/exts/imports/utils.py b/exts/imports/utils.py index ace473b..cb067fe 100644 --- a/exts/imports/utils.py +++ b/exts/imports/utils.py @@ -91,3 +91,5 @@ async def run_command(args): stdout, stderr = await process.communicate() # Return stdout return stdout.decode().strip() + +# TODO Add Paginator diff --git a/exts/utils.py b/exts/utils.py index 38525c5..43e0881 100644 --- a/exts/utils.py +++ b/exts/utils.py @@ -501,6 +501,7 @@ class Utils: em.title = f'Google Search' em.description = f'Top 4 results for "{search}"' em.colour = embed_color + # TODO Fix layout of Results for result in results: em.add_field(name=f'{result["title"]}', value=f'{result["snippet"]}\n{result["link"]}') await ctx.send(embed=em) @@ -530,6 +531,8 @@ class Utils: value=f'Steam ID: {steam[i]}\nPatreon Level: {tier[i]}\nPatron of: {patron[i]}') await ctx.send(embed=em) +# TODO Create Help command + def setup(bot): bot.add_cog(Utils(bot)) From 6d49bcea36d68fd9a466634de7470506a626dff9 Mon Sep 17 00:00:00 2001 From: "Dusty.P" Date: Fri, 20 Apr 2018 19:33:20 -0800 Subject: [PATCH 3/7] Switching to fetchval --- geeksbot.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/geeksbot.py b/geeksbot.py index 4107ada..081851a 100644 --- a/geeksbot.py +++ b/geeksbot.py @@ -62,15 +62,15 @@ class Geeksbot(commands.Bot): user={self.bot_secrets['db_con']['user']}\ password={self.bot_secrets['db_con']['password']}") del self.bot_secrets['db_con'] - self.default_prefix = 'g$' + self.default_prefix = 'g~' self.voice_chans = {} self.spam_list = {} self.gcs_service = build('customsearch', 'v1', developerKey=self.bot_secrets['google_search_key']) @staticmethod async def get_custom_prefix(bot_inst, message): - return bot_inst.db_con.fetch('select prefix from guild_config where guild_id = $1', - message.guild.id) or bot_inst.default_prefix + return bot_inst.db_con.fetchval('select prefix from guild_config where guild_id = $1', + message.guild.id) or bot_inst.default_prefix async def load_ext(self, ctx, mod=None): self.load_extension('{0}.{1}'.format(extension_dir, mod)) @@ -122,14 +122,14 @@ async def unload(ctx, mod): async def on_message(ctx): if not ctx.author.bot: if ctx.guild: - if int(bot.db_con.fetchrow("select channel_lockdown from guild_config where guild_id = $1", + if int(bot.db_con.fetchval("select channel_lockdown from guild_config where guild_id = $1", ctx.guild.id)): - if ctx.channel.id in json.loads(bot.db_con.fetchrow("select allowed_channels from guild_config " + if ctx.channel.id in json.loads(bot.db_con.fetchval("select allowed_channels from guild_config " "where guild_id = $1", ctx.guild.id)): await bot.process_commands(ctx) elif ctx.channel.id == 418452585683484680: - prefix = bot.db_con.fetchrow('select prefix from guild_config where guild_id = $1', ctx.guild.id) + prefix = bot.db_con.fetchval('select prefix from guild_config where guild_id = $1', ctx.guild.id) prefix = prefix[0] if prefix else bot.default_prefix ctx.content = f'{prefix}{ctx.content}' await bot.process_commands(ctx) From 3a43e18c61a94a953e81bebfb35007fb38716ef1 Mon Sep 17 00:00:00 2001 From: "Dusty.P" Date: Fri, 20 Apr 2018 19:33:20 -0800 Subject: [PATCH 4/7] Switching to fetchval --- geeksbot.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/geeksbot.py b/geeksbot.py index 4107ada..0102e79 100644 --- a/geeksbot.py +++ b/geeksbot.py @@ -62,15 +62,15 @@ class Geeksbot(commands.Bot): user={self.bot_secrets['db_con']['user']}\ password={self.bot_secrets['db_con']['password']}") del self.bot_secrets['db_con'] - self.default_prefix = 'g$' + self.default_prefix = 'g~' self.voice_chans = {} self.spam_list = {} self.gcs_service = build('customsearch', 'v1', developerKey=self.bot_secrets['google_search_key']) @staticmethod async def get_custom_prefix(bot_inst, message): - return bot_inst.db_con.fetch('select prefix from guild_config where guild_id = $1', - message.guild.id) or bot_inst.default_prefix + return bot_inst.db_con.fetchval('select prefix from guild_config where guild_id = $1', + message.guild.id) or bot_inst.default_prefix async def load_ext(self, ctx, mod=None): self.load_extension('{0}.{1}'.format(extension_dir, mod)) @@ -122,14 +122,14 @@ async def unload(ctx, mod): async def on_message(ctx): if not ctx.author.bot: if ctx.guild: - if int(bot.db_con.fetchrow("select channel_lockdown from guild_config where guild_id = $1", + if int(bot.db_con.fetchval("select channel_lockdown from guild_config where guild_id = $1", ctx.guild.id)): - if ctx.channel.id in json.loads(bot.db_con.fetchrow("select allowed_channels from guild_config " + if ctx.channel.id in json.loads(bot.db_con.fetchval("select allowed_channels from guild_config " "where guild_id = $1", ctx.guild.id)): await bot.process_commands(ctx) elif ctx.channel.id == 418452585683484680: - prefix = bot.db_con.fetchrow('select prefix from guild_config where guild_id = $1', ctx.guild.id) + prefix = bot.db_con.fetchval('select prefix from guild_config where guild_id = $1', ctx.guild.id) prefix = prefix[0] if prefix else bot.default_prefix ctx.content = f'{prefix}{ctx.content}' await bot.process_commands(ctx) @@ -149,12 +149,12 @@ async def on_ready(): for load_item in load_list: await bot.load_ext(None, f'{load_item}') logging.info('Extension Loaded: {0}'.format(load_item)) - logging.info('Done loading, Geeksbot is active.') with open(f'{config_dir}reboot', 'r') as f: reboot = f.readlines() if int(reboot[0]) == 1: await bot.get_channel(int(reboot[1])).send('Restart Finished.') with open(f'{config_dir}reboot', 'w') as f: f.write(f'0') + logging.info('Done loading, Geeksbot is active.') bot.run(bot.TOKEN) From 9ef06824a7ca5b1314c0c6e9dc77c318b86a79ec Mon Sep 17 00:00:00 2001 From: "Dusty.P" Date: Fri, 20 Apr 2018 19:49:01 -0800 Subject: [PATCH 5/7] Switching to fetchval --- geeksbot.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/geeksbot.py b/geeksbot.py index 0102e79..f8894bb 100644 --- a/geeksbot.py +++ b/geeksbot.py @@ -69,8 +69,8 @@ class Geeksbot(commands.Bot): @staticmethod async def get_custom_prefix(bot_inst, message): - return bot_inst.db_con.fetchval('select prefix from guild_config where guild_id = $1', - message.guild.id) or bot_inst.default_prefix + return await bot_inst.db_con.fetchval('select prefix from guild_config where guild_id = $1', + message.guild.id) or bot_inst.default_prefix async def load_ext(self, ctx, mod=None): self.load_extension('{0}.{1}'.format(extension_dir, mod)) @@ -122,14 +122,14 @@ async def unload(ctx, mod): async def on_message(ctx): if not ctx.author.bot: if ctx.guild: - if int(bot.db_con.fetchval("select channel_lockdown from guild_config where guild_id = $1", - ctx.guild.id)): - if ctx.channel.id in json.loads(bot.db_con.fetchval("select allowed_channels from guild_config " - "where guild_id = $1", - ctx.guild.id)): + if int(await bot.db_con.fetchval("select channel_lockdown from guild_config where guild_id = $1", + ctx.guild.id)): + if ctx.channel.id in json.loads(await bot.db_con.fetchval("select allowed_channels from guild_config " + "where guild_id = $1", + ctx.guild.id)): await bot.process_commands(ctx) elif ctx.channel.id == 418452585683484680: - prefix = bot.db_con.fetchval('select prefix from guild_config where guild_id = $1', ctx.guild.id) + prefix = await bot.db_con.fetchval('select prefix from guild_config where guild_id = $1', ctx.guild.id) prefix = prefix[0] if prefix else bot.default_prefix ctx.content = f'{prefix}{ctx.content}' await bot.process_commands(ctx) From 6d8f3531a7855243dc8206c24289d3683e73523c Mon Sep 17 00:00:00 2001 From: "Dusty.P" Date: Fri, 20 Apr 2018 20:11:55 -0800 Subject: [PATCH 6/7] Switching to fetchval --- exts/repl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exts/repl.py b/exts/repl.py index 8156c38..fef6ead 100644 --- a/exts/repl.py +++ b/exts/repl.py @@ -156,7 +156,7 @@ class Repl: if ctx.author.id != ownerid: return try: - body = self.cleanup_code(body).split(' ') + body = self.cleanup_code(body) result = await asyncio.wait_for(self.bot.loop.create_task(run_command(body)), 10) value = result for page in paginate(value): From 005f4ca25d7faf69a1e21f091d640ff2cc0035ef Mon Sep 17 00:00:00 2001 From: "Dusty.P" Date: Fri, 20 Apr 2018 20:23:02 -0800 Subject: [PATCH 7/7] Fixed Git Pull --- exts/git.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/exts/git.py b/exts/git.py index 5fcc10e..b89f14c 100644 --- a/exts/git.py +++ b/exts/git.py @@ -28,7 +28,9 @@ class Git: em.set_thumbnail(url=f'{ctx.guild.me.avatar_url}') result = await asyncio.wait_for(self.bot.loop.create_task(run_command('git fetch --all')), 120) + '\n' result += await asyncio.wait_for(self.bot.loop.create_task(run_command('git reset --hard ' - 'origin/master')), 120) + '\n\n' + 'origin/$(git ' + 'rev-parse --symbolic-full-name' + '--abbrev-ref HEAD)')), 120) + '\n\n' result += await asyncio.wait_for(self.bot.loop.create_task(run_command('git show --stat | ' 'sed "s/.*@.*[.].*/ /g"')), 10) results = paginate(result, maxlen=1014)