|
5 | 5 | from typing import Literal |
6 | 6 |
|
7 | 7 | import pyjokes |
| 8 | +from aiohttp import ClientError, ClientResponseError |
8 | 9 | from discord import Embed |
9 | 10 | from discord.ext import commands |
10 | 11 | from discord.ext.commands import BadArgument, Cog, Context |
|
14 | 15 | from bot.bot import Bot |
15 | 16 | from bot.constants import Client, Colours, Emojis |
16 | 17 | from bot.utils import helpers, messages |
| 18 | +from bot.utils.quote import daily_quote, random_quote |
17 | 19 |
|
18 | 20 | log = get_logger(__name__) |
19 | 21 |
|
@@ -158,6 +160,57 @@ async def joke(self, ctx: commands.Context, category: Literal["neutral", "chuck" |
158 | 160 | joke = pyjokes.get_joke(category=category) |
159 | 161 | await ctx.send(joke) |
160 | 162 |
|
| 163 | + @commands.group(name="quote") |
| 164 | + async def quote(self, ctx: Context) -> None: |
| 165 | + """ |
| 166 | + Retrieve a quote from zenquotes.io api. |
| 167 | +
|
| 168 | + See `random`, `daily` subcommands. |
| 169 | + """ |
| 170 | + if ctx.invoked_subcommand is None: |
| 171 | + await ctx.invoke(self.bot.get_command("help"), "quote") |
| 172 | + |
| 173 | + @quote.command(name="daily") |
| 174 | + async def quote_daily(self, ctx: Context) -> None: |
| 175 | + """Retrieve the daily quote from zenquotes.io api.""" |
| 176 | + try: |
| 177 | + quote = await daily_quote(self.bot) |
| 178 | + embed = Embed( |
| 179 | + title="Daily Quote", |
| 180 | + description=f"> {quote}\n\n-# Powered by [zenquotes.io](https://zenquotes.io)", |
| 181 | + colour=Colours.blue |
| 182 | + ) |
| 183 | + await ctx.send(embed=embed) |
| 184 | + except ClientResponseError as e: |
| 185 | + log.warning(f"ZenQuotes API error: {e.status} {e.message}") |
| 186 | + await ctx.send(":x: Could not retrieve quote from API.") |
| 187 | + except (ClientError, TimeoutError) as e: |
| 188 | + log.error(f"Network error fetching quote: {e}") |
| 189 | + await ctx.send(":x: Could not connect to the quote service.") |
| 190 | + except Exception: |
| 191 | + log.exception("Unexpected error fetching quote.") |
| 192 | + await ctx.send(":x: Something unexpected happened. Try again later.") |
| 193 | + |
| 194 | + @quote.command(name="random") |
| 195 | + async def quote_random(self, ctx: Context) -> None: |
| 196 | + """Retrieve a random quote from zenquotes.io api.""" |
| 197 | + try: |
| 198 | + quote = await random_quote(self.bot) |
| 199 | + embed = Embed( |
| 200 | + title="Random Quote", |
| 201 | + description=f"> {quote}\n\n-# Powered by [zenquotes.io](https://zenquotes.io)", |
| 202 | + colour=Colours.blue |
| 203 | + ) |
| 204 | + await ctx.send(embed=embed) |
| 205 | + except ClientResponseError as e: |
| 206 | + log.warning(f"ZenQuotes API error: {e.status} {e.message}") |
| 207 | + await ctx.send(":x: Could not retrieve quote from API.") |
| 208 | + except (ClientError, TimeoutError) as e: |
| 209 | + log.error(f"Network error fetching quote: {e}") |
| 210 | + await ctx.send(":x: Could not connect to the quote service.") |
| 211 | + except Exception: |
| 212 | + log.exception("Unexpected error fetching quote.") |
| 213 | + await ctx.send(":x: Something unexpected happened. Try again later.") |
161 | 214 |
|
162 | 215 | async def setup(bot: Bot) -> None: |
163 | 216 | """Load the Fun cog.""" |
|
0 commit comments