From 2abee4572ddb320b3cd31114e7bca62ff91a31df Mon Sep 17 00:00:00 2001 From: Dusty Pianalto Date: Tue, 5 Nov 2019 20:42:47 -0900 Subject: [PATCH] Fix bug when commands have no body --- morpheus/exts/bot.py | 6 ++++-- morpheus/exts/command.py | 23 +++++++++++++---------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/morpheus/exts/bot.py b/morpheus/exts/bot.py index 1c2a27f..47a91eb 100644 --- a/morpheus/exts/bot.py +++ b/morpheus/exts/bot.py @@ -62,7 +62,9 @@ class Bot(Client): if not raw_body.startswith(prefix): return None raw_body = raw_body.lstrip(prefix) - called_with, body = raw_body.split(' ', 1) + body_list = raw_body.split(' ', 1) + called_with = body_list[0] + body = body_list[1] if len(body_list) > 1 else None return Context.get_context(event, prefix, called_with, body) async def process_command(self, event): @@ -73,7 +75,7 @@ class Bot(Client): command = self.commands.get(ctx.called_with) if not command: return - await command.invoke(ctx, ctx.body.split(' ')) + await command.invoke(ctx, ctx.body.split(' ') if ctx.body else None) def listener(self, name=None): def decorator(func): diff --git a/morpheus/exts/command.py b/morpheus/exts/command.py index 2d10f1d..096ce2f 100644 --- a/morpheus/exts/command.py +++ b/morpheus/exts/command.py @@ -69,13 +69,16 @@ class Command: args = [] kwargs = {} - params = self.parser.parse_args(args_list) - - for key, value in iterator: - value: inspect.Parameter - if value.kind == value.VAR_POSITIONAL or value.kind == value.POSITIONAL_OR_KEYWORD: - args.extend(params.__dict__[key]) - else: - kwargs[key] = params.__dict__[key] - - await self.function(ctx, *args, **kwargs) + if args_list: + params = self.parser.parse_args(args_list) + + for key, value in iterator: + value: inspect.Parameter + if value.kind == value.VAR_POSITIONAL or value.kind == value.POSITIONAL_OR_KEYWORD: + args.extend(params.__dict__[key]) + else: + kwargs[key] = params.__dict__[key] + + await self.function(ctx, *args, **kwargs) + else: + await self.function(ctx)