forked from bot/app
feat: 添加了对markdown的简单封装
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
import nonebot
|
||||
from nonebot import on_command
|
||||
from nonebot import on_command, on_message
|
||||
from nonebot.adapters.onebot.v11 import MessageSegment
|
||||
from nonebot.exception import FinishedException
|
||||
from nonebot.params import CommandArg
|
||||
from nonebot.permission import SUPERUSER
|
||||
|
||||
@ -9,6 +10,8 @@ from src.utils.typing import T_Message, T_Bot, v11, T_MessageEvent
|
||||
|
||||
md_test = on_command("mdts", aliases={"会话md"}, permission=SUPERUSER)
|
||||
md_group = on_command("mdg", aliases={"群md"}, permission=SUPERUSER)
|
||||
md_conv = on_command("md",block=False, permission=SUPERUSER)
|
||||
|
||||
|
||||
placeholder = {
|
||||
"[": "[",
|
||||
@ -27,4 +30,17 @@ async def _(bot: T_Bot, event: T_MessageEvent, arg: v11.Message = CommandArg()):
|
||||
bot,
|
||||
message_type=event.message_type,
|
||||
session_id=event.user_id if event.message_type == "private" else event.group_id
|
||||
)
|
||||
)
|
||||
ignore_msg_ids = []
|
||||
last_sent = None
|
||||
@md_conv.handle()
|
||||
async def _(bot: v11.Bot, event: v11.MessageEvent, arg: v11.Message = CommandArg()):
|
||||
if str(event.user_id) == str(bot.self_id) and str(bot.self_id) in ["2751454815"]:
|
||||
nonebot.logger.info("开始处理:%s" % str(event.message_id))
|
||||
|
||||
data, fdata = await send_markdown(str(arg), bot, event.message_type,
|
||||
event.group_id if event.message_type == "group" else event.user_id)
|
||||
await bot.delete_msg(message_id=event.message_id)
|
||||
|
||||
|
||||
nonebot.logger.info("已处理:%s, %s" % (str(data["message_id"]), str(fdata["message_id"])))
|
@ -7,8 +7,7 @@ from nonebot_plugin_alconna import on_alconna
|
||||
from src.utils.data import LiteModel
|
||||
from src.utils.message import send_markdown
|
||||
from src.utils.typing import T_Bot, T_MessageEvent
|
||||
|
||||
pushes = []
|
||||
from src.utils.data import Database
|
||||
|
||||
|
||||
class Node(LiteModel):
|
||||
@ -23,8 +22,12 @@ class Node(LiteModel):
|
||||
class Push(LiteModel):
|
||||
source: Node
|
||||
target: Node
|
||||
inde: int
|
||||
|
||||
|
||||
pushes_db = Database("data/pushes.ldb")
|
||||
pushes_db.auto_migrate(Push, Node)
|
||||
|
||||
alc = Alconna(
|
||||
"lep",
|
||||
Subcommand(
|
||||
@ -55,17 +58,20 @@ async def _(result: Arparma):
|
||||
if source and target:
|
||||
source = source.split(".")
|
||||
target = target.split(".")
|
||||
pushes.append(Push(
|
||||
push1 = Push(
|
||||
source=Node(bot_id=source[0], session_type=source[1], session_id=source[2]),
|
||||
target=Node(bot_id=target[0], session_type=target[1], session_id=target[2])
|
||||
))
|
||||
target=Node(bot_id=target[0], session_type=target[1], session_id=target[2]),
|
||||
inde=len(pushes_db.all(Push, default=[]))
|
||||
)
|
||||
pushes_db.save(push1)
|
||||
|
||||
if result.subcommands["add"].args.get("-b"):
|
||||
pushes.append(Push(
|
||||
if result.subcommands["add"].args.get("bidirectional"):
|
||||
push2 = Push(
|
||||
source=Node(bot_id=target[0], session_type=target[1], session_id=target[2]),
|
||||
target=Node(bot_id=source[0], session_type=source[1], session_id=source[2])
|
||||
))
|
||||
|
||||
target=Node(bot_id=source[0], session_type=source[1], session_id=source[2]),
|
||||
inde=len(pushes_db.all(Push, default=[]))
|
||||
)
|
||||
pushes_db.save(push2)
|
||||
await add_push.finish("添加成功")
|
||||
else:
|
||||
await add_push.finish("参数缺失")
|
||||
@ -73,27 +79,31 @@ async def _(result: Arparma):
|
||||
index = result.subcommands["rm"].args.get("index")
|
||||
if index is not None:
|
||||
try:
|
||||
pushes.pop(index)
|
||||
pushes_db.delete(Push, "inde = ?", index)
|
||||
await add_push.finish("删除成功")
|
||||
except IndexError:
|
||||
await add_push.finish("索引错误")
|
||||
else:
|
||||
await add_push.finish("参数缺失")
|
||||
elif result.subcommands.get("list"):
|
||||
await add_push.finish("\n".join([f"{i} {push.source.bot_id}.{push.source.session_type}.{push.source.session_id} -> "
|
||||
f"{push.target.bot_id}.{push.target.session_type}.{push.target.session_id}" for i, push in enumerate(pushes)]))
|
||||
await add_push.finish(
|
||||
"\n".join([f"{push.inde} {push.source.bot_id}.{push.source.session_type}.{push.source.session_id} -> "
|
||||
f"{push.target.bot_id}.{push.target.session_type}.{push.target.session_id}" for i, push in
|
||||
enumerate(pushes_db.all(Push, default=[]))]))
|
||||
else:
|
||||
await add_push.finish("参数错误")
|
||||
|
||||
|
||||
@on_message(block=False).handle()
|
||||
async def _(event: T_MessageEvent, bot: T_Bot):
|
||||
for push in pushes:
|
||||
for push in pushes_db.all(Push, default=[]):
|
||||
if str(push.source) == f"{bot.self_id}.{event.message_type}.{event.user_id if event.message_type == 'private' else event.group_id}":
|
||||
bot2 = nonebot.get_bot(push.target.bot_id)
|
||||
msg_formatted = ""
|
||||
for l in str(event.message).split("\n"):
|
||||
msg_formatted += f"**{l.strip()}**\n"
|
||||
push_message = f"{msg_formatted}\n\n> From {event.sender.nickname}@{push.source.session_type}.{push.source.session_id}\n> Bot {bot.self_id}"
|
||||
push_message = (
|
||||
f"> From {event.sender.nickname}@{push.source.session_type}.{push.source.session_id}\n> Bot {bot.self_id}\n\n"
|
||||
f"{msg_formatted}")
|
||||
await send_markdown(push_message, bot2, push.target.session_type, push.target.session_id)
|
||||
return
|
||||
|
Reference in New Issue
Block a user