Replies: 2 comments
-
args_parser
具体区别以前的 并且因为是独立的 但是现在 因为是通过 区别举例a16matcher = on_command("test")
async def parse_int(bot: Bot, event: Event, state: T_State):
"""处理参数,转换成数字"""
print("运行 parse_int")
args = str(event.get_message()).strip()
# 检查输入参数是不是数字
if args.isdigit():
state[state["_current_key"]] = int(args)
else:
await matcher.reject("请只输入数字,不然我没法理解呢!")
@matcher.handle()
async def handle_first_receive(bot: Bot, event: Event, state: T_State):
args = str(event.get_message()).strip()
if args and args.isdigit():
state["number"] = int(args)
@matcher.got("number", prompt="请输入一个数字。", args_parser=parse_int)
@matcher.got("number2", prompt="请再输入一个数字。", args_parser=parse_int)
async def handle_group_message(bot: Bot, event: Event, state: T_State):
await matcher.send(str(state["number"] + state["number2"])) 可实现如下对话
或这样,因为在之前的
b1matcher = on_command("test")
def parse_int(key: str):
"""解析数字,并将结果存入 state 中"""
async def _key_parser(
matcher: Matcher, state: T_State = State(), input: Union[int, Message] = Arg(key)
):
print(f"运行 parse_int, key: {key}")
if isinstance(input, int):
return
plaintext = input.extract_plain_text()
if not plaintext.isdigit():
await matcher.reject_arg(key, "请只输入数字,不然我没法理解呢!")
state[key] = int(plaintext)
return _key_parser
@matcher.handle()
async def handle_first_receive(state: T_State = State(), args: Message = CommandArg()):
plaintext = args.extract_plain_text()
if plaintext.isdigit():
state["number"] = int(plaintext)
@matcher.got("number", prompt="请输入一个数字。", parameterless=[Depends(parse_int("number"))])
@matcher.got("number2", prompt="请再输入一个数字。", parameterless=[Depends(parse_int("number2"))])
async def handle_group_message(number: int = Arg(), number2: int = Arg()):
await matcher.send(str(number + number2)) 无论是否提前设置了
适配示例以我自己的禁言插件为例(这只是其中一种修改方法,比较适合我自己之前写的代码) 将 修改为 |
Beta Was this translation helpful? Give feedback.
-
感谢花时间写贴,节省了不少时间。 补充一条。 基于poetry的插件升级到nonebot2 beta: poetry update # 或 poetry install nonebot2@latest ?
poetry add nonebot-adapter-onebot
poetry remove nonebot-adapter-cqhttp 或基于pip pip install nonebot2 -U
pip install nonebot-adapter-onebot
pip uninstall nonebot-adapter-cqhttp |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
💬 对于 OneBot V11 (CQHTTP)
整理了自有项目所涉及到的变动,作为他人的升级参考
.env.* 文件
loguru.logger
日志等级与debug
解绑,新增log_level
字段可自定义日志等级,日志等级见 loguru.logger#levelsaccess_token
字段名变更为onebot_access_token
驱动器
Adapter
的结构有变化,比如获取名称的地方既然
Adapter
名称变了,反向websocket
路由当然也有变化注册驱动器
Adapter
的结构有变化,所以注册驱动器的方法也有变化插件文件
Adapter
名字都改了,那么 import 的位置自然也就变了由于引入了依赖注入,既有事件函数所传入的参数(nonebot 2.0.0-beta.2 已不需改动此项)state
所需的改动event.message
的变更,对于on_command
事件响应器,现在event.message
会返回含有command
本身的消息on_command
事件响应器现在默认block=False
了对于拥有连续会话的功能,现在
Matcher.got()
获取并保存到字段的消息将会是Message
类型而不是str
类型Beta Was this translation helpful? Give feedback.
All reactions