Handler engine now has support for missing argument defaults

This commit is contained in:
Ben 2019-12-02 23:31:53 +00:00
parent 91a4d8acd5
commit db3825ccca
2 changed files with 15 additions and 10 deletions

View File

@ -88,13 +88,20 @@ class _ArgumentDeserializer:
if ctx.component.annotation is ctx.component.empty and ctx.component.default is not ctx.component.empty: if ctx.component.annotation is ctx.component.empty and ctx.component.default is not ctx.component.empty:
handler_call_arguments.append(ctx.component.default) handler_call_arguments.append(ctx.component.default)
elif ctx.component.kind == ctx.component.POSITIONAL_OR_KEYWORD: elif ctx.component.kind == ctx.component.POSITIONAL_OR_KEYWORD:
ctx.argument = next(ctx.arguments) ctx.argument = next(ctx.arguments, None)
converter = get_converter(ctx.component)
if converter == str: if ctx.argument is None:
self._consume_separated_string(ctx) if ctx.component.default is not ctx.component.empty:
handler_call_arguments.append(ctx.component.default)
else:
raise StopIteration
else:
converter = get_converter(ctx.component)
handler_call_arguments.append(await do_conversion(converter, ctx)) if converter == str:
self._consume_separated_string(ctx)
handler_call_arguments.append(await do_conversion(converter, ctx))
elif ctx.component.kind == ctx.component.VAR_POSITIONAL: elif ctx.component.kind == ctx.component.VAR_POSITIONAL:
for argument in ctx.arguments: for argument in ctx.arguments:
ctx.argument = argument ctx.argument = argument

View File

@ -58,12 +58,10 @@ async def handle_moderator_ban(p, penguin_id: int, ban_type: int, reason: int, d
@handlers.handler(XTPacket('m', 'r')) @handlers.handler(XTPacket('m', 'r'))
async def handle_report(p, penguin_id: int, *reason): async def handle_report(p, penguin_id: int, reason: int = 0):
reason = int(reason[0]) if reason else 0
date_now = datetime.datetime.now() date_now = datetime.datetime.now()
server_id = p.server.server_config['Id'] await Report.create(penguin_id=penguin_id, reporter_id=p.id, report_type=reason,
await Report.create(penguin_id=penguin_id, reporter_id=p.data.id, report_type=reason, date=date_now, server_id=p.server.config.id, room_id=p.room.id)
date=date_now, server_id=server_id, room_id=p.room.id)
@handlers.handler(XTPacket('o', 'moderatormessage')) @handlers.handler(XTPacket('o', 'moderatormessage'))