From be2169e87a6e355a0b07d126b1c0813ffe604514 Mon Sep 17 00:00:00 2001 From: Trieu Do Date: Sun, 3 Aug 2025 09:24:12 -0500 Subject: [PATCH 1/3] Add dadjoke command --- bot/exts/fun/fun.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/bot/exts/fun/fun.py b/bot/exts/fun/fun.py index 66c4851729..08d816ef1e 100644 --- a/bot/exts/fun/fun.py +++ b/bot/exts/fun/fun.py @@ -4,6 +4,7 @@ from pathlib import Path from typing import Literal +import aiohttp import pyjokes from discord import Embed from discord.ext import commands @@ -153,10 +154,26 @@ async def caesarcipher_decrypt(self, ctx: Context, offset: int, *, msg: str) -> await self._caesar_cipher(ctx, offset, msg, left_shift=True) @commands.command() - async def joke(self, ctx: commands.Context, category: Literal["neutral", "chuck", "all"] = "all") -> None: - """Retrieves a joke of the specified `category` from the pyjokes api.""" - joke = pyjokes.get_joke(category=category) - await ctx.send(joke) + async def joke(self, ctx: commands.Context, category: Literal["dad", "neutral", "chuck", "all"] = "all") -> None: + """ + Retrieves a joke of the specified `category` from the pyjokes api. + + - dad uses icanhazdadjoke. + - others use pyjokes. + """ + if category == "dad": + async with aiohttp.ClientSession() as session, session.get( + "https://icanhazdadjoke.com", + headers={"Accept":"application/json" + }) as res: + if res.status == 200: + data = await res.json() + await ctx.send(data["joke"]) + else: + await ctx.send("There is no dad joke now") + else: + joke = pyjokes.get_joke(category=category) + await ctx.send(joke) async def setup(bot: Bot) -> None: From a7c595db2858352f4382144abbbb2257ddc786e4 Mon Sep 17 00:00:00 2001 From: Trieu Do Date: Sun, 3 Aug 2025 10:17:45 -0500 Subject: [PATCH 2/3] Add User Agent header to comply with API policy --- bot/exts/fun/fun.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bot/exts/fun/fun.py b/bot/exts/fun/fun.py index 08d816ef1e..701b9760fb 100644 --- a/bot/exts/fun/fun.py +++ b/bot/exts/fun/fun.py @@ -164,8 +164,10 @@ async def joke(self, ctx: commands.Context, category: Literal["dad", "neutral", if category == "dad": async with aiohttp.ClientSession() as session, session.get( "https://icanhazdadjoke.com", - headers={"Accept":"application/json" - }) as res: + headers={ + "Accept":"application/json", + "User-Agent": "Sir-lancebot" + }) as res: if res.status == 200: data = await res.json() await ctx.send(data["joke"]) From 4fa588b2019d6ae277c0f550a53ddb54065abc88 Mon Sep 17 00:00:00 2001 From: T Do Date: Thu, 18 Sep 2025 15:54:16 -0500 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20use=20bot=20builtin=20http=5Fsessio?= =?UTF-8?q?n=20and=20raise=20error=20for=20exceptions=C2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot/exts/fun/fun.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/bot/exts/fun/fun.py b/bot/exts/fun/fun.py index 701b9760fb..e94a759ed1 100644 --- a/bot/exts/fun/fun.py +++ b/bot/exts/fun/fun.py @@ -4,7 +4,6 @@ from pathlib import Path from typing import Literal -import aiohttp import pyjokes from discord import Embed from discord.ext import commands @@ -162,17 +161,15 @@ async def joke(self, ctx: commands.Context, category: Literal["dad", "neutral", - others use pyjokes. """ if category == "dad": - async with aiohttp.ClientSession() as session, session.get( + async with self.bot.http_session.get( "https://icanhazdadjoke.com", headers={ "Accept":"application/json", - "User-Agent": "Sir-lancebot" + "User-Agent": "Sir-lancebot (https://github.com/python-discord/sir-lancebot)" }) as res: - if res.status == 200: - data = await res.json() - await ctx.send(data["joke"]) - else: - await ctx.send("There is no dad joke now") + res.raise_for_status() + data = await res.json() + await ctx.send(data["joke"]) else: joke = pyjokes.get_joke(category=category) await ctx.send(joke)