mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-24 09:05:04 +08:00
Fix args and return values
This commit is contained in:
parent
8faee1151f
commit
844ade5c31
@ -27,8 +27,9 @@ class Command:
|
||||
self.func = func
|
||||
self.permission = permission
|
||||
|
||||
async def run(self, bot, ctx, session) -> Any:
|
||||
async def run(self, bot, session) -> bool:
|
||||
permission = 0
|
||||
ctx = session.ctx
|
||||
if ctx['user_id'] in bot.config.SUPERUSERS:
|
||||
permission |= perm.IS_SUPERUSER
|
||||
if ctx['message_type'] == 'private':
|
||||
@ -56,8 +57,9 @@ class Command:
|
||||
permission |= perm.IS_DISCUSS
|
||||
|
||||
if isinstance(self.func, Callable) and permission & self.permission:
|
||||
return await self.func(bot, ctx, session)
|
||||
return None
|
||||
await self.func(bot, session)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def _find_command(name: Tuple[str]) -> Optional[Command]:
|
||||
@ -89,8 +91,8 @@ class Session:
|
||||
self.args = {}
|
||||
self.last_interaction = None
|
||||
|
||||
def require_arg(self, key: str, *,
|
||||
interactive: bool = True, prompt: str = ''):
|
||||
def require_arg(self, key: str, prompt: str = '', *,
|
||||
interactive: bool = True):
|
||||
# TODO: 检查 key 是否在 args 中,如果不在,抛出异常,保存 session,等待用户填充
|
||||
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.images = [s.data['url'] for s in ctx['message']
|
||||
if s.type == 'image' and 'url' in s.data]
|
||||
await cmd.run(bot, ctx, session)
|
||||
return True
|
||||
return await cmd.run(bot, session)
|
||||
|
||||
|
||||
def on_command(name: Union[str, Tuple[str]], aliases: Iterable = (),
|
||||
|
@ -7,16 +7,10 @@ from none.helpers import send
|
||||
|
||||
|
||||
@none.on_command('echo')
|
||||
async def echo(bot, ctx, session: Session):
|
||||
text = session.require_arg('text')
|
||||
await send(bot, session.ctx, text)
|
||||
|
||||
|
||||
@echo.argparser
|
||||
def _(session: Session):
|
||||
session.args['text'] = session.current_arg
|
||||
async def echo(bot, session: Session):
|
||||
await send(bot, session.ctx, session.current_arg)
|
||||
|
||||
|
||||
@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))
|
||||
|
Loading…
Reference in New Issue
Block a user