[docs]classCommands:"""Generic dispatcher that routes *exclamation-prefixed* commands to asynchronous handler functions. Parameters ---------- nostr_client : NostrClient Client instance used to send direct messages back to users. commands : dict[str, Callable[[str, str], Awaitable[None]]] Mapping from *command name* (without the leading ``!``) to an async coroutine accepting ``(command_text, pubkey)``. """
[docs]asyncdefdefault(self,command:str,pubkey:str):"""Fallback handler for *unknown* or *non-command* messages. Parameters ---------- command : str The raw message text received from the user. pubkey : str Hex-encoded public key identifying the sender. The dispatcher will reply to this pubkey via a Nostr DM. """awaitself.nostr_client.send_direct_message(pubkey,f"Invalid command: {command}")
[docs]asyncdefrun_command(self,command:str,pubkey:str):"""Parse the incoming text and forward it to the matching command coroutine. The method expects an *exclamation-prefixed* string such as ``"!help"`` or ``"!deposit 100"``. """ifnotcommand.startswith("!"):awaitself.default(command,pubkey)returncommand=command[1:].strip()ifcommand.split()[0]notinself.commands:awaitself.default(command,pubkey)returnawaitself.commands[command.split()[0]](command,pubkey)