Package: arclet-alconna-ariadne
Alconna Graia该项目为Alconna为GraiaProject下项目的内建支持包括解析器、Dispatcher、SayaSchema 和 附加组件安装pipinstallarclet-alconna-graia,arclet-alconna-ariadne
pdmaddarclet-alconna-graia,arclet-alconna-ariadne或pipinstallarclet-alconna-graia,arclet-alconna-avilla
pdmaddarclet-alconna-graia,arclet-alconna-avilla或pipinstallarclet-alconna-graia,arclet-alconna-ichika
pdmaddarclet-alconna-graia,arclet-alconna-ichika前提Alconna-Graia 现在依赖Launart的service功能你需要在你使用 Alconna-Graia 组件时加入如下代码：fromlaunartimportLaunartfromarclet.alconna.graiaimportAlconnaGraiaServicefromarclet.alconna.xxximportAlconnaXXXAdapter...manager=Launart(...)manager.add_service(AlconnaGraiaService(AlconnaXXXAdapter))adapater的使用可以直接导入：fromlaunartimportLaunartfromarclet.alconna.graiaimportAlconnaGraiaServiceimportarclet.alconna.xxx...manager=Launart(...)manager.add_service(AlconnaGraiaService())或传入 endpoint, 其等价于直接导入:fromlaunartimportLaunartfromarclet.alconna.graiaimportAlconnaGraiaService...manager=Launart(...)manager.add_service(AlconnaGraiaService("xxx"))不指定 adapter 时 Alconna-Graia 默认使用基础 adapter快速使用单文件ariadne:fromarclet.alconnaimportArgsfromarclet.alconna.graiaimportAlconna,AlconnaDispatcher,Match,CommandResultfromarclet.alconna.graia.serviceimportAlconnaGraiaServiceimportarclet.alconna.ariadne...manager=Launart(...)manager.add_service(AlconnaGraiaService())app=Ariadne(...)alc=Alconna("!jrrp",Args["sth",str,1123])@app.broadcast.receiver(GroupMessage,dispatchers=[AlconnaDispatcher(alc,send_flag='stay')])asyncdeftest2(group:Group,result:CommandResult[GroupMessage],sth:Match[str]):print("sign:",result.result)print("sender:",group)print("match",sth.available,sth.result)avilla:fromarclet.alconnaimportArgsfromarclet.alconna.graiaimportAlconna,AlconnaDispatcher,Match,CommandResultfromarclet.alconna.graia.serviceimportAlconnaGraiaServiceimportarclet.alconna.avilla...broadcast=create(Broadcast)manager=Launart(...)manager.add_service(AlconnaGraiaService())avilla=Avilla(...)alc=Alconna("!jrrp",Args["sth",str,1123])@broadcast.receiver(MessageReceived,dispatchers=[AlconnaDispatcher(alc,send_flag='stay')])asyncdeftest2(context:Context,result:CommandResult[MessageReceived],sth:Match[str]):print("sign:",result.result)print("sender:",context.scene)print("match",sth.available,sth.result)使用 Sayain module.py:fromarclet.alconna.graiaimportAlconna,AlconnaDispatcher,Match,CommandResult,AlconnaSchemafromarclet.alconnaimportArgs...channel=Channel.current()alc=Alconna("!jrrp",Args["sth",str,1123])@channel.use(AlconnaSchema(AlconnaDispatcher(alc)))@channel.use(ListenerSchema([...]))asyncdeftest2(result:CommandResult[...],sth:Match[str]):print("sign:",result.result)print("match",sth.available,sth.result)in main.py:fromarclet.alconna.graiaimportAlconnaBehaviourfromcreartimportcreate...saya=create(Saya)create(AlconnaBehaviour)withsaya.module_context():saya.require("module")使用 Saya Utilin module.py:fromgraiax.shortcut.sayaimportlistenfromarclet.alconna.graiaimportalcommand,Alconna,Match,from_command,startswith,endswithfromarclet.alconnaimportArgs,Arpamar...@alcommand(Alconna("!jrrp",Args["sth",str,1123]),private=False)asyncdeftest1(result:Arpamar,sth:Match[str]):print("sign:",result)print("match",sth.available,sth.result)@alcommand("[!|.]hello <name:str>",send_error=True)asyncdeftest1(result:Arpamar,name:Match[str]):print("sign:",result)print("match",name.available,name.result)@listen(...)@from_command("foo bar{baz}")asyncdeftest2(baz:int):print("baz",baz)@listen(...)@startswith("foo bar")asyncdeftest3(event:...):...@listen(...)@endswith(int)asyncdeftest4(event:...):...in main.py:fromcreartimportcreate...saya=create(Saya)withsaya.module_context():saya.require("module")AlconnaDispatcher 参数说明classAlconnaDispatcher(BaseDispatcher,Generic[TOHandler]):def__init__(self,command:Alconna|AlconnaGroup,*,send_flag:Literal["reply","post","stay"]="stay",skip_for_unmatch:bool=True,comp_session:Optional[CompConfig]=None,message_converter:Callable[[OutType,str],MessageChain|Coroutine[Any,Any,MessageChain]]|None=None,):...command: 使用的 Alconna 指令send_flag: 解析期间输出信息的发送方式reply: 直接发送给指令发送者post: 以事件通过 Broadcast 广播stay: 存入 CommandResult 传递给事件处理器skip_for_unmatch: 解析失败时是否跳过, 否则错误信息按 send_flag 处理comp_session: 补全会话配置, 不传入则不启用补全会话message_converter: send_flag 为 reply 时 输出信息的预处理器附加组件Match: 查询某个参数是否匹配，如foo: Match[int]。使用时以Match.available判断是否匹配成功，以Match.result获取匹配结果Query: 查询某个参数路径是否存在，如sth: Query[int] = Query("foo.bar")；可以指定默认值如Query("foo.bar", 1234)。使用时以Query.available判断是否匹配成功，以Query.result获取匹配结果Header: 表示命令头部为特殊形式时的头部匹配assign: 依托路径是否匹配成功为命令分发处理器。fromarclet.alconna.graiaimportassign,alcommandfromarclet.alconnaimportAlconna,Arpamar...alc=Alconna(...)@alcommand(alc,private=False)@assign("foo")asyncdeffoo(result:Arpamar):...@alcommand(alc,private=False)@assign("bar.baz",1)asyncdefbar_baz_1(result:Arpamar):...便捷方法fromarclet.alconna.graiaimportMatch,Alc...@app.broadcast.receiver(...,dispatchers=[Alc.from_format("foo bar {baz:int}")])asyncdeftest2(baz:Match[int]):print("match",baz.available,baz.result)orfromarclet.alconna.graiaimportMatch,AlconnaSchema...channel=Channel.current()@channel.use(AlconnaSchema.from_("foo {sth:str} bar {baz:int}"))@channel.use(ListenerSchema([...]))asyncdeftest2(sth:Match[str]):print("match",sth.available,sth.result)文档链接