From e7c2cdceb7c2116b3477b8da98beb014a59021d2 Mon Sep 17 00:00:00 2001 From: phxntxm Date: Sun, 23 Sep 2018 01:04:03 -0500 Subject: [PATCH] Change database saving, and command reading to not create tasks each time --- bot.py | 7 +---- cogs/admin.py | 58 ++++++++++++++++++++--------------------- cogs/birthday.py | 6 ++--- cogs/interaction.py | 2 +- cogs/osu.py | 2 +- cogs/overwatch.py | 4 +-- cogs/picarto.py | 16 ++++++------ cogs/raffle.py | 13 ++++----- cogs/roles.py | 4 +-- cogs/tags.py | 6 ++--- cogs/twitch.py | 16 ++++++------ cogs/utils/database.py | 9 ++++--- cogs/utils/utilities.py | 4 +-- 13 files changed, 71 insertions(+), 76 deletions(-) diff --git a/bot.py b/bot.py index 9dd9306..a8f565d 100644 --- a/bot.py +++ b/bot.py @@ -38,11 +38,6 @@ async def on_message(message): @bot.event async def on_command_completion(ctx): - # There's no reason to continue waiting for this to complete, so lets immediately launch this in a new future - bot.loop.create_task(process_command(ctx)) - - -async def process_command(ctx): author = ctx.message.author server = ctx.message.guild command = ctx.command @@ -68,7 +63,7 @@ async def process_command(ctx): command_usage['server_usage'] = total_server_usage # Save all the changes - bot.db.save('command_usage', command_usage) + await bot.db.save('command_usage', command_usage) @bot.event diff --git a/cogs/admin.py b/cogs/admin.py index 3ced113..fc1a640 100644 --- a/cogs/admin.py +++ b/cogs/admin.py @@ -58,11 +58,10 @@ class Administration: 'server_id': str(ctx.message.guild.id), 'battles': msgs } - self.bot.db.save('server_settings', update) + await self.bot.db.save('server_settings', update) fmt = "I have just saved your new battle message, it will appear like this: \n\n*{}*".format(message) await ctx.send(fmt.format(loser=ctx.message.author.display_name, winner=ctx.message.guild.me.display_name)) - @battles.command(name='remove', aliases=['delete']) @commands.guild_only() @utils.custom_perms(manage_guild=True) @@ -77,6 +76,7 @@ class Administration: # Then let them know to respond with the number needed await ctx.send("Please respond with the number matching the battle message you want to remove") # The check to ensure it's in this channel...and what's provided is an int + def check(m): if m.author == ctx.message.author and m.channel == ctx.message.channel: try: @@ -107,10 +107,9 @@ class Administration: 'server_id': str(ctx.message.guild.id), 'battles': msgs } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) await ctx.send("I have just removed that battle message") - @battles.command(name='default') @commands.guild_only() @utils.custom_perms(send_messages=True) @@ -130,7 +129,7 @@ class Administration: 'server_id': str(ctx.message.guild.id), 'default_battles': setting } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) fmt = "" if setting else "not " await ctx.send("Default messages will {}be used as well as custom messages".format(fmt)) @@ -176,11 +175,10 @@ class Administration: 'server_id': str(ctx.message.guild.id), 'hugs': msgs } - self.bot.db.save('server_settings', update) + await self.bot.db.save('server_settings', update) fmt = "I have just saved your new hug message, it will appear like this: \n\n*{}*".format(message) await ctx.send(fmt.format(user=ctx.message.author.display_name)) - @hugs.command(name='remove', aliases=['delete']) @commands.guild_only() @utils.custom_perms(manage_guild=True) @@ -195,6 +193,7 @@ class Administration: # Then let them know to respond with the number needed await ctx.send("Please respond with the number matching the hug message you want to remove") # The check to ensure it's in this channel...and what's provided is an int + def check(m): if m.author == ctx.message.author and m.channel == ctx.message.channel: try: @@ -225,10 +224,9 @@ class Administration: 'server_id': str(ctx.message.guild.id), 'hugs': msgs } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) await ctx.send("I have just removed that hug message") - @hugs.command(name='default') @commands.guild_only() @utils.custom_perms(send_messages=True) @@ -248,7 +246,7 @@ class Administration: 'server_id': str(ctx.message.guild.id), 'default_hugs': setting } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) fmt = "" if setting else "not " await ctx.send("Default messages will {}be used as well as custom messages".format(fmt)) @@ -269,7 +267,7 @@ class Administration: 'server_id': str(ctx.message.guild.id), 'birthdays_allowed': allowed } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) fmt = "The birthday announcements have just been turned {}".format("on" if allowed else "off") await ctx.send(fmt) @@ -290,7 +288,7 @@ class Administration: 'server_id': str(ctx.message.guild.id), 'colour_roles_allowed': allowed } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) fmt = "The ability to use colour roles have just been turned {}".format("on" if allowed else "off") await ctx.send(fmt) @@ -311,7 +309,7 @@ class Administration: 'server_id': str(ctx.message.guild.id), 'playlists_allowed': allowed } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) fmt = "The ability to use playlists has just been turned {}".format("on" if allowed else "off") await ctx.send(fmt) @@ -557,7 +555,7 @@ class Administration: 'to': to } } - self.bot.db.save('server_settings', update) + await self.bot.db.save('server_settings', update) elif from_entry: restrictions = self.bot.db.load('server_settings', key=ctx.message.guild.id, pluck='restrictions') or {} _from = restrictions.get('from', []) @@ -569,7 +567,7 @@ class Administration: 'from': _from } } - self.bot.db.save('server_settings', update) + await self.bot.db.save('server_settings', update) elif overwrites: channel = overwrites.pop('channel') for target, setting in overwrites.items(): @@ -644,7 +642,7 @@ class Administration: 'server_id': str(ctx.message.guild.id), 'restrictions': restrictions } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) # If this isn't a blacklist/whitelist, then we are attempting to remove an overwrite else: # Get the source and destination based on whatever order is provided @@ -743,7 +741,7 @@ class Administration: 'server_id': str(key) } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) await ctx.send(fmt) @commands.command() @@ -792,7 +790,7 @@ class Administration: 'server_id': str(key) } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) await ctx.send(fmt) @commands.command(aliases=['notifications']) @@ -814,7 +812,7 @@ class Administration: } } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) await ctx.send("I have just changed this server's default 'notifications' channel" "\nAll notifications will now default to `{}`".format(channel)) @@ -838,7 +836,7 @@ class Administration: 'join_leave': on_off } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) fmt = "notify" if on_off else "not notify" await ctx.send("This server will now {} if someone has joined or left".format(fmt)) @@ -858,7 +856,7 @@ class Administration: } } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) await ctx.send( "I have just changed this server's welcome/goodbye notifications channel to {}".format(channel.name)) @@ -882,7 +880,7 @@ class Administration: await ctx.send("Illegal content in {} message".format(parent)) else: try: - test_keys = msg.format(member='test', server='test') + msg.format(member='test', server='test') except KeyError: await ctx.send("Illegal keyword in {0} message. Please use `{1.prefix}help {0} message` " "for what keywords can be used".format(parent, ctx)) @@ -891,7 +889,7 @@ class Administration: 'server_id': str(ctx.message.guild.id), parent + '_message': msg } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) await ctx.send("I have just updated your {} message".format(parent)) @commands.group() @@ -923,7 +921,7 @@ class Administration: 'nsfw_channels': channels } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) await ctx.send("This channel has just been registered as 'nsfw'! Have fun you naughties ;)") @@ -949,7 +947,7 @@ class Administration: 'server_id': key, 'nsfw_channels': channels } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) await ctx.send("This channel has just been unregistered as a nsfw channel") else: await ctx.send("This channel is not registerred as a nsfw channel!") @@ -1086,7 +1084,7 @@ class Administration: 'permissions': {cmd.qualified_name: perm_value} } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) await ctx.send("I have just added your custom permissions; " "you now need to have `{}` permissions to use the command `{}`".format(permissions, command)) @@ -1113,7 +1111,7 @@ class Administration: 'permissions': {cmd.qualified_name: None} } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) await ctx.send("I have just removed the custom permissions for {}!".format(cmd)) @commands.command() @@ -1137,7 +1135,7 @@ class Administration: 'prefix': prefix } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) if prefix is None: fmt = "I have just cleared your custom prefix, the default prefix will have to be used now" @@ -1194,7 +1192,7 @@ class Administration: 'rules': rules } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) await ctx.send("I have just saved your new rule, use the rules command to view this server's current rules") @@ -1216,7 +1214,7 @@ class Administration: 'server_id': key, 'rules': rules } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) await ctx.send("I have just removed that rule from your list of rules!") except IndexError: await ctx.send("That is not a valid rule number, try running the command again.") diff --git a/cogs/birthday.py b/cogs/birthday.py index 940b1f4..6853273 100644 --- a/cogs/birthday.py +++ b/cogs/birthday.py @@ -154,7 +154,7 @@ class Birthday: 'member_id': str(ctx.message.author.id), 'birthday': date } - self.bot.db.save('birthdays', entry) + await self.bot.db.save('birthdays', entry) await ctx.send("I have just saved your birthday as {}".format(date)) @birthday.command(name='remove') @@ -169,7 +169,7 @@ class Birthday: 'member_id': str(ctx.message.author.id), 'birthday': None } - self.bot.db.save('birthdays', entry) + await self.bot.db.save('birthdays', entry) await ctx.send("I don't know your birthday anymore :(") @birthday.command(name='alerts', aliases=['notifications']) @@ -188,7 +188,7 @@ class Birthday: 'birthday': str(channel.id) } } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) await ctx.send("All birthday notifications will now go to {}".format(channel.mention)) diff --git a/cogs/interaction.py b/cogs/interaction.py index 3eb7f8d..50f43fe 100644 --- a/cogs/interaction.py +++ b/cogs/interaction.py @@ -387,7 +387,7 @@ class Interaction: str(boopee.id): amount } } - self.bot.db.save('boops', entry) + await self.bot.db.save('boops', entry) fmt = "{0.mention} has just booped {1.mention}{3}! That's {2} times now!" await ctx.send(fmt.format(booper, boopee, amount, message)) diff --git a/cogs/osu.py b/cogs/osu.py index 3db985a..a9bf495 100644 --- a/cogs/osu.py +++ b/cogs/osu.py @@ -107,7 +107,7 @@ class Osu: 'osu_username': user.username } - self.bot.db.save('osu', entry) + await self.bot.db.save('osu', entry) await ctx.send("I have just saved your Osu user {}".format(author.display_name)) diff --git a/cogs/overwatch.py b/cogs/overwatch.py index 1cbddeb..91a0a03 100644 --- a/cogs/overwatch.py +++ b/cogs/overwatch.py @@ -119,7 +119,7 @@ class Overwatch: 'battletag': bt } - self.bot.db.save('overwatch', entry) + await self.bot.db.save('overwatch', entry) await ctx.send("I have just saved your battletag {}".format(ctx.message.author.mention)) @ow.command(name="delete", aliases=['remove']) @@ -134,7 +134,7 @@ class Overwatch: 'member_id': str(ctx.message.author.id), 'battletag': None } - self.bot.db.save('overwatch', entry) + await self.bot.db.save('overwatch', entry) await ctx.send("I no longer have your battletag saved {}".format(ctx.message.author.mention)) diff --git a/cogs/picarto.py b/cogs/picarto.py index 02c4e82..55b1f2d 100644 --- a/cogs/picarto.py +++ b/cogs/picarto.py @@ -90,11 +90,11 @@ class Picarto: # If they're currently online, but saved as not then we'll let servers know they are now online if online and data['live'] == 0: msg = "{member.display_name} has just gone live!" - self.bot.db.save('picarto', {'live': 1, 'member_id': str(m_id)}) + await self.bot.db.save('picarto', {'live': 1, 'member_id': str(m_id)}) # Otherwise our notification will say they've gone offline elif not online and data['live'] == 1: msg = "{member.display_name} has just gone offline!" - self.bot.db.save('picarto', {'live': 0, 'member_id': str(m_id)}) + await self.bot.db.save('picarto', {'live': 0, 'member_id': str(m_id)}) else: continue @@ -200,7 +200,7 @@ class Picarto: 'live': 0, 'member_id': key } - self.bot.db.save('picarto', entry) + await self.bot.db.save('picarto', entry) await ctx.send( "I have just saved your Picarto URL {}, this guild will now be notified when you go live".format( ctx.message.author.mention)) @@ -219,7 +219,7 @@ class Picarto: 'member_id': str(ctx.message.author.id) } - self.bot.db.save('picarto', entry) + await self.bot.db.save('picarto', entry) await ctx.send("I am no longer saving your picarto URL {}".format(ctx.message.author.mention)) else: await ctx.send("I cannot remove something that I don't have (you've never saved your Picarto URL)") @@ -240,7 +240,7 @@ class Picarto: 'picarto': str(channel.id) } } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) await ctx.send("All Picarto notifications will now go to {}".format(channel.mention)) @picarto.group(invoke_without_command=True) @@ -269,7 +269,7 @@ class Picarto: 'member_id': key, 'servers': servers } - self.bot.db.save('picarto', entry) + await self.bot.db.save('picarto', entry) await ctx.send("This server will now be notified if you go live") @notify.command(name='on', aliases=['start,yes']) @@ -288,7 +288,7 @@ class Picarto: 'member_id': key, 'notifications_on': 1 } - self.bot.db.save('picarto', entry) + await self.bot.db.save('picarto', entry) await ctx.send("I will notify if you go live {}, you'll get a bajillion followers I promise c:".format( ctx.message.author.mention)) else: @@ -310,7 +310,7 @@ class Picarto: 'member_id': key, 'notifications_on': 0 } - self.bot.db.save('picarto', entry) + await self.bot.db.save('picarto', entry) await ctx.send( "I will not notify if you go live anymore {}, " "are you going to stream some lewd stuff you don't want people to see?~".format( diff --git a/cogs/raffle.py b/cogs/raffle.py index 37361c4..4ba092c 100644 --- a/cogs/raffle.py +++ b/cogs/raffle.py @@ -94,7 +94,7 @@ class Raffle: 'server_id': raffle['server_id'], 'raffles': raffle['raffles'] } - self.bot.db.save('raffles', entry) + await self.bot.db.save('raffles', entry) @commands.command() @commands.guild_only() @@ -157,7 +157,7 @@ class Raffle: 'raffles': raffles, 'server_id': key } - self.bot.db.save('raffles', update) + await self.bot.db.save('raffles', update) await ctx.send("{} you have just entered the raffle!".format(author.mention)) # Otherwise, make sure the author gave a valid raffle_id elif raffle_id in range(raffle_count): @@ -175,12 +175,13 @@ class Raffle: 'raffles': raffles, 'server_id': key } - self.bot.db.save('raffles', update) + await self.bot.db.save('raffles', update) await ctx.send("{} you have just entered the raffle!".format(author.mention)) else: fmt = "Please provide a valid raffle ID, as there are more than one setup on the server! " \ "There are currently `{}` raffles running, use {}raffles to view the current running raffles".format( - raffle_count, ctx.prefix) + raffle_count, ctx.prefix + ) await ctx.send(fmt) @raffle.command(name='create', aliases=['start', 'begin', 'add']) @@ -275,7 +276,7 @@ class Raffle: 'server_id': str(server.id), 'raffles': raffles } - self.bot.db.save('raffles', update) + await self.bot.db.save('raffles', update) await ctx.send("I have just saved your new raffle!") @raffle.command(name='alerts') @@ -294,7 +295,7 @@ class Raffle: 'raffle': str(channel.id) } } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) await ctx.send("All raffle notifications will now go to {}".format(channel.mention)) diff --git a/cogs/roles.py b/cogs/roles.py index 8c05025..4e90e57 100644 --- a/cogs/roles.py +++ b/cogs/roles.py @@ -452,7 +452,7 @@ class Roles: 'self_assignable_roles': self_assignable_roles } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) if len(roles) == 1: fmt = "Successfully added {} as a self-assignable role".format(role[0].name) @@ -522,7 +522,7 @@ class Roles: 'self_assignable_roles': self_assignable_roles, 'server_id': key } - self.bot.db.save('server_settings', update) + await self.bot.db.save('server_settings', update) await ctx.send(fmt) diff --git a/cogs/tags.py b/cogs/tags.py index 3491907..28ad46f 100644 --- a/cogs/tags.py +++ b/cogs/tags.py @@ -147,7 +147,7 @@ class Tags: 'server_id': str(ctx.message.guild.id), 'tags': tags } - self.bot.db.save('tags', entry) + await self.bot.db.save('tags', entry) await ctx.send("I have just setup a new tag for this server! You can call your tag with {}".format(trigger)) @tag.command(name='edit') @@ -186,7 +186,7 @@ class Tags: 'server_id': str(ctx.message.guild.id), 'tags': tags } - self.bot.db.save('tags', entry) + await self.bot.db.save('tags', entry) await ctx.send("Alright, the tag {} has been updated".format(tag)) return else: @@ -217,7 +217,7 @@ class Tags: 'server_id': str(ctx.message.guild.id), 'tags': tags } - self.bot.db.save('tags', entry) + await self.bot.db.save('tags', entry) await ctx.send("I have just removed the tag {}".format(tag)) else: await ctx.send("You don't own that tag! You can't remove it!") diff --git a/cogs/twitch.py b/cogs/twitch.py index eedf394..37d5c51 100644 --- a/cogs/twitch.py +++ b/cogs/twitch.py @@ -104,12 +104,12 @@ class Twitch: # If they're currently online, but saved as not then we'll let servers know they are now online if embed and data['live'] == 0: msg = "{member.display_name} has just gone live!" - self.bot.db.save('twitch', {'live': 1, 'member_id': str(m_id)}) + await self.bot.db.save('twitch', {'live': 1, 'member_id': str(m_id)}) # Otherwise our notification will say they've gone offline elif not embed and data['live'] == 1: msg = "{member.display_name} has just gone offline!" embed = await self.offline_embed(url) - self.bot.db.save('twitch', {'live': 0, 'member_id': str(m_id)}) + await self.bot.db.save('twitch', {'live': 0, 'member_id': str(m_id)}) else: continue @@ -214,7 +214,7 @@ class Twitch: 'live': 0, 'member_id': key } - self.bot.db.save('twitch', entry) + await self.bot.db.save('twitch', entry) await ctx.send("I have just saved your twitch url {}".format(ctx.message.author.mention)) @twitch.command(name='remove', aliases=['delete']) @@ -231,7 +231,7 @@ class Twitch: 'member_id': str(ctx.message.author.id) } - self.bot.db.save('twitch', entry) + await self.bot.db.save('twitch', entry) await ctx.send("I am no longer saving your twitch URL {}".format(ctx.message.author.mention)) @twitch.command(name='alerts', aliases=['notifications']) @@ -250,7 +250,7 @@ class Twitch: 'twitch': str(channel.id) } } - self.bot.db.save('server_settings', entry) + await self.bot.db.save('server_settings', entry) await ctx.send("All Twitch notifications will now go to {}".format(channel.mention)) @twitch.group(invoke_without_command=True) @@ -279,7 +279,7 @@ class Twitch: 'member_id': key, 'servers': servers } - self.bot.db.save('twitch', entry) + await self.bot.db.save('twitch', entry) await ctx.send("This server will now be notified if you go live") @notify.command(name='on', aliases=['start,yes']) @@ -298,7 +298,7 @@ class Twitch: 'member_id': key, 'notifications_on': 1 } - self.bot.db.save('twitch', entry) + await self.bot.db.save('twitch', entry) await ctx.send("I will notify if you go live {}, you'll get a bajillion followers I promise c:".format( ctx.message.author.mention)) else: @@ -320,7 +320,7 @@ class Twitch: 'member_id': key, 'notifications_on': 0 } - self.bot.db.save('twitch', entry) + await self.bot.db.save('twitch', entry) await ctx.send( "I will not notify if you go live anymore {}, " "are you going to stream some lewd stuff you don't want people to see?~".format( diff --git a/cogs/utils/database.py b/cogs/utils/database.py index 2898989..08e9b86 100644 --- a/cogs/utils/database.py +++ b/cogs/utils/database.py @@ -52,6 +52,7 @@ class Cache: if key is None and table_filter is None: return self.values elif key: + key = str(key) for value in self.values: if value[self.key] == key: if pluck: @@ -94,11 +95,11 @@ class DB: await conn.close() return cursor - def save(self, table, content): - """A synchronous task to throw saving content into a task""" - self.loop.create_task(self._save(table, content)) +# def save(self, table, content): +# """A synchronous task to throw saving content into a task""" +# self.loop.create_task(self._save(table, content)) - async def _save(self, table, content): + async def save(self, table, content): """Saves data in the table""" index = await self.query(r.table(table).info()) diff --git a/cogs/utils/utilities.py b/cogs/utils/utilities.py index 1f6a16f..e8e1cc5 100644 --- a/cogs/utils/utilities.py +++ b/cogs/utils/utilities.py @@ -184,5 +184,5 @@ async def update_records(key, db, winner, loser): winner_stats = {'wins': winner_wins, 'losses': winner_losses, 'rating': winner_rating, 'member_id': str(winner.id)} loser_stats = {'wins': loser_wins, 'losses': loser_losses, 'rating': loser_rating, 'member_id': str(loser.id)} - db.save(key, winner_stats) - db.save(key, loser_stats) + await db.save(key, winner_stats) + await db.save(key, loser_stats)