⚗️ new dynamic handler args

This commit is contained in:
yanyongyu
2020-12-16 23:13:00 +08:00
parent ca8f764b8b
commit 784bc7b0cf
5 changed files with 84 additions and 58 deletions

View File

@ -7,7 +7,7 @@ from nonebot.message import event_preprocessor, run_preprocessor
@event_preprocessor
async def handle(bot: Bot, event: Event, state: State):
state["preprocessed"] = True
print(event)
print(type(event), event)
@run_preprocessor

View File

@ -8,14 +8,14 @@ weather = on_command("天气", rule=to_me(), priority=1)
@weather.handle()
async def handle_first_receive(bot: Bot, event: Event, state: State):
args = str(event.message).strip() # 首次发送命令时跟随的参数,例:/天气 上海则args为上海
args = str(event.get_message()).strip() # 首次发送命令时跟随的参数,例:/天气 上海则args为上海
print(f"==={args}===")
if args:
state["city"] = args # 如果用户发送了参数则直接赋值
@weather.got("city", prompt="你想查询哪个城市的天气呢?")
async def handle_city(bot: Bot, event: Event, state: State):
async def handle_city(bot: Bot, state: State):
city = state["city"]
if city not in ["上海", "北京"]:
await weather.reject("你想查询的城市暂不支持,请重新输入!")