Add temporary AI commands and request transcripts
This commit is contained in:
@@ -15,6 +15,7 @@ from app.commands.core import (
|
||||
)
|
||||
from app.commands.dispatcher import CommandDispatcher
|
||||
from app.commands.persistent import load_persistent_commands
|
||||
from app.commands.temporary import TemporaryCommandStore
|
||||
|
||||
__all__ = [
|
||||
"CheckResult",
|
||||
@@ -33,4 +34,5 @@ __all__ = [
|
||||
"require_reply_check",
|
||||
"shared_access_check",
|
||||
"load_persistent_commands",
|
||||
"TemporaryCommandStore",
|
||||
]
|
||||
|
||||
47
app/commands/temporary.py
Normal file
47
app/commands/temporary.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from app.commands.core import CommandRegistry
|
||||
|
||||
|
||||
_COMMAND_NAME_RE = re.compile(r"[a-z][a-z0-9_-]{0,31}")
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class TemporaryCommandStore:
|
||||
"""Registers simple commands that exist only for this bot process."""
|
||||
|
||||
registry: CommandRegistry
|
||||
names: set[str] = field(default_factory=set)
|
||||
|
||||
def create(self, name: str, response: str, description: str | None = None) -> str:
|
||||
normalized_name = name.strip().lower()
|
||||
if not _COMMAND_NAME_RE.fullmatch(normalized_name):
|
||||
raise ValueError(
|
||||
"temporary_command_create: name must start with a letter and contain only "
|
||||
"letters, digits, '-' or '_' (up to 32 characters)"
|
||||
)
|
||||
if self.registry.has_name(normalized_name):
|
||||
raise ValueError(f"temporary_command_create: command .{normalized_name} already exists")
|
||||
|
||||
text = response.strip()
|
||||
if not text:
|
||||
raise ValueError("temporary_command_create: response must not be empty")
|
||||
if len(text) > 3900:
|
||||
raise ValueError("temporary_command_create: response must be at most 3900 characters")
|
||||
|
||||
command_description = (description or "Temporary AI-created command").strip()
|
||||
|
||||
async def handler(ctx: object) -> None:
|
||||
await ctx.reply(text) # type: ignore[attr-defined]
|
||||
|
||||
self.registry.command(
|
||||
normalized_name,
|
||||
command_description,
|
||||
usage=f".{normalized_name}",
|
||||
category="temporary",
|
||||
)(handler)
|
||||
self.names.add(normalized_name)
|
||||
return f"Created temporary owner-only command .{normalized_name}; it will be removed on bot restart"
|
||||
Reference in New Issue
Block a user