Skip to content

Commit 3d3401a

Browse files
authored
Dadjoke (#1693)
* Add dadjoke command * Add User Agent header to comply with API policy * feat: use bot builtin http_session and raise error for exceptions�
1 parent 5629434 commit 3d3401a

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

bot/exts/fun/fun.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,26 @@ async def caesarcipher_decrypt(self, ctx: Context, offset: int, *, msg: str) ->
155155
await self._caesar_cipher(ctx, offset, msg, left_shift=True)
156156

157157
@commands.command()
158-
async def joke(self, ctx: commands.Context, category: Literal["neutral", "chuck", "all"] = "all") -> None:
159-
"""Retrieves a joke of the specified `category` from the pyjokes api."""
160-
joke = pyjokes.get_joke(category=category)
161-
await ctx.send(joke)
158+
async def joke(self, ctx: commands.Context, category: Literal["dad", "neutral", "chuck", "all"] = "all") -> None:
159+
"""
160+
Retrieves a joke of the specified `category` from the pyjokes api.
161+
162+
- dad uses icanhazdadjoke.
163+
- others use pyjokes.
164+
"""
165+
if category == "dad":
166+
async with self.bot.http_session.get(
167+
"https://icanhazdadjoke.com",
168+
headers={
169+
"Accept":"application/json",
170+
"User-Agent": "Sir-lancebot (https://github.com/python-discord/sir-lancebot)"
171+
}) as res:
172+
res.raise_for_status()
173+
data = await res.json()
174+
await ctx.send(data["joke"])
175+
else:
176+
joke = pyjokes.get_joke(category=category)
177+
await ctx.send(joke)
162178

163179
@commands.group(name="quote")
164180
async def quote(self, ctx: Context) -> None:

0 commit comments

Comments
 (0)