From 92d99fbf35155b9530c55feebb81ed9e577058d0 Mon Sep 17 00:00:00 2001 From: Marko Kovačević Date: Tue, 9 Oct 2018 11:53:06 +0200 Subject: Add random spooky gif feature. --- bot/cogs/gif.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 bot/cogs/gif.py (limited to 'bot/cogs/gif.py') diff --git a/bot/cogs/gif.py b/bot/cogs/gif.py new file mode 100644 index 00000000..559e0eaf --- /dev/null +++ b/bot/cogs/gif.py @@ -0,0 +1,29 @@ +from os import environ + +import aiohttp +from discord.ext import commands + + +class Gif: + + """ + A cog to fetch a random spooky gif from the web! + """ + + def __init__(self, bot): + self.bot = bot + self.GIPHY_TOKEN = environ.get('GIPHY_TOKEN') + + @commands.command() + async def gif(self, ctx): + async with aiohttp.ClientSession() as session: + params = {'api_key': self.GIPHY_TOKEN, 'tag': 'halloween', 'rating': 'g'} + # Make a GET request to the Giphy API to get a random halloween gif. + async with session.get('http://api.giphy.com/v1/gifs/random', params=params) as resp: + data = await resp.json() + url = data['data']['url'] + await ctx.send(url) + + +def setup(bot): + bot.add_cog(Gif(bot)) -- cgit v1.2.3 From 5cf56e648416b22d50ef3ab0f230618ed5ab2925 Mon Sep 17 00:00:00 2001 From: Marko Kovačević Date: Wed, 10 Oct 2018 12:02:14 +0200 Subject: Refactoring. --- bot/cogs/gif.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'bot/cogs/gif.py') diff --git a/bot/cogs/gif.py b/bot/cogs/gif.py index 559e0eaf..cacb77ce 100644 --- a/bot/cogs/gif.py +++ b/bot/cogs/gif.py @@ -4,8 +4,7 @@ import aiohttp from discord.ext import commands -class Gif: - +class SpookyGif: """ A cog to fetch a random spooky gif from the web! """ @@ -16,6 +15,10 @@ class Gif: @commands.command() async def gif(self, ctx): + """ + Fetches a random gif from the GIPHY API and responds with it. + """ + async with aiohttp.ClientSession() as session: params = {'api_key': self.GIPHY_TOKEN, 'tag': 'halloween', 'rating': 'g'} # Make a GET request to the Giphy API to get a random halloween gif. @@ -26,4 +29,4 @@ class Gif: def setup(bot): - bot.add_cog(Gif(bot)) + bot.add_cog(SpookyGif(bot)) -- cgit v1.2.3