53 lines
1.3 KiB
Python
53 lines
1.3 KiB
Python
"""Command decorator for auto-registration of bot commands.
|
|
|
|
Usage:
|
|
@bot_command(name="/idea", description="Create a new idea", requires_auth=True)
|
|
async def cmd_idea(update, context): ...
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import inspect
|
|
from dataclasses import dataclass, field
|
|
from typing import Any, Callable
|
|
|
|
|
|
@dataclass
|
|
class BotCommandDef:
|
|
name: str
|
|
description: str
|
|
requires_auth: bool = False
|
|
handler: Callable | None = None
|
|
|
|
|
|
_registry: list[BotCommandDef] = []
|
|
|
|
|
|
def bot_command(
|
|
name: str,
|
|
description: str,
|
|
requires_auth: bool = False,
|
|
) -> Callable:
|
|
"""Decorator that registers a function as a bot command handler."""
|
|
|
|
def wrapper(func: Callable) -> Callable:
|
|
_registry.append(BotCommandDef(
|
|
name=name,
|
|
description=description,
|
|
requires_auth=requires_auth,
|
|
handler=func,
|
|
))
|
|
return func
|
|
|
|
return wrapper
|
|
|
|
|
|
def get_registered_commands() -> list[BotCommandDef]:
|
|
"""Return all registered command definitions."""
|
|
return list(_registry)
|
|
|
|
|
|
def get_enabled_commands(enabled_names: set[str]) -> list[BotCommandDef]:
|
|
"""Return only commands that are in the enabled set."""
|
|
return [cmd for cmd in _registry if cmd.name in enabled_names]
|