Skip to content

Commit

Permalink
feat(Randimals): add kangaroo, raccoon, and redpanda
Browse files Browse the repository at this point in the history
  • Loading branch information
tmercswims committed Jun 17, 2024
1 parent 8948fc8 commit 304c365
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions randimals/randimals.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,60 @@ async def fetcher() -> str:
log.warning("API call failed; unable to get bird picture")
await ctx.send("I was unable to get a bird picture.")

@commands.hybrid_command()
async def kangaroo(self, ctx: commands.Context) -> None:
"""Get a random kangaroo."""

await ctx.typing()

async def fetcher() -> str:
url = "https://some-random-api.com/animal/kangaroo"
async with self.__session.get(url) as response:
return (await response.json())["image"]

try:
file = await self.__get_image_carefully(fetcher)
await ctx.send(file=file)
except (aiohttp.ClientError, RetryLimitExceeded):
log.warning("API call failed; unable to get kangaroo picture")
await ctx.send("I was unable to get a kangaroo picture.")

@commands.hybrid_command()
async def raccoon(self, ctx: commands.Context) -> None:
"""Get a random raccoon."""

await ctx.typing()

async def fetcher() -> str:
url = "https://some-random-api.com/animal/raccoon"
async with self.__session.get(url) as response:
return (await response.json())["image"]

try:
file = await self.__get_image_carefully(fetcher)
await ctx.send(file=file)
except (aiohttp.ClientError, RetryLimitExceeded):
log.warning("API call failed; unable to get raccoon picture")
await ctx.send("I was unable to get a raccoon picture.")

@commands.hybrid_command()
async def redpanda(self, ctx: commands.Context) -> None:
"""Get a random red panda."""

await ctx.typing()

async def fetcher() -> str:
url = "https://some-random-api.com/animal/red_panda"
async with self.__session.get(url) as response:
return (await response.json())["image"]

try:
file = await self.__get_image_carefully(fetcher)
await ctx.send(file=file)
except (aiohttp.ClientError, RetryLimitExceeded):
log.warning("API call failed; unable to get red panda picture")
await ctx.send("I was unable to get a red panda picture.")

async def __get_image_carefully(self, fetcher: Callable[[], Awaitable[str]]) -> discord.File:
# We need to send a user agent pretending to be a normal browser so that Imgur works. Without this, it assumes
# that we _aren't_ a normal browser (it's correct) and automatically returns a 429 (too many requests), which is
Expand Down

0 comments on commit 304c365

Please sign in to comment.