123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import logging
- import inspect
- from ...messaging import RollbotResponse, RollbotFailure, RollbotFailureException
- from ..base import RollbotPlugin
- from .arg_wiring import get_converters
- def as_plugin(command):
- if isinstance(command, str):
- command_name = command
- else:
- command_name = command.__name__
- def init_standin(self, bot, logger=logging.getLogger(__name__)):
- RollbotPlugin.__init__(self, command_name, bot, logger=logger)
- def decorator(fn):
- sig = inspect.signature(fn)
- try:
- converters = get_converters(sig.parameters, fn.__annotations__)
- except ValueError as ve:
- raise ValueError(f"Illegal argument name {str(ve)} in decorated plugin {command_name}")
- def on_command_standin(self, db, msg):
- try:
- res = fn(*[c(self, db, msg) for c in converters])
- except RollbotFailureException as rfe:
- res = rfe.failure
- if res is None:
- return RollbotResponse(msg, respond=False)
- elif isinstance(res, RollbotResponse):
- return res
- elif isinstance(res, RollbotFailure):
- return RollbotResponse(msg, failure=res, debugging=res.get_debugging())
- else:
- return RollbotResponse(msg, txt=str(res))
- return type(
- f"AutoGenerated`{command_name}`Command",
- (RollbotPlugin,),
- dict(
- __init__=init_standin,
- on_command=on_command_standin,
- )
- )
- if isinstance(command, str):
- return decorator
- else:
- return decorator(command)
|