From 84cff2b35d9c16f7f670db7680cc844c9a61b6af Mon Sep 17 00:00:00 2001 From: phxntxm Date: Thu, 29 Sep 2016 01:58:51 -0500 Subject: [PATCH 1/2] Disabling battling until it's been fixed --- cogs/interaction.py | 2 +- cogs/utils/config.py | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/cogs/interaction.py b/cogs/interaction.py index c97fa40..f9e9900 100644 --- a/cogs/interaction.py +++ b/cogs/interaction.py @@ -38,7 +38,7 @@ class Interaction: self.battles[ctx.message.server.id] = {p1: p2 for p1, p2 in battles.items() if not p2 == player_id and not p1 == player_id} - @commands.group(pass_context=True, no_pm=True, invoke_without_command=True) + @commands.group(pass_context=True, no_pm=True, invoke_without_command=True, enabled=False) @commands.cooldown(1, 180, BucketType.user) @checks.custom_perms(send_messages=True) async def battle(self, ctx, player2: discord.Member): diff --git a/cogs/utils/config.py b/cogs/utils/config.py index 4e8c2c0..44ddbd2 100644 --- a/cogs/utils/config.py +++ b/cogs/utils/config.py @@ -108,11 +108,14 @@ async def update_records(key, winner, loser): winner_stats = {} loser_stats = {} - for stat in matches: - if stat.get('member_id') == winner.id: - winner_stats = stat - elif stat.get('member_id') == loser.id: - loser_stats = stat + try: + for stat in matches: + if stat.get('member_id') == winner.id: + winner_stats = stat + elif stat.get('member_id') == loser.id: + loser_stats = stat + except TypeError: + pass winner_rating = winner_stats.get('rating') or 1000 loser_rating = loser_stats.get('rating') or 1000 @@ -138,10 +141,10 @@ async def update_records(key, winner, loser): loser_rating -= 16 + rating_change # Just increase wins/losses for each person, making sure it's at least 0 - winner_wins = winner_stats.get('wins') or 0 - winner_losses = winner_stats.get('losses') or 0 - loser_wins = loser_stats.get('wins') or 0 - loser_losses = loser_stats.get('losses') or 0 + winner_wins = winner_stats.get('wins', 0) + winner_losses = winner_stats.get('losses', 0) + loser_wins = loser_stats.get('wins', 0) + loser_losses = loser_stats.get('losses', 0) winner_wins += 1 loser_losses += 1 From 0c448d775d25e418872908b34a2e48c4801e9d03 Mon Sep 17 00:00:00 2001 From: phxntxm Date: Thu, 29 Sep 2016 02:40:29 -0500 Subject: [PATCH 2/2] Added the ability to get an image from any page from derpi; linked to the original image as well --- cogs/links.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/cogs/links.py b/cogs/links.py index f89d66d..61c1f83 100644 --- a/cogs/links.py +++ b/cogs/links.py @@ -6,6 +6,7 @@ import discord import aiohttp import random import re +import math class Links: @@ -90,15 +91,17 @@ class Links: await self.bot.say("No results with that search term, {0}!".format(ctx.message.author.mention)) return - # Find a random image based on the first page of results. - # Currently derpibooru provides no way to change how many results can be shown on one page - # Nor anyway to see how many pages are returned by a certain query - # Due to the fact that a query may only return one page - # We cannot try to check more than one as it might fail - # So this is the best that we can do at the moment + # The first request we've made ensures there are results + # Now we can get the total count from that, and make another request based on the number of pages as well if len(results) > 0: + pages = math.ceil(data['total'] / len(results)) + url += '&page={}'.format(random.SystemRandom().randint(1, pages)) + async with self.session.get(url, headers=self.headers) as r: + data = await r.json() + results = data['search'] + index = random.SystemRandom().randint(0, len(results) - 1) - image_link = 'http://{}'.format(results[index].get('representations').get('full')[2:].strip()) + image_link = 'https://derpibooru.org/{}'.format(results[index]) else: await self.bot.say("No results with that search term, {0}!".format(ctx.message.author.mention)) return