没有人需要的提取首字母功能
This commit is contained in:
66
konabot/plugins/tqszm/__init__.py
Normal file
66
konabot/plugins/tqszm/__init__.py
Normal file
@ -0,0 +1,66 @@
|
||||
"""
|
||||
提取首字母
|
||||
|
||||
...谁需要啊!!!
|
||||
"""
|
||||
|
||||
import functools
|
||||
|
||||
from nonebot import on_message
|
||||
from nonebot.rule import KeywordsRule, Rule, ToMeRule
|
||||
from nonebot.adapters.onebot.v11.bot import Bot as OBBot
|
||||
from nonebot.adapters.onebot.v11.event import MessageEvent as OBMessageEvent
|
||||
from nonebot.adapters.onebot.v11.message import MessageSegment as OBMessageSegment
|
||||
from nonebot_plugin_alconna import Text, UniMsg
|
||||
from nonebot_plugin_alconna.uniseg.adapters.onebot11.builder import Onebot11MessageBuilder
|
||||
from pypinyin import pinyin, Style as PinyinStyle
|
||||
|
||||
from konabot.common.longtask import DepLongTaskTarget
|
||||
|
||||
|
||||
keywords = ("szmtq", "tqszm", "提取首字母", "首字母提取", )
|
||||
|
||||
|
||||
cmd_tqszm = on_message(rule=Rule(ToMeRule(), KeywordsRule(*keywords)))
|
||||
|
||||
@cmd_tqszm.handle()
|
||||
async def _(target: DepLongTaskTarget, msg: UniMsg, evt: OBMessageEvent | None = None, bot: OBBot | None = None):
|
||||
texts = ""
|
||||
command_occurred = False
|
||||
is_reply_mode = False
|
||||
|
||||
if evt is not None and bot is not None:
|
||||
reply = await Onebot11MessageBuilder().extract_reply(evt, bot)
|
||||
if reply is not None:
|
||||
is_reply_mode = True
|
||||
for seg in reply.data.get('msg', []):
|
||||
if isinstance(seg, OBMessageSegment) and seg.type == 'text':
|
||||
segtext: str = seg.data.get('text', '')
|
||||
texts += segtext
|
||||
|
||||
if is_reply_mode:
|
||||
for seg in msg:
|
||||
if isinstance(seg, Text):
|
||||
if all(seg.text.strip() != k for k in keywords):
|
||||
return
|
||||
else:
|
||||
for seg in msg:
|
||||
if isinstance(seg, Text):
|
||||
if command_occurred:
|
||||
texts += seg.text
|
||||
continue
|
||||
if any(seg.text.startswith(w) for w in keywords):
|
||||
command_occurred = True
|
||||
texts += next(
|
||||
seg.text.removeprefix(w) for w in keywords
|
||||
if seg.text.startswith(w)
|
||||
).removeprefix(" ")
|
||||
|
||||
result = pinyin(texts, style=PinyinStyle.FIRST_LETTER, errors=lambda x: x)
|
||||
print(result)
|
||||
result_text = functools.reduce(lambda x, y: x + y, [
|
||||
p[0] for p in result if len(p) > 0
|
||||
], "")
|
||||
|
||||
await target.send_message(result_text, at=False)
|
||||
|
||||
19
poetry.lock
generated
19
poetry.lock
generated
@ -3739,6 +3739,23 @@ type = "legacy"
|
||||
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
|
||||
reference = "mirrors"
|
||||
|
||||
[[package]]
|
||||
name = "pypinyin"
|
||||
version = "0.55.0"
|
||||
description = "汉字拼音转换模块/工具."
|
||||
optional = false
|
||||
python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*, <4"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "pypinyin-0.55.0-py2.py3-none-any.whl", hash = "sha256:d53b1e8ad2cdb815fb2cb604ed3123372f5a28c6f447571244aca36fc62a286f"},
|
||||
{file = "pypinyin-0.55.0.tar.gz", hash = "sha256:b5711b3a0c6f76e67408ec6b2e3c4987a3a806b7c528076e7c7b86fcf0eaa66b"},
|
||||
]
|
||||
|
||||
[package.source]
|
||||
type = "legacy"
|
||||
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
|
||||
reference = "mirrors"
|
||||
|
||||
[[package]]
|
||||
name = "pytest"
|
||||
version = "9.0.1"
|
||||
@ -4966,4 +4983,4 @@ reference = "mirrors"
|
||||
[metadata]
|
||||
lock-version = "2.1"
|
||||
python-versions = ">=3.12,<4.0"
|
||||
content-hash = "194661c8f97e7ec5f21603e625aa70d0d2ae4e0b47f10de1578e5f042f3baaf1"
|
||||
content-hash = "59498c038a603c90f051d2f360cb9226ec0fc4470942c0a7cf34f832701f0ce7"
|
||||
|
||||
@ -30,6 +30,7 @@ dependencies = [
|
||||
"aiosqlite (>=0.20.0,<1.0.0)",
|
||||
"sqlparse (>=0.5.0,<1.0.0)",
|
||||
"alibabacloud-green20220302 (>=3.0.1,<4.0.0)",
|
||||
"pypinyin (>=0.55.0,<0.56.0)",
|
||||
]
|
||||
|
||||
[tool.poetry]
|
||||
|
||||
Reference in New Issue
Block a user