Fix args and return values

This commit is contained in:
Richard Chien 2018-06-25 10:41:48 +08:00
parent 8faee1151f
commit 844ade5c31
2 changed files with 11 additions and 16 deletions

View File

@ -27,8 +27,9 @@ class Command:
self.func = func self.func = func
self.permission = permission self.permission = permission
async def run(self, bot, ctx, session) -> Any: async def run(self, bot, session) -> bool:
permission = 0 permission = 0
ctx = session.ctx
if ctx['user_id'] in bot.config.SUPERUSERS: if ctx['user_id'] in bot.config.SUPERUSERS:
permission |= perm.IS_SUPERUSER permission |= perm.IS_SUPERUSER
if ctx['message_type'] == 'private': if ctx['message_type'] == 'private':
@ -56,8 +57,9 @@ class Command:
permission |= perm.IS_DISCUSS permission |= perm.IS_DISCUSS
if isinstance(self.func, Callable) and permission & self.permission: if isinstance(self.func, Callable) and permission & self.permission:
return await self.func(bot, ctx, session) await self.func(bot, session)
return None return True
return False
def _find_command(name: Tuple[str]) -> Optional[Command]: def _find_command(name: Tuple[str]) -> Optional[Command]:
@ -89,8 +91,8 @@ class Session:
self.args = {} self.args = {}
self.last_interaction = None self.last_interaction = None
def require_arg(self, key: str, *, def require_arg(self, key: str, prompt: str = '', *,
interactive: bool = True, prompt: str = ''): interactive: bool = True):
# TODO: 检查 key 是否在 args 中,如果不在,抛出异常,保存 session等待用户填充 # TODO: 检查 key 是否在 args 中,如果不在,抛出异常,保存 session等待用户填充
pass pass
@ -138,8 +140,7 @@ async def handle_command(bot: CQHttp, ctx: Dict[str, Any]) -> bool:
session = Session(cmd=cmd, ctx=ctx, current_arg=''.join(cmd_remained)) session = Session(cmd=cmd, ctx=ctx, current_arg=''.join(cmd_remained))
session.images = [s.data['url'] for s in ctx['message'] session.images = [s.data['url'] for s in ctx['message']
if s.type == 'image' and 'url' in s.data] if s.type == 'image' and 'url' in s.data]
await cmd.run(bot, ctx, session) return await cmd.run(bot, session)
return True
def on_command(name: Union[str, Tuple[str]], aliases: Iterable = (), def on_command(name: Union[str, Tuple[str]], aliases: Iterable = (),

View File

@ -7,16 +7,10 @@ from none.helpers import send
@none.on_command('echo') @none.on_command('echo')
async def echo(bot, ctx, session: Session): async def echo(bot, session: Session):
text = session.require_arg('text') await send(bot, session.ctx, session.current_arg)
await send(bot, session.ctx, text)
@echo.argparser
def _(session: Session):
session.args['text'] = session.current_arg
@none.on_command('say', permission=perm.SUPERUSER) @none.on_command('say', permission=perm.SUPERUSER)
async def _(bot, ctx, session: Session): async def _(bot, session: Session):
await send(bot, session.ctx, unescape(session.current_arg)) await send(bot, session.ctx, unescape(session.current_arg))