signal-bot/signal_bot/popes_command.py
Krzysztof kuhy Rudnicki 47de47eb61
Some checks are pending
CI / check (push) Waiting to run
release: 0.1.4 «Eleuteriusz» — the list of commands lives in the chat now
Thirteen commands, and the only list of them was a README table nobody in a
Signal group can read — which had itself gone stale two releases earlier,
still advertising four commands after nine more had shipped. 0.1.1 gave each
command an output that says what the command is; that only helps once you
already know to type it.

!pomoc (also !pomocy, !help, !komendy, !commands) prints all fourteen,
grouped, one Polish line each.

The interesting part is that it cannot go stale. CommandRegistry.register now
takes the description as a REQUIRED argument, so a command that does not
explain itself does not compile; and render_help prints any command that
help_command.GROUPS forgot to file, under no heading, rather than dropping
it. Two independent ways to fail closed, because the README proved that a
hand-maintained second list is a list that drifts.

Command also gained `primary`, the first raw alias. The trigger set could not
supply it: those are folded skeletons in an unordered frozenset, so all that
survives of "!papież" there is "!papiez", and the styled Unicode aliases come
out as zalgo. aliases.json already lists the canonical Polish spelling first.

The full trigger table moves to DOCS-commands.md. It did not fit in README —
that file was already sitting exactly on the shared 250-line cap, and the
gate caught the overflow on the first commit attempt.

Exercised in testbot at the unbumped version before this bump: the live
dispatcher ran it and posted the list, journal clean. pytest 708 green at
100% branch coverage, e2e green including a new scenario that asserts !pomoc
mentions a command registered by a different module.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017w1HZ3cZ7neL3rFdQ6VMBA
2026-08-30 21:08:02 +02:00

76 lines
2.5 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""The !papież command: a random pope, his portrait, his Latin name, his article.
The list this draws from is the same signal_bot/popes.json the version ladder
is built on, so the bot posts the popes it is named after.
"""
from __future__ import annotations
import secrets
from typing import TYPE_CHECKING, Final
from urllib.parse import quote
from signal_bot.animals import load_aliases
from signal_bot.versioning import POPES
if TYPE_CHECKING:
from signal_bot.decorators import Decorator
from signal_bot.portraits import PortraitCache
from signal_bot.registry import CommandContext, CommandRegistry, Handler
from signal_bot.versioning import Pope
COMMAND_NAME: Final = "pope"
DESCRIPTION: Final = "losowy papież z portretem i imieniem po łacinie"
WIKI_BASE: Final = "https://pl.wikipedia.org/wiki/"
def random_pope() -> Pope:
"""Return one pope, uniformly at random.
secrets rather than random for the reason animals.paged_url gives: the
choice is not security-sensitive, but ruff's ALL rule set flags `random`
and this repo forbids the noqa that would silence it.
"""
return POPES[secrets.randbelow(len(POPES))]
def article_url(pope: Pope) -> str:
"""Return the pl.wikipedia URL for a pope.
Built from title, not name: half the early popes live at "Ewaryst
(papież)" and only the title carries the disambiguator.
"""
return WIKI_BASE + quote(pope.title.replace(" ", "_"))
def caption_for(pope: Pope) -> str:
"""Return the message that rides along with the portrait."""
return f"{pope.ordinal}. {pope.name} — {pope.latin} ({pope.reign})\n{article_url(pope)}"
def pope_handler(cache: PortraitCache) -> Handler:
"""Build the handler that posts a random pope."""
async def handler(ctx: CommandContext) -> None:
pope = random_pope()
image = await cache.get_base64(pope)
await ctx.reply_image(image, caption_for(pope))
return handler
def register_pope_command(
registry: CommandRegistry,
cache: PortraitCache,
*,
decorate: Decorator | None = None,
aliases: dict[str, list[str]] | None = None,
) -> None:
"""Register !papież under every trigger aliases.json gives it."""
table = load_aliases() if aliases is None else aliases
handler = pope_handler(cache)
if decorate is not None:
handler = decorate(handler)
registry.register(COMMAND_NAME, table[COMMAND_NAME], handler, DESCRIPTION)