From 8c90220eab3decdc674681db26b02c18c556ba90 Mon Sep 17 00:00:00 2001 From: phxntxm Date: Sun, 11 Sep 2016 03:28:57 -0500 Subject: [PATCH 1/4] Corrected some of the logic behind cache; fixed an error that caused our cache object to be deleted and replaced by the dictionary that sould have been saved to it's value --- cogs/utils/config.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/cogs/utils/config.py b/cogs/utils/config.py index 2f799ad..da8810d 100644 --- a/cogs/utils/config.py +++ b/cogs/utils/config.py @@ -82,7 +82,8 @@ db_opts = {'host': db_host, 'db': db_name, 'port': db_port, 'user': db_user, 'pa possible_keys = ['prefixes', 'battling', 'battle_records', 'boops', 'server_alerts', 'user_notifications', 'nsfw_channels', 'custom_permissions', 'rules', 'overwatch', 'picarto', 'twitch', 'strawpolls', 'tags', - 'tictactoe', 'bot_data'] + 'tictactoe', 'bot_data', 'command_manage'] + # This will be a dictionary that holds the cache object, based on the key that is saved cache = {} @@ -124,22 +125,28 @@ async def save_content(table: str, content): # Now that we've saved the new content, we should update our cache cached = cache.get(table) # While this should theoretically never happen, we just want to make sure - if cached is None or isinstance(cached, dict) or len(cached.values) == 0: + if cached is None: cache[table] = Cache(table) else: - await cached.update() + loop.create_task(cached.update()) async def get_content(key: str): cached = cache.get(key) # We want to check here if the key exists in cache, and it was not created more than an hour ago # We also want to make sure that if what we're getting in cache has content - # if not, lets make sure something didn't go awry, by getting from the database instead - if cached is None or isinstance(cached, dict) or len(cached.values) == 0 or ( - pendulum.utcnow() - cached.refreshed).hours >= 1: + # If not, lets make sure something didn't go awry, by getting from the database instead + + # If we found this object not cached, cache it + if cached is None: value = await _get_content(key) - # If we found this object not cached, cache it - cache[key] = value + cache[key] = Cache(key) + # Otherwise, check our timeout and make sure values is invalid + # If either of these are the case, we want to updated our values, and get our current data from the database + elif len(cached.values) == 0 or (pendulum.utcnow() - cached.refreshed).hours >= 1: + value = await _get_content(key) + loop.create_task(cached.update()) + # Otherwise, we have valid content in cache, use it else: value = cached.values return value @@ -154,8 +161,8 @@ async def _get_content(key: str): # We should only ever get one result, so use it if it exists, otherwise return none try: cursor = await r.table(key).run(conn) - await conn.close() items = list(cursor.items)[0] + await conn.close() except (IndexError, r.ReqlOpFailedError): await conn.close() return {} From f4291ab9237a5b157ac7109ab5870ddf0f7b77e5 Mon Sep 17 00:00:00 2001 From: phxntxm Date: Sun, 11 Sep 2016 03:30:23 -0500 Subject: [PATCH 2/4] Used the new cooldown reset method; simplified boops --- cogs/interaction.py | 44 +++++++++++--------------------------------- 1 file changed, 11 insertions(+), 33 deletions(-) diff --git a/cogs/interaction.py b/cogs/interaction.py index f805319..7dc5a2b 100644 --- a/cogs/interaction.py +++ b/cogs/interaction.py @@ -93,21 +93,15 @@ class Interaction: async def battle(self, ctx, player2: discord.Member): """Challenges the mentioned user to a battle""" if ctx.message.author.id == player2.id: - bucket = ctx.command._buckets.get_bucket(ctx) - bucket.is_rate_limited() - bucket._tokens = bucket.rate + ctx.command.reset_cooldown(ctx) await self.bot.say("Why would you want to battle yourself? Suicide is not the answer") return if self.bot.user.id == player2.id: - bucket = ctx.command._buckets.get_bucket(ctx) - bucket.is_rate_limited() - bucket._tokens = bucket.rate + ctx.command.reset_cooldown(ctx) await self.bot.say("I always win, don't even try it.") return if self.user_battling(ctx, player2): - bucket = ctx.command._buckets.get_bucket(ctx) - bucket.is_rate_limited() - bucket._tokens = bucket.rate + ctx.command.reset_cooldown(ctx) await self.bot.say("You or the person you are trying to battle is already in a battle!") return @@ -176,39 +170,23 @@ class Interaction: """Boops the mentioned person""" booper = ctx.message.author if boopee.id == booper.id: - bucket = ctx.command._buckets.get_bucket(ctx) - bucket.is_rate_limited() - bucket._tokens = bucket.rate + ctx.command.reset_cooldown(ctx) await self.bot.say("You can't boop yourself! Silly...") return if boopee.id == self.bot.user.id: - bucket = ctx.command._buckets.get_bucket(ctx) - bucket.is_rate_limited() - bucket._tokens = bucket.rate + ctx.command.reset_cooldown(ctx) await self.bot.say("Why the heck are you booping me? Get away from me >:c") return boops = await config.get_content('boops') - # This is only used to print the amount of times they've booped someone - # Set to 1 for the first time someone was booped - amount = 1 # Get all the booped stats for the author - booper_boops = boops.get(ctx.message.author.id) - # If the author does not exist in the dictionary, then he has never booped someone - # Create a new dictionary with the amount - if booper_boops is None: - boops[ctx.message.author.id] = {boopee.id: 1} - # If the booper has never booped the member provided, still add that user - # To the dictionary with the amount of 1 to start it off - elif booper_boops.get(boopee.id) is None: - booper_boops[boopee.id] = 1 - boops[ctx.message.author.id] = booper_boops - # Otherwise increment how many times they've booped that user - else: - amount = booper_boops.get(boopee.id) + 1 - booper_boops[boopee.id] = amount - boops[ctx.message.author.id] = booper_boops + # Set to default as having just booped boopee 0 times, so that we can increment that + booper_boops = boops.get(ctx.message.author.id, {boopee.id: 0}) + # If the booper has never booped the member provided, assume 0 like above so we can increment like normal + amount = booper_boops.get(boopee.id, 0) + 1 + booper_boops[boopee.id] = amount + boops[ctx.message.author.id] = booper_boops await config.save_content('boops', boops) fmt = "{0.mention} has just booped you {1.mention}! That's {2} times now!" From 374a0433aa22bcd4679cdf6c007d951f54aee01a Mon Sep 17 00:00:00 2001 From: phxntxm Date: Sun, 11 Sep 2016 03:32:36 -0500 Subject: [PATCH 3/4] Used the new cooldown reset method --- cogs/hangman.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/cogs/hangman.py b/cogs/hangman.py index 764a2d2..b6ce1c0 100644 --- a/cogs/hangman.py +++ b/cogs/hangman.py @@ -114,9 +114,7 @@ class Hangman: """Makes a guess towards the server's currently running hangman game""" game = self.games.get(ctx.message.server.id) if not game: - bucket = ctx.command._buckets.get_bucket(ctx) - bucket.is_rate_limited() - bucket._token = bucket.rate + ctx.command.reset_cooldown(ctx) await self.bot.say("There are currently no hangman games running!") return @@ -126,9 +124,7 @@ class Hangman: # And also add a message for a loss/win if len(guess) == 1: if guess in game.guessed_letters: - bucket = ctx.command._buckets.get_bucket(ctx) - bucket.is_rate_limited() - bucket._tokens = bucket.rate + ctx.command.reset_cooldown(ctx) await self.bot.say("That letter has already been guessed!") # Return here as we don't want to count this as a failure return From 52ad367e0b56258b1ca9d482a6f9bb3594618068 Mon Sep 17 00:00:00 2001 From: phxntxm Date: Sun, 11 Sep 2016 13:17:04 -0500 Subject: [PATCH 4/4] Made sure that the id of the strawpoll is a string, not an int, when saving --- cogs/strawpoll.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/strawpoll.py b/cogs/strawpoll.py index 4cd7f46..0f0fd44 100644 --- a/cogs/strawpoll.py +++ b/cogs/strawpoll.py @@ -109,7 +109,7 @@ class Strawpoll: # Save this strawpoll in the list of running strawpolls for a server all_polls = await config.get_content('strawpolls') server_polls = all_polls.get(ctx.message.server.id) or {} - server_polls[data['id']] = {'author': ctx.message.author.id, 'date': str(pendulum.utcnow()), 'title': title} + server_polls[str(data['id'])] = {'author': ctx.message.author.id, 'date': str(pendulum.utcnow()), 'title': title} all_polls[ctx.message.server.id] = server_polls await config.save_content('strawpolls', all_polls)