Support Alconna to GraiaProject
Project description
Alconna Graia
该项目为 Alconna
为 GraiaProject
下项目的内建支持
包括解析器、Dispatcher、SayaSchema 和 附加组件
快速使用
单文件
from arclet.alconna.graia import AlconnaDispatcher, Match, AlconnaProperty
from arclet.alconna import Alconna, Args
...
app = Ariadne(...)
alc = Alconna("!jrrp", Args["sth", str, 1123])
@app.broadcast.receiver(
GroupMessage,
dispatchers=[AlconnaDispatcher(alc, send_flag='stay')]
)
async def test2(
group: Group,
result: AlconnaProperty[GroupMessage],
sth: Match[str]
):
print("sign:", result.result)
print("sender:", group)
print("match", sth.available, sth.result)
使用 Saya
in module.py:
from arclet.alconna.graia import Alc, Match, AlconnaProperty, AlconnaSchema
from arclet.alconna import Alconna, Args
...
channel = Channel.current()
alc = Alconna("!jrrp", Args["sth", str, 1123])
@channel.use(AlconnaSchema(Alc(alc)))
@channel.use(ListenerSchema([GroupMessage]))
async def test2(group: Group, result: AlconnaProperty[GroupMessage], sth: Match[str]):
print("sign:", result.result)
print("sender:", group)
print("match", sth.available, sth.result)
in main.py:
from arclet.alconna.graia import AlconnaBehaviour
from creart import create
...
saya = create(Saya)
create(AlconnaBehaviour)
with saya.module_context():
saya.require("module")
使用 Saya Util
in module.py:
from graiax.shortcut.saya import listen
from arclet.alconna.graia import Match, alcommand, from_command, startswith, endswith
from arclet.alconna import Alconna, Args, Arpamar
...
@alcommand(Alconna("!jrrp", Args["sth", str, 1123]), private=False)
async def test1(group: Group, result: Arpamar, sth: Match[str]):
print("sign:", result)
print("sender:", group)
print("match", sth.available, sth.result)
@alcommand("[!|.]hello <name:str>;say <word>", send_error=True)
async def test1(group: Group, result: Arpamar, name: Match[str]):
print("sign:", result)
print("sender:", group)
print("match", name.available, name.result)
@listen(GroupMessage)
@from_command("foo bar {baz}")
async def test2(baz: int):
print("baz", baz)
@listen(GroupMessage)
@startswith("foo bar")
async def test3(event: GroupMessage):
...
@listen(GroupMessage)
@endswith(int)
async def test4(event: GroupMessage):
...
in main.py:
from creart import create
...
saya = create(Saya)
with saya.module_context():
saya.require("module")
AlconnaDispatcher 参数说明
class AlconnaDispatcher(BaseDispatcher):
def __init__(
self,
command: Union[Alconna, AlconnaGroup],
*,
send_flag: Literal["reply", "post", "stay"] = "stay",
skip_for_unmatch: bool = True,
send_handler: Optional[Callable[[str], MessageChain]] = None,
): ...
command
: 使用的 Alconna 指令
send_flag
: 解析期间输出信息的发送方式
- reply: 直接发送给指令发送者
- post: 以事件通过 Broadcast 广播
- stay: 存入 AlconnaProperty 传递给事件处理器
skip_for_unmatch
: 解析失败时是否跳过, 否则错误信息按 send_flag 处理
send_handler
: 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
获取匹配结果 -
assign
: 依托路径是否匹配成功为命令分发处理器。
from arclet.alconna.graia import alcommand, assign
from arclet.alconna import Alconna, Args, Arpamar
...
alc = Alconna(...)
@alcommand(alc, private=False)
@assign("foo")
async def foo(group: Group, result: Arpamar):
...
@alcommand(alc, private=False)
@assign("bar.baz", 1)
async def bar_baz_1(group: Group, result: Arpamar):
...
便捷方法
from arclet.alconna.graia import Alc, Match
...
app = Ariadne(...)
@app.broadcast.receiver(
GroupMessage, dispatchers=[Alc.from_format("foo bar {baz:int}")]
)
async def test2(group: Group, baz: Match[int]):
print("sender:", group)
print("match", baz.available, baz.result)
or
from arclet.alconna.graia import Match, AlconnaSchema
...
channel = Channel.current()
@channel.use(AlconnaSchema.from_("foo <arg:str>", "bar"))
@channel.use(ListenerSchema([GroupMessage]))
async def test2(group: Group, sth: Match[str]):
print("sender:", group)
print("match", sth.available, sth.result)
文档
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for arclet-alconna-graia-0.10.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26fedba4455982e3c9761fab286d71e876eb4303aa9e4355e509917037026f24 |
|
MD5 | 8cc5467b04299f920e3bcd48b5308b21 |
|
BLAKE2b-256 | 7cfaef37d3ceacffc146a214740b2189f0a4853bf0105fb386a7fbf55dc473f8 |
Close
Hashes for arclet_alconna_graia-0.10.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | be0d11bb32ecb408f791df0da05105535f447717729d410eca2dc138f6d86b8b |
|
MD5 | 22a98f7be7339adcf2c457cb8d9286af |
|
BLAKE2b-256 | cacbed6f78357571704f8ede99529bdc1ab2bb762bb67c867b8d8781ca605205 |