aboutsummaryrefslogtreecommitdiffstats
path: root/bot/exts/holidays/halloween/spookygif.py
blob: 45e6936ee1defb1cae67b3d0d493360885d7fea5 (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
38
39
40
41
import discord
from discord.ext import commands
from pydis_core.utils.logging import get_logger

from bot.bot import Bot
from bot.constants import Colours, Tokens

log = get_logger(__name__)

API_URL = "http://api.giphy.com/v1/gifs/random"


class SpookyGif(commands.Cog):
    """A cog to fetch a random spooky gif from the web!"""

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

    @commands.command(name="spookygif", aliases=("sgif", "scarygif"))
    async def spookygif(self, ctx: commands.Context) -> None:
        """Fetches a random gif from the GIPHY API and responds with it."""
        async with ctx.typing():
            params = {"api_key": Tokens.giphy.get_secret_value(), "tag": "halloween", "rating": "g"}
            # Make a GET request to the Giphy API to get a random halloween gif.
            async with self.bot.http_session.get(API_URL, params=params) as resp:
                data = await resp.json()
            url = data["data"]["images"]["downsized"]["url"]

            embed = discord.Embed(title="A spooooky gif!", colour=Colours.purple)
            embed.set_image(url=url)

        await ctx.send(embed=embed)


async def setup(bot: Bot) -> None:
    """Spooky GIF Cog load."""
    if not Tokens.giphy:
        log.warning("No Giphy token. Not loading SpookyGif cog.")
        return
    await bot.add_cog(SpookyGif(bot))