aboutsummaryrefslogtreecommitdiffstats
path: root/bot/cogs/hacktober/spookygif.py
blob: 98a411f69191a02c315c270741cccd07edab9d62 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import aiohttp
import discord
from discord.ext import commands

from bot.constants import GIPHY_TOKEN


class SpookyGif:
    """
    A cog to fetch a random spooky gif from the web!
    """

    def __init__(self, bot):
        self.bot = bot

    @commands.command(name="spookygif", aliases=["sgif", "scarygif"])
    async def spookygif(self, ctx):
        """
        Fetches a random gif from the GIPHY API and responds with it.
        """
        async with ctx.typing():
            async with aiohttp.ClientSession() as session:
                params = {'api_key': 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']['image_url']

                embed = discord.Embed(colour=0x9b59b6)
                embed.title = "A spooooky gif!"
                embed.set_image(url=url)

        await ctx.send(embed=embed)


def setup(bot):
    bot.add_cog(SpookyGif(bot))