From 0e2f256f8c6f4ac0ddfca9809e30bf506b98b73b Mon Sep 17 00:00:00 2001 From: sgoudham Date: Wed, 26 Aug 2020 16:02:15 +0100 Subject: [PATCH] Trying to retrieve killing gifs from api --- cogs/interactive.py | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/cogs/interactive.py b/cogs/interactive.py index 892a4f24..b80b6528 100644 --- a/cogs/interactive.py +++ b/cogs/interactive.py @@ -167,29 +167,34 @@ class Interactive(Cog): else: title = f":scream: :scream: | **{ctx.author.display_name}** killed **{member.display_name}**" - try: - - # Open the file containing the killing gifs - with open('images/FunCommands/killing.txt') as file: - # Store content of the file in killing_array - killing_array = file.readlines() + # Set details for search + apikey = config("TENOR_AUTH") # test value + search_term = "anime-kill" + url = f"https://api.tenor.com/v1/random?q={search_term}&key={apikey}&limit=1&media_filter=minimal" - # Get the member and the userAvatar - member, userAvatar = getMember(ctx) + # get random results using default locale of EN_US + # Searching API for the current airing shows + async with aiohttp.ClientSession() as session: + async with session.get(url) as resp: + if resp.status == 200: + gifs = await resp.json() + url = gifs["results"][0]["media"][0]["gif"]["url"] + else: + self.bot.generate_embed(ctx, desc="**Something Went Wrong With Tenor!**") - # Set up the embed to display a random killing gif - embed = Embed( - title=title, - colour=self.bot.random_colour(), - timestamp=datetime.datetime.utcnow()) - embed.set_image(url=random.choice(killing_array)) - embed.set_footer(text=f"Requested by {member}", icon_url=userAvatar) + # Get the member and the userAvatar + member, userAvatar = getMember(ctx) - # Send the embedded message to the user - await ctx.send(embed=embed) + # Set up the embed to display a random killing gif + embed = Embed( + title=title, + colour=self.bot.random_colour(), + timestamp=datetime.datetime.utcnow()) + embed.set_image(url=url) + embed.set_footer(text=f"Requested by {member}", icon_url=userAvatar) - except FileNotFoundError as e: - print(e) + # Send the embedded message to the user + await ctx.send(embed=embed) @command(name="slap") @bot_has_permissions(embed_links=True)