From 55f6307b7f2c2c82ec917abedd1cf1da5339d065 Mon Sep 17 00:00:00 2001 From: Twentysix Date: Fri, 7 Apr 2017 23:29:02 +0200 Subject: [PATCH] [Image] [p]imgur search: Return up to 3 results --- cogs/image.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/cogs/image.py b/cogs/image.py index 4d754d2e5..a4c222569 100644 --- a/cogs/image.py +++ b/cogs/image.py @@ -1,5 +1,5 @@ from discord.ext import commands -from random import choice +from random import choice, shuffle import aiohttp import functools import asyncio @@ -54,19 +54,24 @@ class Image: @_imgur.command(pass_context=True, name="search") async def imgur_search(self, ctx, *, term: str): - """Searches Imgur for the specified term""" + """Searches Imgur for the specified term and returns up to 3 results""" task = functools.partial(self.imgur.gallery_search, term, advanced=None, sort='time', window='all', page=0) task = self.bot.loop.run_in_executor(None, task) try: - result = await asyncio.wait_for(task, timeout=10) + results = await asyncio.wait_for(task, timeout=10) except asyncio.TimeoutError: await self.bot.say("Error: request timed out") else: - if result: - await self.bot.say(result[0].link) + if results: + shuffle(results) + msg = "Search results...\n" + for r in results[:3]: + msg += r.gifv if hasattr(r, "gifv") else r.link + msg += "\n" + await self.bot.say(msg) else: await self.bot.say("Your search terms gave no results.")