aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Joe Banks <[email protected]>2024-06-05 18:03:51 +0100
committerGravatar GitHub <[email protected]>2024-06-05 18:03:51 +0100
commit366f426b6fca6ae91c6b5969f75b9f686213d071 (patch)
treece19c0e96addc3ec5f22c54252c40db2073b8e14
parentUse a face generation algorithm compatible with our ImageMagick version (diff)
parentAdd more system information commands (diff)
Merge pull request #207 from python-discord/improved-system-resource-support
-rw-r--r--arthur/exts/systems/system_information.py49
1 files changed, 30 insertions, 19 deletions
diff --git a/arthur/exts/systems/system_information.py b/arthur/exts/systems/system_information.py
index 3ea28e7..12afc0b 100644
--- a/arthur/exts/systems/system_information.py
+++ b/arthur/exts/systems/system_information.py
@@ -4,6 +4,7 @@ import asyncio
import io
import random
from datetime import UTC, datetime
+from typing import Literal
from urllib import parse
import aiohttp
@@ -17,8 +18,6 @@ from arthur.bot import KingArthur
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 +66,18 @@ I enjoy talking to you. Your mind appeals to me. It resembles my own mind except
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 +112,7 @@ I enjoy talking to you. Your mind appeals to me. It resembles my own mind except
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 +126,7 @@ I enjoy talking to you. Your mind appeals to me. It resembles my own mind except
@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)
@@ -184,6 +176,25 @@ I enjoy talking to you. Your mind appeals to me. It resembles my own mind except
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."""