mirror of
https://github.com/TriM-Organization/LiteyukiBot-TriM.git
synced 2024-11-11 01:27:29 +08:00
Core适配satori
This commit is contained in:
parent
24722447da
commit
251bfaf410
@ -37,6 +37,7 @@ markdown_image = common_db.where_one(StoredConfig(), default=StoredConfig()).con
|
|||||||
),
|
),
|
||||||
permission=SUPERUSER
|
permission=SUPERUSER
|
||||||
).handle()
|
).handle()
|
||||||
|
# Satori OK
|
||||||
async def _(bot: T_Bot, matcher: Matcher, result: Arparma):
|
async def _(bot: T_Bot, matcher: Matcher, result: Arparma):
|
||||||
if result.main_args.get("text"):
|
if result.main_args.get("text"):
|
||||||
await matcher.finish(Message(unescape(result.main_args.get("text"))))
|
await matcher.finish(Message(unescape(result.main_args.get("text"))))
|
||||||
@ -51,9 +52,11 @@ async def _(bot: T_Bot, matcher: Matcher, result: Arparma):
|
|||||||
),
|
),
|
||||||
permission=SUPERUSER
|
permission=SUPERUSER
|
||||||
).handle()
|
).handle()
|
||||||
|
# Satori OK
|
||||||
async def _(bot: T_Bot, event: T_MessageEvent):
|
async def _(bot: T_Bot, event: T_MessageEvent):
|
||||||
# 使用git pull更新
|
# 使用git pull更新
|
||||||
ulang = get_user_lang(str(event.user_id))
|
|
||||||
|
ulang = get_user_lang(str(event.user.id if isinstance(event, satori.event.Event) else event.user_id))
|
||||||
success, logs = update_liteyuki()
|
success, logs = update_liteyuki()
|
||||||
reply = "Liteyuki updated!\n"
|
reply = "Liteyuki updated!\n"
|
||||||
reply += f"```\n{logs}\n```\n"
|
reply += f"```\n{logs}\n```\n"
|
||||||
@ -70,18 +73,19 @@ async def _(bot: T_Bot, event: T_MessageEvent):
|
|||||||
),
|
),
|
||||||
permission=SUPERUSER
|
permission=SUPERUSER
|
||||||
).handle()
|
).handle()
|
||||||
|
# Satori OK
|
||||||
async def _(matcher: Matcher, bot: T_Bot, event: T_MessageEvent):
|
async def _(matcher: Matcher, bot: T_Bot, event: T_MessageEvent):
|
||||||
await matcher.send("Liteyuki reloading")
|
await matcher.send("Liteyuki reloading")
|
||||||
temp_data = common_db.where_one(TempConfig(), default=TempConfig())
|
temp_data = common_db.where_one(TempConfig(), default=TempConfig())
|
||||||
|
|
||||||
temp_data.data.update(
|
temp_data.data.update(
|
||||||
{
|
{
|
||||||
"reload" : True,
|
"reload": True,
|
||||||
"reload_time" : time.time(),
|
"reload_time": time.time(),
|
||||||
"reload_bot_id" : bot.self_id,
|
"reload_bot_id": bot.self_id,
|
||||||
"reload_session_type": event.message_type,
|
"reload_session_type": satori_utils.get_message_type(event),
|
||||||
"reload_session_id" : event.group_id if event.message_type == "group" else event.user_id,
|
"reload_session_id": (event.group_id if event.message_type == "group" else event.user_id) if not isinstance(event,satori.event.Event) else event.channel.id,
|
||||||
"delta_time" : 0
|
"delta_time": 0
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -112,8 +116,9 @@ async def _(matcher: Matcher, bot: T_Bot, event: T_MessageEvent):
|
|||||||
),
|
),
|
||||||
permission=SUPERUSER
|
permission=SUPERUSER
|
||||||
).handle()
|
).handle()
|
||||||
|
# Satori OK
|
||||||
async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot, matcher: Matcher):
|
async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot, matcher: Matcher):
|
||||||
ulang = get_user_lang(str(event.user_id))
|
ulang = get_user_lang(str(satori_utils.get_user_id(event)))
|
||||||
stored_config: StoredConfig = common_db.where_one(StoredConfig(), default=StoredConfig())
|
stored_config: StoredConfig = common_db.where_one(StoredConfig(), default=StoredConfig())
|
||||||
if result.subcommands.get("set"):
|
if result.subcommands.get("set"):
|
||||||
key, value = result.subcommands.get("set").args.get("key"), result.subcommands.get("set").args.get("value")
|
key, value = result.subcommands.get("set").args.get("key"), result.subcommands.get("set").args.get("value")
|
||||||
@ -159,15 +164,17 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot, matcher: Matcher
|
|||||||
),
|
),
|
||||||
permission=SUPERUSER
|
permission=SUPERUSER
|
||||||
).handle()
|
).handle()
|
||||||
|
# Satori OK
|
||||||
async def _(event: T_MessageEvent, matcher: Matcher):
|
async def _(event: T_MessageEvent, matcher: Matcher):
|
||||||
global markdown_image
|
global markdown_image
|
||||||
# 切换图片模式,False以图片形式发送,True以markdown形式发送
|
# 切换图片模式,False以图片形式发送,True以markdown形式发送
|
||||||
ulang = get_user_lang(str(event.user_id))
|
ulang = get_user_lang(str(satori_utils.get_user_id(event)))
|
||||||
stored_config: StoredConfig = common_db.where_one(StoredConfig(), default=StoredConfig())
|
stored_config: StoredConfig = common_db.where_one(StoredConfig(), default=StoredConfig())
|
||||||
stored_config.config["markdown_image"] = not stored_config.config.get("markdown_image", False)
|
stored_config.config["markdown_image"] = not stored_config.config.get("markdown_image", False)
|
||||||
markdown_image = stored_config.config["markdown_image"]
|
markdown_image = stored_config.config["markdown_image"]
|
||||||
common_db.save(stored_config)
|
common_db.save(stored_config)
|
||||||
await matcher.finish(ulang.get("liteyuki.image_mode_on" if stored_config.config["markdown_image"] else "liteyuki.image_mode_off"))
|
await matcher.finish(
|
||||||
|
ulang.get("liteyuki.image_mode_on" if stored_config.config["markdown_image"] else "liteyuki.image_mode_off"))
|
||||||
|
|
||||||
|
|
||||||
@on_alconna(
|
@on_alconna(
|
||||||
@ -176,6 +183,7 @@ async def _(event: T_MessageEvent, matcher: Matcher):
|
|||||||
),
|
),
|
||||||
aliases={"轻雪文档"},
|
aliases={"轻雪文档"},
|
||||||
).handle()
|
).handle()
|
||||||
|
# Satori OK
|
||||||
async def _(matcher: Matcher):
|
async def _(matcher: Matcher):
|
||||||
await matcher.finish("https://bot.liteyuki.icu/usage")
|
await matcher.finish("https://bot.liteyuki.icu/usage")
|
||||||
|
|
||||||
@ -228,7 +236,8 @@ async def _(result: Arparma, bot: T_Bot, event: T_MessageEvent, matcher: Matcher
|
|||||||
@Bot.on_calling_api # 图片模式检测
|
@Bot.on_calling_api # 图片模式检测
|
||||||
async def test_for_md_image(bot: T_Bot, api: str, data: dict):
|
async def test_for_md_image(bot: T_Bot, api: str, data: dict):
|
||||||
# 截获大图发送,转换为markdown发送
|
# 截获大图发送,转换为markdown发送
|
||||||
if api in ["send_msg", "send_private_msg", "send_group_msg"] and markdown_image and data.get("user_id") != bot.self_id:
|
if api in ["send_msg", "send_private_msg", "send_group_msg"] and markdown_image and data.get(
|
||||||
|
"user_id") != bot.self_id:
|
||||||
if api == "send_msg" and data.get("message_type") == "private" or api == "send_private_msg":
|
if api == "send_msg" and data.get("message_type") == "private" or api == "send_private_msg":
|
||||||
session_type = "private"
|
session_type = "private"
|
||||||
session_id = data.get("user_id")
|
session_id = data.get("user_id")
|
||||||
@ -241,10 +250,12 @@ async def test_for_md_image(bot: T_Bot, api: str, data: dict):
|
|||||||
file: str = data["message"][0].data.get("file")
|
file: str = data["message"][0].data.get("file")
|
||||||
# file:// http:// base64://
|
# file:// http:// base64://
|
||||||
if file.startswith("http"):
|
if file.startswith("http"):
|
||||||
result = await md.send_md(await md.image_async(file), bot, message_type=session_type, session_id=session_id)
|
result = await md.send_md(await md.image_async(file), bot, message_type=session_type,
|
||||||
|
session_id=session_id)
|
||||||
elif file.startswith("file"):
|
elif file.startswith("file"):
|
||||||
file = file.replace("file://", "")
|
file = file.replace("file://", "")
|
||||||
result = await md.send_image(open(file, "rb").read(), bot, message_type=session_type, session_id=session_id)
|
result = await md.send_image(open(file, "rb").read(), bot, message_type=session_type,
|
||||||
|
session_id=session_id)
|
||||||
elif file.startswith("base64"):
|
elif file.startswith("base64"):
|
||||||
file_bytes = base64.b64decode(file.replace("base64://", ""))
|
file_bytes = base64.b64decode(file.replace("base64://", ""))
|
||||||
result = await md.send_image(file_bytes, bot, message_type=session_type, session_id=session_id)
|
result = await md.send_image(file_bytes, bot, message_type=session_type, session_id=session_id)
|
||||||
@ -271,7 +282,7 @@ async def on_shutdown():
|
|||||||
@driver.on_bot_connect
|
@driver.on_bot_connect
|
||||||
async def _(bot: T_Bot):
|
async def _(bot: T_Bot):
|
||||||
temp_data = common_db.where_one(TempConfig(), default=TempConfig())
|
temp_data = common_db.where_one(TempConfig(), default=TempConfig())
|
||||||
if isinstance(bot,satori.Bot):
|
if isinstance(bot, satori.Bot):
|
||||||
await satori_utils.user_infos.load_friends(bot)
|
await satori_utils.user_infos.load_friends(bot)
|
||||||
# 用于重启计时
|
# 用于重启计时
|
||||||
if temp_data.data.get("reload", False):
|
if temp_data.data.get("reload", False):
|
||||||
@ -283,13 +294,19 @@ async def _(bot: T_Bot):
|
|||||||
reload_session_id = temp_data.data.get("reload_session_id", 0)
|
reload_session_id = temp_data.data.get("reload_session_id", 0)
|
||||||
delta_time = temp_data.data.get("delta_time", 0)
|
delta_time = temp_data.data.get("delta_time", 0)
|
||||||
common_db.save(temp_data) # 更新数据
|
common_db.save(temp_data) # 更新数据
|
||||||
await bot.call_api(
|
if isinstance(bot,satori.Bot):
|
||||||
"send_msg",
|
await bot.send_message(
|
||||||
message_type=reload_session_type,
|
channel_id=reload_session_id,
|
||||||
user_id=reload_session_id,
|
message="Liteyuki reloaded in %.2f s" % delta_time
|
||||||
group_id=reload_session_id,
|
)
|
||||||
message="Liteyuki reloaded in %.2f s" % delta_time
|
else:
|
||||||
)
|
await bot.call_api(
|
||||||
|
"send_msg",
|
||||||
|
message_type=reload_session_type,
|
||||||
|
user_id=reload_session_id,
|
||||||
|
group_id=reload_session_id,
|
||||||
|
message="Liteyuki reloaded in %.2f s" % delta_time
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# 每天4点更新
|
# 每天4点更新
|
||||||
@ -308,21 +325,21 @@ async def every_day_update():
|
|||||||
|
|
||||||
# 安全的需要用户id的api
|
# 安全的需要用户id的api
|
||||||
need_user_id = (
|
need_user_id = (
|
||||||
"send_private_msg",
|
"send_private_msg",
|
||||||
"send_msg",
|
"send_msg",
|
||||||
"set_group_card",
|
"set_group_card",
|
||||||
"set_group_special_title",
|
"set_group_special_title",
|
||||||
"get_stranger_info",
|
"get_stranger_info",
|
||||||
"get_group_member_info"
|
"get_group_member_info"
|
||||||
)
|
)
|
||||||
|
|
||||||
need_group_id = (
|
need_group_id = (
|
||||||
"send_group_msg",
|
"send_group_msg",
|
||||||
"send_msg",
|
"send_msg",
|
||||||
"set_group_card",
|
"set_group_card",
|
||||||
"set_group_name",
|
"set_group_name",
|
||||||
"set_group_special_title",
|
"set_group_special_title",
|
||||||
"get_group_member_info",
|
"get_group_member_info",
|
||||||
"get_group_member_list",
|
"get_group_member_list",
|
||||||
"get_group_honor_info"
|
"get_group_honor_info"
|
||||||
)
|
)
|
||||||
|
@ -7,6 +7,7 @@ from PIL import Image
|
|||||||
import aiohttp
|
import aiohttp
|
||||||
import nonebot
|
import nonebot
|
||||||
from nonebot import require
|
from nonebot import require
|
||||||
|
from nonebot.adapters import satori
|
||||||
from nonebot.adapters.onebot import v11
|
from nonebot.adapters.onebot import v11
|
||||||
from typing import Any, Type
|
from typing import Any, Type
|
||||||
|
|
||||||
@ -65,28 +66,37 @@ class MarkdownMessage:
|
|||||||
"""
|
"""
|
||||||
formatted_md = v11.unescape(markdown).replace("\n", r"\n").replace('"', r'\\\"')
|
formatted_md = v11.unescape(markdown).replace("\n", r"\n").replace('"', r'\\\"')
|
||||||
if event is not None and message_type is None:
|
if event is not None and message_type is None:
|
||||||
message_type = event.message_type
|
if isinstance(event, satori.event.Event):
|
||||||
session_id = event.user_id if event.message_type == "private" else event.group_id
|
message_type = "private" if event.guild is None else "group"
|
||||||
|
group_id = event.guild.id if event.guild is not None else None
|
||||||
|
else:
|
||||||
|
assert event is not None
|
||||||
|
message_type = event.message_type
|
||||||
|
group_id = event.group_id if message_type == "group" else None
|
||||||
|
user_id = event.user.id if isinstance(event, satori.event.Event) else event.user_id
|
||||||
|
session_id = user_id if message_type == "private" else group_id
|
||||||
|
else:
|
||||||
|
pass
|
||||||
try:
|
try:
|
||||||
raise TencentBannedMarkdownError("Tencent banned markdown")
|
raise TencentBannedMarkdownError("Tencent banned markdown")
|
||||||
forward_id = await bot.call_api(
|
forward_id = await bot.call_api(
|
||||||
"send_private_forward_msg",
|
"send_private_forward_msg",
|
||||||
messages=[
|
messages=[
|
||||||
{
|
{
|
||||||
"type": "node",
|
"type": "node",
|
||||||
"data": {
|
"data": {
|
||||||
"content": [
|
"content": [
|
||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"content": "{\"content\":\"%s\"}" % formatted_md,
|
"content": "{\"content\":\"%s\"}" % formatted_md,
|
||||||
},
|
},
|
||||||
"type": "markdown"
|
"type": "markdown"
|
||||||
}
|
|
||||||
],
|
|
||||||
"name" : "[]",
|
|
||||||
"uin" : bot.self_id
|
|
||||||
}
|
}
|
||||||
|
],
|
||||||
|
"name": "[]",
|
||||||
|
"uin": bot.self_id
|
||||||
}
|
}
|
||||||
|
}
|
||||||
],
|
],
|
||||||
user_id=bot.self_id
|
user_id=bot.self_id
|
||||||
|
|
||||||
@ -96,12 +106,12 @@ class MarkdownMessage:
|
|||||||
group_id=session_id,
|
group_id=session_id,
|
||||||
message_type=message_type,
|
message_type=message_type,
|
||||||
message=[
|
message=[
|
||||||
{
|
{
|
||||||
"type": "longmsg",
|
"type": "longmsg",
|
||||||
"data": {
|
"data": {
|
||||||
"id": forward_id
|
"id": forward_id
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
@ -117,12 +127,19 @@ class MarkdownMessage:
|
|||||||
width=540,
|
width=540,
|
||||||
device_scale_factor=4
|
device_scale_factor=4
|
||||||
)
|
)
|
||||||
data = await bot.send_msg(
|
if isinstance(bot, satori.Bot):
|
||||||
message_type=message_type,
|
msg_seg = satori.MessageSegment.image(raw=md_image_bytes,mime="image/png")
|
||||||
group_id=session_id,
|
data = await bot.send(
|
||||||
user_id=session_id,
|
event=event,
|
||||||
message=v11.MessageSegment.image(md_image_bytes),
|
message=msg_seg
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
data = await bot.send_msg(
|
||||||
|
message_type=message_type,
|
||||||
|
group_id=session_id,
|
||||||
|
user_id=session_id,
|
||||||
|
message=v11.MessageSegment.image(md_image_bytes),
|
||||||
|
)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -164,7 +181,8 @@ class MarkdownMessage:
|
|||||||
if method == 2:
|
if method == 2:
|
||||||
base64_string = base64.b64encode(image).decode("utf-8")
|
base64_string = base64.b64encode(image).decode("utf-8")
|
||||||
data = await bot.call_api("upload_image", file=f"base64://{base64_string}")
|
data = await bot.call_api("upload_image", file=f"base64://{base64_string}")
|
||||||
await MarkdownMessage.send_md(MarkdownMessage.image(data, Image.open(io.BytesIO(image)).size), bot, event=event, message_type=message_type,
|
await MarkdownMessage.send_md(MarkdownMessage.image(data, Image.open(io.BytesIO(image)).size), bot,
|
||||||
|
event=event, message_type=message_type,
|
||||||
session_id=session_id, **kwargs)
|
session_id=session_id, **kwargs)
|
||||||
|
|
||||||
# 其他实现端方案
|
# 其他实现端方案
|
||||||
@ -172,13 +190,14 @@ class MarkdownMessage:
|
|||||||
image_message_id = (await bot.send_private_msg(
|
image_message_id = (await bot.send_private_msg(
|
||||||
user_id=bot.self_id,
|
user_id=bot.self_id,
|
||||||
message=[
|
message=[
|
||||||
v11.MessageSegment.image(file=image)
|
v11.MessageSegment.image(file=image)
|
||||||
]
|
]
|
||||||
))["message_id"]
|
))["message_id"]
|
||||||
image_url = (await bot.get_msg(message_id=image_message_id))["message"][0]["data"]["url"]
|
image_url = (await bot.get_msg(message_id=image_message_id))["message"][0]["data"]["url"]
|
||||||
image_size = Image.open(io.BytesIO(image)).size
|
image_size = Image.open(io.BytesIO(image)).size
|
||||||
image_md = MarkdownMessage.image(image_url, image_size)
|
image_md = MarkdownMessage.image(image_url, image_size)
|
||||||
return await MarkdownMessage.send_md(image_md, bot, message_type=message_type, session_id=session_id, event=event, **kwargs)
|
return await MarkdownMessage.send_md(image_md, bot, message_type=message_type, session_id=session_id,
|
||||||
|
event=event, **kwargs)
|
||||||
|
|
||||||
if data is None:
|
if data is None:
|
||||||
data = await bot.send_msg(
|
data = await bot.send_msg(
|
||||||
|
@ -1 +1,3 @@
|
|||||||
from .user_info import user_infos
|
from .user_info import user_infos
|
||||||
|
from .get_message_type import get_message_type
|
||||||
|
from .event_tools import *
|
||||||
|
10
liteyuki/utils/satori_utils/event_tools.py
Normal file
10
liteyuki/utils/satori_utils/event_tools.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
from nonebot.adapters import satori
|
||||||
|
|
||||||
|
from liteyuki.utils.base.ly_typing import T_MessageEvent
|
||||||
|
|
||||||
|
|
||||||
|
def get_user_id(event: T_MessageEvent):
|
||||||
|
if isinstance(event, satori.event.Event):
|
||||||
|
return event.user.id
|
||||||
|
else:
|
||||||
|
return event.user_id
|
10
liteyuki/utils/satori_utils/get_message_type.py
Normal file
10
liteyuki/utils/satori_utils/get_message_type.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
from nonebot.adapters import satori
|
||||||
|
|
||||||
|
from liteyuki.utils.base.ly_typing import T_MessageEvent
|
||||||
|
|
||||||
|
|
||||||
|
def get_message_type(event: T_MessageEvent) -> str:
|
||||||
|
if isinstance(event, satori.event.Event):
|
||||||
|
return "private" if event.guild is None else "group"
|
||||||
|
else:
|
||||||
|
return event.message_type
|
Loading…
Reference in New Issue
Block a user