diff options
author | 2021-10-01 04:32:58 -0500 | |
---|---|---|
committer | 2021-10-01 02:32:58 -0700 | |
commit | beb42c28911d8bbc4de5b0926a77619b4454f402 (patch) | |
tree | d7dd87607356c0c4c4084a42413b51c806b85d2e /bot/exts/fun/quack.py | |
parent | Allow everyone to use the `.bm` command everywhere (#885) (diff) |
`.quack` (#849)
* feat: Added quack command
* added log.error call for request fails
* spacing change
Co-authored-by: Bluenix <[email protected]>
* another spacing change
Co-authored-by: Bluenix <[email protected]>
* Moved description to footer
Co-authored-by: Bluenix <[email protected]>
* whitespace fix
* chore: Removed the link from the footer and set it as the url param
* chore: moved footer to description
Co-authored-by: Bluenix <[email protected]>
Co-authored-by: Xithrius <[email protected]>
Diffstat (limited to 'bot/exts/fun/quack.py')
-rw-r--r-- | bot/exts/fun/quack.py | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/bot/exts/fun/quack.py b/bot/exts/fun/quack.py new file mode 100644 index 00000000..0c228aed --- /dev/null +++ b/bot/exts/fun/quack.py @@ -0,0 +1,75 @@ +import logging +import random +from typing import Literal, Optional + +import discord +from discord.ext import commands + +from bot.bot import Bot +from bot.constants import Colours, NEGATIVE_REPLIES + +API_URL = 'https://quackstack.pythondiscord.com' + +log = logging.getLogger(__name__) + + +class Quackstack(commands.Cog): + """Cog used for wrapping Quackstack.""" + + def __init__(self, bot: Bot): + self.bot = bot + + @commands.command() + async def quack( + self, + ctx: commands.Context, + ducktype: Literal["duck", "manduck"] = "duck", + *, + seed: Optional[str] = None + ) -> None: + """ + Use the Quackstack API to generate a random duck. + + If a seed is provided, a duck is generated based on the given seed. + Either "duck" or "manduck" can be provided to change the duck type generated. + """ + ducktype = ducktype.lower() + quackstack_url = f"{API_URL}/{ducktype}" + params = {} + if seed is not None: + try: + seed = int(seed) + except ValueError: + # We just need to turn the string into an integer any way possible + seed = int.from_bytes(seed.encode(), "big") + params["seed"] = seed + + async with self.bot.http_session.get(quackstack_url, params=params) as response: + error_embed = discord.Embed( + title=random.choice(NEGATIVE_REPLIES), + description="The request failed. Please try again later.", + color=Colours.soft_red, + ) + if response.status != 200: + log.error(f"Response to Quackstack returned code {response.status}") + await ctx.send(embed=error_embed) + return + + data = await response.json() + file = data["file"] + + embed = discord.Embed( + title=f"Quack! Here's a {ducktype} for you.", + description=f"A {ducktype} from Quackstack.", + color=Colours.grass_green, + url=f"{API_URL}/docs" + ) + + embed.set_image(url=API_URL + file) + + await ctx.send(embed=embed) + + +def setup(bot: Bot) -> None: + """Loads the Quack cog.""" + bot.add_cog(Quackstack(bot)) |