Red-DiscordBot icon indicating copy to clipboard operation
Red-DiscordBot copied to clipboard

RPC decorator for adding rpc handlers

Open npc203 opened this issue 4 years ago • 0 comments

What component of Red (cog, command, API) would you like to see improvements on?

RPC

Describe the enhancement you're suggesting.

RPC methods are registered manually as of now when a cog is loaded. I'd like propose a decorator similar to commands in dpy, to add RPC methods when a cog is loaded. Something on the lines of @rpc_method which would register the method.

image

Just a quality of life feature.

Anything else?

One way I could see this done is, adding an attribute to the function which is wrapped.

def rpc_method(func):
    """
    Decorator for RPC methods
    """
    func.rpc_method = True
    return func

then whenever a cog is loaded, one could loop around the methods perhaps? somwhere on the lines of

for name, method in inspect.getmembers(cog, predicate=inspect.ismethod):
            if getattr(method, "rpc_method", None) is True:
                self.bot.register_rpc_handler(method)

npc203 avatar Feb 15 '22 09:02 npc203