-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathgiphy.py
More file actions
83 lines (63 loc) · 2.38 KB
/
Copy pathgiphy.py
File metadata and controls
83 lines (63 loc) · 2.38 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Module for giphy extension in the bot."""
from __future__ import annotations
from typing import TYPE_CHECKING, Self
from discord.ext import commands
import ui
from core import auxiliary, cogs
if TYPE_CHECKING:
import bot
async def setup(bot: bot.TechSupportBot) -> None:
"""Loading the Giphy plugin into the bot
Args:
bot (bot.TechSupportBot): The bot object to register the cogs to
Raises:
AttributeError: Raised if an API key is missing to prevent unusable commands from loading
"""
# Don't load without the API key
try:
if not bot.file_config.api.api_keys.giphy:
raise AttributeError("Giphy was not loaded due to missing API key")
except AttributeError as exc:
raise AttributeError("Giphy was not loaded due to missing API key") from exc
await bot.add_cog(Giphy(bot=bot))
class Giphy(cogs.BaseCog):
"""Class for the giphy extension.
Attributes:
GIPHY_URL (str): The URL for the giphy API
SEARCH_LIMIT (int): The max amount of gifs to search for
"""
GIPHY_URL: str = "https://api.giphy.com/v1/gifs/search?q={}&api_key={}&limit={}"
SEARCH_LIMIT: int = 10
@auxiliary.with_typing
@commands.guild_only()
@commands.command(
name="giphy",
brief="Grabs a random Giphy image",
description="Grabs a random Giphy image based on your search",
usage="[query]",
)
async def giphy(self: Self, ctx: commands.Context, *, query: str) -> None:
"""The main entry point and logic for the giphy command
Args:
ctx (commands.Context): The context in which the command was run
query (str): The string to query the giphy API for
"""
response = await self.bot.http_functions.http_call(
"get",
self.GIPHY_URL.format(
query.replace(" ", "+"),
self.bot.file_config.api.api_keys.giphy,
self.SEARCH_LIMIT,
),
)
data = response.get("data")
if not data:
await auxiliary.send_deny_embed(
message=f"No search results found for: *{query}*", channel=ctx.channel
)
return
embeds = []
for item in data:
url = item.get("embed_url", {})
embeds.append(url)
await ui.PaginateView().send(ctx.channel, ctx.author, embeds)