forked from SAGIRI-kawaii/saya_plugins_collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbbreviatedPrediction.py
71 lines (61 loc) · 2.6 KB
/
AbbreviatedPrediction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import aiohttp
from graia.application.message.chain import MessageChain
from graia.application.message.elements.internal import Plain
from graia.saya import Saya, Channel
from graia.saya.builtins.broadcast.schema import ListenerSchema
from graia.application import GraiaMiraiApplication
from graia.application.event.messages import GroupMessage, Group
from graia.application.message.parser.kanata import Kanata
from graia.application.message.parser.signature import RegexMatch
from graia.application.exceptions import AccountMuted
# 插件信息
__name__ = "AbbreviatedPrediction"
__description__ = "一个可以获取字母缩写内容的插件"
__author__ = "SAGIRI-kawaii"
__usage__ = "在群内发送 `缩写 缩写` 即可"
saya = Saya.current()
channel = Channel.current()
channel.name(__name__)
channel.description(f"{__description__}\n使用方法:{__usage__}")
channel.author(__author__)
@channel.use(ListenerSchema(listening_events=[GroupMessage], inline_dispatchers=[Kanata([RegexMatch('缩写 .*')])]))
async def abbreviated_prediction(app: GraiaMiraiApplication, message: MessageChain, group: Group):
if abbreviation := message.asDisplay()[3:]:
try:
if abbreviation.isalnum():
await app.sendGroupMessage(group, await get_abbreviation_explain(abbreviation))
else:
await app.sendGroupMessage(group, MessageChain.create([Plain(text="缩写部分只能为英文/数字!")]))
except AccountMuted:
pass
async def get_abbreviation_explain(abbreviation: str) -> MessageChain:
url = "https://lab.magiconch.com/api/nbnhhsh/guess"
headers = {
"referer": "https://lab.magiconch.com/nbnhhsh/"
}
data = {
"text": abbreviation
}
async with aiohttp.ClientSession() as session:
async with session.post(url=url, headers=headers, data=data) as resp:
res = await resp.json()
# print(res)
result = "可能的结果:\n"
has_result = False
for i in res:
if "trans" in i:
if i["trans"]:
has_result = True
result += f"{i['name']} => {','.join(i['trans'])}\n"
else:
result += f"{i['name']} => 没找到结果!\n"
else:
if i["inputting"]:
has_result = True
result += f"{i['name']} => {','.join(i['inputting'])}\n"
else:
result += f"{i['name']} => 没找到结果!\n"
if has_result:
return MessageChain.create([Plain(text=result)])
else:
return MessageChain.create([Plain(text="没有找到结果哦~")])