From c3febe07463407b9cdf6828fa940e32c9be1d36a Mon Sep 17 00:00:00 2001 From: Scumm Boy Date: Wed, 10 Feb 2016 13:01:56 -0500 Subject: [PATCH] Randomize !gif results Let the bot pick a random result out of the returned giphy array. Make sure to not choose a number higher than the length of the array. --- cogs/image.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cogs/image.py b/cogs/image.py index 6ddbb695d..ce91e041c 100644 --- a/cogs/image.py +++ b/cogs/image.py @@ -3,6 +3,7 @@ from discord.ext import commands from random import randint from imgurpython import ImgurClient import aiohttp +import random class Image: """Image related commands.""" @@ -45,6 +46,7 @@ class Image: @commands.command(no_pm=True) async def gif(self, *text): + random.seed() """ gif [keyword] - retrieves first search result from giphy """ if len(text) > 0: if len(text[0]) > 1 and len(text[0]) < 20: @@ -54,7 +56,8 @@ class Image: async with aiohttp.get(search) as r: result = await r.json() if result["data"] != []: - url = result["data"][0]["url"] + maxarray = len(result) + url = result["data"][random.randint(0,maxarray)]["url"] await self.bot.say(url) else: await self.bot.say("Your search terms gave no results.")