Package: nonebot-plugin-alconna
NoneBot Plugin Alconna✨ Alconna Usage For NoneBot2 ✨该插件提供了Alconna的NoneBot2适配版本与工具特性完整的 Alconna 特性支持基本的 rule, matcher 与 依赖注入自动回复命令帮助信息 (help, shortcut, completion) 选项现有全部协议的 Segment 标注match_value, match_path 等检查函数补全会话支持跨平台的接收与发送消息讨论QQ 交流群:链接使用方法NoneBot 文档:📖这里仓库内介绍:📦这里消息解析fromnonebot.adapters.onebot.v12importMessage,MessageSegmentfromarclet.alconnaimportAlconna,Option,Argsmsg=Message("Hello! --foo 123")img=MessageSegment.image("1.png")print(msg)alc=Alconna("Hello!",Option("--foo",Args["foo",int]))res=alc.parse(msg)assertres.matchedassertres.query("foo.foo")==123assertnotalc.parse(Message(["Hello!",img])).matchedMessageSegment 标注特定适配器:fromnonebot_plugin_alconna.adapters.onebot12importMentionfromnonebot.adapters.onebot.v12importMessagefromarclet.alconnaimportAlconna,Argsmsg=Message(["Hello!",Mention("123")])print(msg)# Hello![mention:user_id=123]alc=Alconna("Hello!",Args["target",Mention])res=alc.parse(msg)assertres.matchedassertres.query("target").data['user_id']=='123'通用标注:fromnonebot.adapters.onebot.v12importMessageasOb12Msg,MessageSegmentasOb12MSfromnonebot.adapters.onebot.v11importMessageasOb11Msg,MessageSegmentasOb11MSfromnonebot_plugin_alconnaimportAtfromarclet.alconnaimportAlconna,Argsmsg1=Ob12Msg(["Hello!",Ob12MS.mention("123")])# Hello![mention:user_id=123]msg2=Ob11Msg(["Hello!",Ob11MS.at(123)])# Hello![CQ:at,qq=123]alc=Alconna("Hello!",Args["target",At])res1=alc.parse(msg1)assertres1.matchedtarget=res1.query("target")assertisinstance(target,At)asserttarget.target=='123'res2=alc.parse(msg2)assertres2.matchedtarget=res2.query("target")assertisinstance(target,At)asserttarget.target=='123'Matcher 与 依赖注入...fromnonebotimportrequirerequire("nonebot_plugin_alconna")...fromnonebot_plugin_alconnaimport(on_alconna,Match,Query,AlconnaMatch,AlconnaQuery,AlcMatches,AlcResult)fromarclet.alconnaimportAlconna,Args,Optiontest=on_alconna(Alconna("test",Option("foo",Args["bar",int]),Option("baz",Args["qux",bool,False])),auto_send_output=True)@test.handle()asyncdefhandle_test1(result:AlcResult):awaittest.send(f"matched:{result.matched}")awaittest.send(f"maybe output:{result.output}")@test.handle()asyncdefhandle_test2(result:AlcMatches):awaittest.send(f"head result:{result.header_result}")awaittest.send(f"args:{result.all_matched_args}")@test.handle()asyncdefhandle_test3(bar:Match[int]=AlconnaMatch("bar")):ifbar.available:awaittest.send(f"foo={bar.result}")@test.handle()asyncdefhandle_test4(qux:Query[bool]=AlconnaQuery("baz.qux",False)):ifqux.available:awaittest.send(f"baz.qux={qux.result}")条件控制...fromnonebotimportrequirerequire("nonebot_plugin_alconna")...fromarclet.alconnaimportAlconna,Subcommand,Option,Argsfromnonebot_plugin_alconnaimporton_alconna,AlconnaResult,CommandResultpip=Alconna("pip",Subcommand("install",Args["pak",str],Option("--upgrade"),Option("--force-reinstall")),Subcommand("list",Option("--out-dated")))pip_cmd=on_alconna(pip)@pip_cmd.assign("install.pak","pip")asyncdefupdate(arp:CommandResult=AlconnaResult()):...@pip_cmd.assign("list")asyncdeflist_(arp:CommandResult=AlconnaResult()):...install_cmd=pip_cmd.dispatch("install")@install_cmd.handle()asyncdefinstall(arp:CommandResult=AlconnaResult()):...@install_cmd.assign("install.pak","nonebot")asyncdefnonebot(arp:CommandResult=AlconnaResult()):...Duplication...fromnonebotimportrequirerequire("nonebot_plugin_alconna")...fromnonebot_plugin_alconnaimport(on_alconna,AlconnaDuplication)fromarclet.alconnaimportAlconna,Args,Duplication,Option,OptionStubtest=on_alconna(Alconna("test",Option("foo",Args["bar",int]),Option("baz",Args["qux",bool,False])),auto_send_output=True)classMyResult(Duplication):bar:intqux:boolfoo:OptionStub@test.handle()asyncdefhandle_test1(result:MyResult=AlconnaDuplication(MyResult)):awaittest.send(f"matched: bar={result.bar}, qux={result.qux}")awaittest.send(f"options: foo={result.foo.origin}")配置目前配置项有：ALCONNA_AUTO_SEND_OUTPUT : 是否全局启用输出信息自动发送ALCONNA_USE_COMMAND_START : 是否将 COMMAND_START 作为全局命令前缀ALCONNA_AUTO_COMPLETION: 是否全局启用补全会话功能ALCONNA_USE_ORIGIN: 是否全局使用原始消息 (即未经过 to_me 等处理的)ALCONNA_USE_PARAM: 是否使用特制的 Param 提供更好的依赖注入ALCONNA_USE_CMD_SEP: 是否将 COMMAND_SEP 作为全局命令分隔符ALCONNA_GLOBAL_EXTENSIONS: 全局加载的扩展, 路径以 . 分隔, 如 foo.bar.baz:DemoExtension参数解释defon_alconna(command:Alconna|str,skip_for_unmatch:bool=True,auto_send_output:bool=False,aliases:set[str|tuple[str,...]]|None=None,comp_config:CompConfig|None=None,extensions:list[type[Extension]|Extension]|None=None,exclude_ext:list[type[Extension]|str]|None=None,use_origin:bool=False,use_cmd_start:bool=False,use_cmd_sep:bool=False,**kwargs,)->type[AlconnaMatcher]:command: Alconna 命令skip_for_unmatch: 是否在命令不匹配时跳过该响应auto_send_output: 是否自动发送输出信息并跳过响应aliases: 命令别名, 作用类似于on_commandcomp_config: 补全会话配置, 不传入则不启用补全会话extensions: 需要加载的匹配扩展, 可以是扩展类或扩展实例exclude_ext: 需要排除的匹配扩展, 可以是扩展类或扩展的iduse_origin: 是否使用未经 to_me 等处理过的消息use_cmd_start: 是否使用 COMMAND_START 作为命令前缀use_cmd_sep: 是否使用 COMMAND_SEP 作为命令分隔符提供了 MessageSegment标注 的协议:协议名称路径OneBot 协议adapters.onebot11, adapters.onebot12Telegramadapters.telegram飞书adapters.feishuGitHubadapters.githubQQ botadapters.qqQQ 频道botadapters.qqguild钉钉adapters.dingConsoleadapters.console开黑啦adapters.kookMiraiadapters.miraiNtchatadapters.ntchatMineCraftadapters.minecraftBiliBili Liveadapters.bilibiliWalle-Qadapters.onebot12Villaadapters.villaDiscordadapters.discordRed 协议adapters.redSatoriadapters.satoriDodo IMadapters.dodo便捷装饰器funcommand装饰器用于将一个接受任意参数，返回str或Message或MessageSegment的函数转换为命令响应器。fromnonebot_plugin_alconnaimportfuncommand@funcommand()asyncdefecho(msg:str):returnmsg跨平台消息fromnonebot_plugin_alconnaimportUniMessage,on_alconnatest=on_alconna("test")@test.handle()asyncdefhandle_test():r=awaitUniMessage.image(path="path/to/img").send()awaitr.reply("图片已发送，10秒后撤回")awaitr.recall(delay=10,index=0)@test.got("foo",prompt=UniMessage.template("{:Reply($message_id)}请输入图片"))asyncdefhandle_foo():awaittest.send("图片已收到")体验demo bot