Reverted changes to !gif, created !gifr

Instead of modifying !gif, I have moved the changes into !gifr for accessing random images within giphy's search results.
This commit is contained in:
Scumm Boy 2016-02-10 14:04:22 -05:00
parent c3febe0746
commit 15601fb9a6

View File

@ -46,7 +46,6 @@ class Image:
@commands.command(no_pm=True) @commands.command(no_pm=True)
async def gif(self, *text): async def gif(self, *text):
random.seed()
""" gif [keyword] - retrieves first search result from giphy """ """ gif [keyword] - retrieves first search result from giphy """
if len(text) > 0: if len(text) > 0:
if len(text[0]) > 1 and len(text[0]) < 20: if len(text[0]) > 1 and len(text[0]) < 20:
@ -56,8 +55,7 @@ class Image:
async with aiohttp.get(search) as r: async with aiohttp.get(search) as r:
result = await r.json() result = await r.json()
if result["data"] != []: if result["data"] != []:
maxarray = len(result) url = result["data"][0]["url"]
url = result["data"][random.randint(0,maxarray)]["url"]
await self.bot.say(url) await self.bot.say(url)
else: else:
await self.bot.say("Your search terms gave no results.") await self.bot.say("Your search terms gave no results.")
@ -68,5 +66,29 @@ class Image:
else: else:
await self.bot.say("gif [text]") await self.bot.say("gif [text]")
@commands.command(no_pm=True)
async def gifr(self, *text):
""" gifr [keyword] - retrieves a random gif from a giphy search """
random.seed()
if len(text) > 0:
if len(text[0]) > 1 and len(text[0]) < 20:
try:
msg = "+".join(text)
search = "http://api.giphy.com/v1/gifs/search?q=" + msg + "&api_key=dc6zaTOxFJmzC"
async with aiohttp.get(search) as r:
result = await r.json()
if result["data"] != []:
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.")
except:
await self.bot.say("Error.")
else:
await self.bot.say("Invalid search.")
else:
await self.bot.say("gif [text]")
def setup(bot): def setup(bot):
bot.add_cog(Image(bot)) bot.add_cog(Image(bot))