-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #235 from python-discord/quotes
Add a quotes command
- Loading branch information
Showing
3 changed files
with
104 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import random | ||
from typing import NamedTuple | ||
|
||
import aiohttp | ||
from bs4 import BeautifulSoup | ||
from discord.ext import commands | ||
|
||
from arthur import KingArthur | ||
from arthur.log import logger | ||
|
||
|
||
class Quote(NamedTuple): | ||
"""A data structure to hold quotes and their source.""" | ||
|
||
text: str | ||
source: str | None | ||
|
||
def __str__(self): | ||
return f"{self.text}\n-- {self.source}" | ||
|
||
|
||
class QuotesCog(commands.Cog): | ||
"""A cog that sends programming quotes.""" | ||
|
||
def __init__(self, bot: KingArthur) -> None: | ||
self.bot = bot | ||
self.quotes = [] | ||
|
||
async def cog_load(self) -> None: | ||
"""Fetch the quotes to be used.""" | ||
try: | ||
async with self.bot.http_session.get( | ||
"http://quotes.cat-v.org/programming/" | ||
) as response: | ||
content = await response.content.read() | ||
except aiohttp.ClientResponseError: | ||
logger.exception("Couldn't fetch programming quotes.") | ||
return | ||
|
||
soup = BeautifulSoup(content, "html.parser") | ||
prev_text = None | ||
|
||
for item in soup.article.find_all("p"): | ||
text = item.get_text().replace("\xa0", "") | ||
|
||
if prev_text is not None: | ||
if text.startswith("—"): | ||
self.quotes.append(Quote(text=prev_text, source=text.removeprefix("— "))) | ||
elif not prev_text.startswith("—"): | ||
self.quotes.append(Quote(text=prev_text, source=None)) | ||
|
||
prev_text = text | ||
|
||
@commands.command(name="quote") | ||
async def quote(self, ctx: commands.Context) -> None: | ||
"""Send a random programming quote.""" | ||
if not self.quotes: | ||
await ctx.reply(":x: Couldn't fetch quotes, try reloading the cog.") | ||
return | ||
|
||
quote = random.choice(self.quotes) | ||
await ctx.reply(str(quote)) | ||
|
||
|
||
async def setup(bot: KingArthur) -> None: | ||
"""Load the QuotesCog.""" | ||
await bot.add_cog(QuotesCog(bot)) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters