添加对lyfunction的支持

This commit is contained in:
远野千束 2024-05-31 22:59:02 +08:00
parent c2b3018908
commit 074882f092
4 changed files with 19 additions and 9 deletions

View File

@ -216,7 +216,12 @@ async def _(result: Arparma, bot: T_Bot, event: T_MessageEvent, matcher: Matcher
arg = arg.replace("\\=", "EQUAL_SIGN")
if "=" in arg:
key, value = arg.split("=", 1)
_kwargs[key] = value.replace("EQUAL_SIGN", "=")
value = unescape(value.replace("EQUAL_SIGN", "="))
try:
value = eval(value)
except:
value = value
_kwargs[key] = value
else:
_args.append(arg.replace("EQUAL_SIGN", "="))
@ -226,6 +231,7 @@ async def _(result: Arparma, bot: T_Bot, event: T_MessageEvent, matcher: Matcher
await ly_func(*tuple(_args), **_kwargs)
@on_alconna(
command=Alconna(
"/api",
@ -269,6 +275,7 @@ async def _(result: Arparma, bot: T_Bot, event: T_MessageEvent, matcher: Matcher
print(f"API: {api_name}\n\nArgs: \n{args_show}\n\nResult: {result}")
await matcher.finish(f"API: {api_name}\n\nArgs: \n{args_show}\n\nResult: {result}")
# system hook
@Bot.on_calling_api # 图片模式检测
async def test_for_md_image(bot: T_Bot, api: str, data: dict):
@ -300,6 +307,7 @@ async def test_for_md_image(bot: T_Bot, api: str, data: dict):
return
raise MockApiException(result=result)
@driver.on_startup
async def on_startup():
temp_data = common_db.where_one(TempConfig(), default=TempConfig())
@ -309,10 +317,12 @@ async def on_startup():
temp_data.data["delta_time"] = delta_time
common_db.save(temp_data) # 更新数据
@driver.on_shutdown
async def on_shutdown():
pass
@driver.on_bot_connect
async def _(bot: T_Bot):
temp_data = common_db.where_one(TempConfig(), default=TempConfig())
@ -342,6 +352,7 @@ async def _(bot: T_Bot):
message="Liteyuki reloaded in %.2f s" % delta_time
)
# 每天4点更新
@scheduler.scheduled_job("cron", hour=4)
async def every_day_update():
@ -355,6 +366,7 @@ async def every_day_update():
else:
nonebot.logger.info(logs)
# 需要用户id的api
need_user_id = (
"send_private_msg",

View File

@ -1,4 +1,3 @@
var qq={0} msg={1} a=A
api send_private_msg user_id=qq message=msg
sleep 3

View File

@ -1,4 +1,3 @@
var user_id={0}
api friend_poke user_id=user_id
api friend_poke user_id=user_id
sleep 0.2

View File

@ -31,14 +31,16 @@ class LiteyukiFunction:
self.name = name
self.functions: list[str] = list()
self.bot: Bot = None
self.var_data = dict()
self.macro_data = dict()
self.kwargs_data = dict()
self.args_data = list()
self.matcher: Matcher = None
self.end = False
self.sub_tasks: list[asyncio.Task] = list()
async def __call__(self, *args, **kwargs):
self.kwargs_data.update(kwargs)
self.args_data = list(set(self.args_data + list(args)))
for i, cmd in enumerate(self.functions):
r = await self.execute_line(cmd, i, *args, **kwargs)
if r == 0:
@ -62,7 +64,6 @@ class LiteyukiFunction:
line: 行数
Returns:
"""
cmd = cmd.format(*args, **kwargs)
no_head = cmd.split(" ", 1)[1] if len(cmd.split(" ")) > 1 else ""
try:
head, args, kwargs = self.get_args(cmd)
@ -74,7 +75,7 @@ class LiteyukiFunction:
if head == "var":
# 变量定义
self.var_data.update(kwargs)
self.kwargs_data.update(kwargs)
elif head == "cmd":
# 在当前计算机上执行命令
@ -97,7 +98,6 @@ class LiteyukiFunction:
elif head == "nohup":
# 挂起运行
print("挂起运行")
task = asyncio.create_task(self.execute_line(no_head))
self.sub_tasks.append(task)
@ -129,7 +129,7 @@ class LiteyukiFunction:
try:
value = eval(value)
except:
value = self.var_data.get(value, value)
value = self.kwargs_data.get(value, value)
kwargs[key] = value
else:
if i == 0: