Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved system resource support #207

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions arthur/exts/systems/system_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io
import random
from datetime import UTC, datetime
from typing import Literal
from urllib import parse

import aiohttp
Expand All @@ -17,8 +18,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
Expand Down Expand Up @@ -67,25 +66,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:
Expand Down Expand Up @@ -120,7 +112,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

Expand All @@ -134,7 +126,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)

Expand Down Expand Up @@ -184,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."""
Expand Down