Skip to main content

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)

Metadata

Release files for graiax-msgparse 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for graiax-msgparse 0.0.4
File Size Uploaded
graiax-msgparse-0.0.4.tar.gz 16.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for graiax-msgparse 0.0.4
File Interpreter ABI Platform
graiax_msgparse-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 33.5 kB

Release files / graiax-msgparse-0.0.4.tar.gz

Download URL graiax-msgparse-0.0.4.tar.gz
Size 16.9 kB
Tags Source
SHA-256 checksum
How to use checksums
5981ae420f71b2de4f4768c52eb852815cba931347719af0d17e9a0a8fb86910
BLAKE2b-256 checksum
How to use checksums
6e274baec0410f9f260676aab6be0a2e32858cea00943a8d123eb2f4ba293d71
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.7 CPython/3.9.8 Windows/10

Release files / graiax_msgparse-0.0.4-py3-none-any.whl

Download URL graiax_msgparse-0.0.4-py3-none-any.whl
Size 16.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
66d37949d85934103731a4809a920bc13f24b267ec2457d021307ca187181015
BLAKE2b-256 checksum
How to use checksums
ae2fec166456b87baecc0c8b1e97393c90c1e5de97bc9025dec738760acc95ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.7 CPython/3.9.8 Windows/10

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page