From 92e290ab714c76fe08fab635c49d064cf4ecf0af Mon Sep 17 00:00:00 2001 From: Johannes Christ Date: Wed, 5 Jun 2024 18:30:31 +0200 Subject: [PATCH 1/2] Simplify fetching system resources --- arthur/exts/systems/system_information.py | 29 ++++++++--------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/arthur/exts/systems/system_information.py b/arthur/exts/systems/system_information.py index 3ea28e7..53caac9 100644 --- a/arthur/exts/systems/system_information.py +++ b/arthur/exts/systems/system_information.py @@ -17,8 +17,6 @@ from arthur.config import CONFIG BASE_RESOURCE = "https://git.9front.org/plan9front/plan9front/HEAD/{}/raw" -BLOGCOM = BASE_RESOURCE.format("lib/blogcom") -BULLSHIT = BASE_RESOURCE.format("lib/bullshit") THRESHOLD = 0.01 MIN_MINUTES = 30 BLOG_ABOUT_IT_THRESHOLD = 1000 @@ -67,25 +65,18 @@ class SystemInformation(Cog): def __init__(self, bot: KingArthur) -> None: self.bot = bot + self.cached_resources = {} self.cached_blogcom = None self.cached_bullshit = None self.last_sent = None - async def fetch_blogcom(self) -> str: - """Fetch the blogcom file from the upstream location, or return the cached copy.""" - if not self.cached_blogcom: - async with aiohttp.ClientSession() as session, session.get(BLOGCOM) as resp: - self.cached_blogcom = await resp.text() - - return self.cached_blogcom - - async def fetch_bullshit(self) -> str: - """Fetch the bullshit file from the upstream location, or return the cached copy.""" - if not self.cached_bullshit: - async with aiohttp.ClientSession() as session, session.get(BULLSHIT) as resp: - self.cached_bullshit = await resp.text() - - return self.cached_bullshit + async def fetch_resource(self, name: str) -> str: + """Fetch the file contents of the given filename, starting from ``/``.""" + if name not in self.cached_resources: + url = BASE_RESOURCE.format(name) + async with aiohttp.ClientSession() as session, session.get(url) as resp: + self.cached_resources[name] = await resp.text() + return self.cached_resources[name] @Cog.listener() async def on_message(self, msg: Message) -> None: @@ -120,7 +111,7 @@ async def on_message(self, msg: Message) -> None: if random.random() < msg_thresh: logger.trace("Criteria hit, generating comment.") if random.random() < 0.9: - blogcom = await self.fetch_blogcom() + blogcom = await self.fetch_resource("lib/blogcom") else: blogcom = self.SUPPORTIVE_PYTHON_DISCORD_COMMENTS @@ -134,7 +125,7 @@ async def on_message(self, msg: Message) -> None: @command(name="software") async def software(self, ctx: Context) -> None: """Return information on installed and available software.""" - bullshit = await self.fetch_bullshit() + bullshit = await self.fetch_resource("lib/bullshit") program = lib9front.generate_buzzwords(bullshit) await ctx.reply(program) From 2769800556fc552b855f30be3b5c6d37bfedf210 Mon Sep 17 00:00:00 2001 From: Johannes Christ Date: Wed, 5 Jun 2024 18:50:08 +0200 Subject: [PATCH 2/2] Add more system information commands --- arthur/exts/systems/system_information.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/arthur/exts/systems/system_information.py b/arthur/exts/systems/system_information.py index 53caac9..12afc0b 100644 --- a/arthur/exts/systems/system_information.py +++ b/arthur/exts/systems/system_information.py @@ -4,6 +4,7 @@ import io import random from datetime import UTC, datetime +from typing import Literal from urllib import parse import aiohttp @@ -175,6 +176,25 @@ async def face( await ctx.reply(file=File(out_bytes, filename="face.png")) + @command(name="wisdom") + async def wisdom( + self, ctx: Context, by: Literal["ken", "rob", "rsc", "theo", "uriel"] | None = None + ) -> None: + """Retrieve some software engineering wisdom.""" + if by is None: + by = random.choice(("ken", "rob", "rsc", "theo", "uriel")) + + contents = await self.fetch_resource(f"lib/{by}") + result = random.choice(contents.splitlines()) + await ctx.reply(result) + + @command(name="troll") + async def troll(self, ctx: Context) -> None: + """Utter statements of utmost importance.""" + contents = await self.fetch_resource("lib/troll") + result = random.choice(contents.splitlines()) + await ctx.reply(result) + async def setup(bot: KingArthur) -> None: """Add cog to bot."""