A argparse for MessageChain in Graia Framework
Project description
graiax-msgparse
这是什么
这是一个为匹配graia-application-mirai中MessageChain的设计创造出来的一个为匹配MessageChain做出的argparse 注:从0.0.4开始会尝试匹配graia-ariadne进行适配
安装
pip install graiax-msgparse
# 或使用 poetry
poetry add graiax-msgparse
如何使用
注意:在查看示例代码前,个人建议先查看以下文档
graia-document中对Application的介绍
Python-document中对argparse模块的介绍
from graiax.msgparse import MessageChainParser, ParserExit, Element2Msg, Element2Mirai
@bcc.receiver(GroupMessage, dispatchers = [Kanata([RegexMatch('parse.*')])])
async def parse_test(app: GraiaMiraiApplication, group: Group, message: MessageChain, member: Member):
parser = MessageChainParser(start_string = 'parse', description='parse测试')
parser.add_argument('-r')
try:
parser.parse_obj(message, space_in_gap = True)
except ParserExit as e:
await app.sendGroupMessage(group, MessageChain.create([
Plain(str(parser.usage) if e.status == 0 else '参数不足或不正确,请使用 --help 参数查询使用帮助')
]))
方法几乎跟python自带的ArgumentParser使用方法如出一辙(毕竟是继承于此) 我们就先看看二者不同的区别
parser = MessageChainParser(start_string = 'parse')
start_string是你的listener的触发条件,MessageChainParser将会检查message开头时候为start_string中的文字并去除
parser.parse_obj(message, space_in_gap = True)
space_in_gap参数是用于给各个非Plain之间添加缝隙的,因为MessageChainParser的内部处理机制是先将MessageChain转换为字符串,然后再通过shlex.split方法对转换过的字符串进行处理。所以可能会出现一些问题
如(At(123),Plain('-s'))
当sapce_in_gap为True时,将会自动在俩个element之间添加空格
用Element2Msg, Element2Mirai转换非纯Plain消息
由于MessageChainParser中将会把除了Plain以外的元素进行特殊处理,所以假设你的参数中带有At,AtAll,Image参数,那么可能会返回这么一串奇怪的东西
[json_element:eyJ0eXBlIjoiQXQiLCJ0YXJnZXQiOjEyMywiZGlzcGxheSI6bnVsbH0=]
如果需要将这串玩意变成MessageChain或者是Mirai码
你就会需要
MsgString
from graiax.msgparse import MessageChainParser, MsgString
...
parser.add_argument('-r',type = MsgString.decode())#将接收到的信息转换为MessageChain的形式
parser.parse_obj(message)
Project details
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file graiax-msgparse-0.0.4.tar.gz.
File metadata
- Download URL: graiax-msgparse-0.0.4.tar.gz
- Upload date:
- Size: 16.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.7 CPython/3.9.8 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5981ae420f71b2de4f4768c52eb852815cba931347719af0d17e9a0a8fb86910
|
|
| MD5 |
d2de83ac7631a032090714ac7275a5f5
|
|
| BLAKE2b-256 |
6e274baec0410f9f260676aab6be0a2e32858cea00943a8d123eb2f4ba293d71
|
File details
Details for the file graiax_msgparse-0.0.4-py3-none-any.whl.
File metadata
- Download URL: graiax_msgparse-0.0.4-py3-none-any.whl
- Upload date:
- Size: 16.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.7 CPython/3.9.8 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66d37949d85934103731a4809a920bc13f24b267ec2457d021307ca187181015
|
|
| MD5 |
67ee886d0279844e5e8e0ea6f313d948
|
|
| BLAKE2b-256 |
ae2fec166456b87baecc0c8b1e97393c90c1e5de97bc9025dec738760acc95ef
|