mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-03 02:30:01 +08:00
✨ 签到ui改版
This commit is contained in:
@@ -62,8 +62,8 @@ async def _():
|
||||
group_user = []
|
||||
try:
|
||||
group_user = await GroupInfoUser.filter(uid__isnull=False).all()
|
||||
except Exception:
|
||||
logger.warning("获取GroupInfoUser数据uid失败...")
|
||||
except Exception as e:
|
||||
logger.warning("获取GroupInfoUser数据uid失败...", e=e)
|
||||
user2uid = {u.user_id: u.uid for u in group_user}
|
||||
db = Tortoise.get_connection("default")
|
||||
old_sign_list = await db.execute_query_dict(SIGN_SQL)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import nonebot
|
||||
from nonebot.adapters import Bot
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from nonebot.rule import to_me
|
||||
from nonebot_plugin_alconna import Alconna, Args, Match, on_alconna
|
||||
from nonebot_plugin_session import EventSession
|
||||
|
||||
@@ -47,6 +47,7 @@ _matcher = on_alconna(
|
||||
priority=1,
|
||||
block=True,
|
||||
permission=SUPERUSER,
|
||||
rule=to_me(),
|
||||
)
|
||||
|
||||
|
||||
@@ -66,22 +67,3 @@ async def _(bot: Bot, session: EventSession, ver_type: Match[str]):
|
||||
if result:
|
||||
await MessageUtils.build_message(result).finish()
|
||||
await MessageUtils.build_message("更新版本失败...").finish()
|
||||
|
||||
|
||||
# driver = nonebot.get_driver()
|
||||
|
||||
|
||||
# @driver.on_startup
|
||||
# async def _():
|
||||
# result = await UpdateManage.check_version()
|
||||
# print("-----------------------")
|
||||
# print("-----------------------")
|
||||
# print(result)
|
||||
# print("-----------------------")
|
||||
# print("-----------------------")
|
||||
# result = await UpdateManage.update(None, "", "dev")
|
||||
# print("-----------------------")
|
||||
# print("-----------------------")
|
||||
# print(result)
|
||||
# print("-----------------------")
|
||||
# print("-----------------------")
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
import os
|
||||
import platform
|
||||
from pathlib import Path
|
||||
|
||||
import nonebot
|
||||
from nonebot import on_command
|
||||
from nonebot.adapters import Bot
|
||||
from nonebot.params import ArgStr
|
||||
from nonebot.permission import SUPERUSER
|
||||
from nonebot.plugin import PluginMetadata
|
||||
from nonebot.rule import to_me
|
||||
from nonebot_plugin_session import EventSession
|
||||
|
||||
from zhenxun.configs.config import NICKNAME
|
||||
from zhenxun.configs.utils import PluginExtraData
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.utils.enum import PluginType
|
||||
from zhenxun.utils.message import MessageUtils
|
||||
from zhenxun.utils.platform import PlatformUtils
|
||||
|
||||
__plugin_meta__ = PluginMetadata(
|
||||
name="重启",
|
||||
description="执行脚本重启真寻",
|
||||
usage=f"""
|
||||
重启
|
||||
""".strip(),
|
||||
extra=PluginExtraData(
|
||||
author="HibiKier", version="0.1", plugin_type=PluginType.SUPERUSER
|
||||
).dict(),
|
||||
)
|
||||
|
||||
|
||||
_matcher = on_command(
|
||||
"重启",
|
||||
permission=SUPERUSER,
|
||||
rule=to_me(),
|
||||
priority=1,
|
||||
block=True,
|
||||
)
|
||||
|
||||
driver = nonebot.get_driver()
|
||||
|
||||
|
||||
RESTART_MARK = Path() / "is_restart"
|
||||
|
||||
RESTART_FILE = Path() / "restart.sh"
|
||||
|
||||
|
||||
@_matcher.got(
|
||||
"flag",
|
||||
prompt=f"确定是否重启{NICKNAME}?确定请回复[是|好|确定](重启失败咱们将失去联系,请谨慎!)",
|
||||
)
|
||||
async def _(bot: Bot, session: EventSession, flag: str = ArgStr("flag")):
|
||||
if flag.lower() in ["true", "是", "好", "确定", "确定是"]:
|
||||
await MessageUtils.build_message(f"开始重启{NICKNAME}..请稍等...").send()
|
||||
with open(RESTART_MARK, "w", encoding="utf8") as f:
|
||||
f.write(f"{bot.self_id} {session.id1}")
|
||||
logger.info("开始重启真寻...", "重启", session=session)
|
||||
if str(platform.system()).lower() == "windows":
|
||||
import sys
|
||||
|
||||
python = sys.executable
|
||||
os.execl(python, python, *sys.argv)
|
||||
else:
|
||||
os.system("./restart.sh")
|
||||
else:
|
||||
await MessageUtils.build_message("已取消操作...").send()
|
||||
|
||||
|
||||
@driver.on_bot_connect
|
||||
async def _(bot: Bot):
|
||||
if str(platform.system()).lower() != "windows":
|
||||
if not RESTART_FILE.exists():
|
||||
with open(RESTART_FILE, "w", encoding="utf8") as f:
|
||||
f.write(
|
||||
f"pid=$(netstat -tunlp | grep "
|
||||
+ str(bot.config.port)
|
||||
+ " | awk '{print $7}')\n"
|
||||
"pid=${pid%/*}\n"
|
||||
"kill -9 $pid\n"
|
||||
"sleep 3\n"
|
||||
"python3 bot.py"
|
||||
)
|
||||
os.system("chmod +x ./restart.sh")
|
||||
logger.info(
|
||||
"已自动生成 restart.sh(重启) 文件,请检查脚本是否与本地指令符合..."
|
||||
)
|
||||
if RESTART_MARK.exists():
|
||||
with open(RESTART_MARK, "r", encoding="utf8") as f:
|
||||
bot_id, session_id = f.read().split()
|
||||
if bot := nonebot.get_bot(bot_id):
|
||||
if target := PlatformUtils.get_target(bot, session_id):
|
||||
await MessageUtils.build_message(f"{NICKNAME}已成功重启!").send(
|
||||
target, bot=bot
|
||||
)
|
||||
RESTART_MARK.unlink()
|
||||
@@ -72,6 +72,12 @@ __plugin_meta__ = PluginMetadata(
|
||||
default_value=0.05,
|
||||
type=float,
|
||||
),
|
||||
RegisterConfig(
|
||||
key="IMAGE_STYLE",
|
||||
value="zhenxun",
|
||||
help="签到图片样式, [normal, zhenxun]",
|
||||
default_value="zhenxun",
|
||||
),
|
||||
],
|
||||
limits=[PluginCdBlock()],
|
||||
).dict(),
|
||||
|
||||
@@ -7,9 +7,10 @@ from pathlib import Path
|
||||
import nonebot
|
||||
import pytz
|
||||
from nonebot.drivers import Driver
|
||||
from nonebot_plugin_htmlrender import template_to_pic
|
||||
|
||||
from zhenxun.configs.config import NICKNAME, Config
|
||||
from zhenxun.configs.path_config import IMAGE_PATH
|
||||
from zhenxun.configs.path_config import IMAGE_PATH, TEMPLATE_PATH
|
||||
from zhenxun.models.sign_log import SignLog
|
||||
from zhenxun.models.sign_user import SignUser
|
||||
from zhenxun.utils.image_utils import BuildImage
|
||||
@@ -25,8 +26,27 @@ from .config import (
|
||||
lik2relation,
|
||||
)
|
||||
|
||||
AVA_URL = "http://q1.qlogo.cn/g?b=qq&nk={}&s=160"
|
||||
|
||||
driver: Driver = nonebot.get_driver()
|
||||
|
||||
base_config = Config.get("sign_in")
|
||||
|
||||
|
||||
MORNING_MESSAGE = [
|
||||
"早上好,希望今天是美好的一天!",
|
||||
"醒了吗,今天也要元气满满哦!",
|
||||
"早上好呀,今天也要开心哦!",
|
||||
"早安,愿你拥有美好的一天!",
|
||||
]
|
||||
|
||||
LG_MESSAGE = [
|
||||
"今天要早点休息哦~",
|
||||
"可不要熬夜到太晚呀",
|
||||
"请尽早休息吧!",
|
||||
"不要熬夜啦!",
|
||||
]
|
||||
|
||||
|
||||
@driver.on_startup
|
||||
async def init_image():
|
||||
@@ -73,9 +93,14 @@ async def get_card(
|
||||
if card_file.exists():
|
||||
return card_file
|
||||
is_card_view = True
|
||||
return await _generate_card(
|
||||
user, nickname, add_impression, gold, gift, is_double, is_card_view
|
||||
)
|
||||
if base_config.get("IMAGE_STYLE") == "zhenxun":
|
||||
return await _generate_html_card(
|
||||
user, nickname, add_impression, gold, gift, is_double, is_card_view
|
||||
)
|
||||
else:
|
||||
return await _generate_card(
|
||||
user, nickname, add_impression, gold, gift, is_double, is_card_view
|
||||
)
|
||||
|
||||
|
||||
async def _generate_card(
|
||||
@@ -237,8 +262,8 @@ async def _generate_card(
|
||||
_type = "sign"
|
||||
current_date = datetime.now()
|
||||
current_datetime_str = current_date.strftime("%Y-%m-%d %a %H:%M:%S")
|
||||
data = current_date.date()
|
||||
data_img = await BuildImage.build_text_image(
|
||||
date = current_date.date()
|
||||
date_img = await BuildImage.build_text_image(
|
||||
f"时间:{current_datetime_str}", size=20
|
||||
)
|
||||
await bk.paste(nickname_img, (30, 15))
|
||||
@@ -249,7 +274,7 @@ async def _generate_card(
|
||||
# await bk.paste(sign_day_img, (398, 158))
|
||||
# await bk.text((_x, 167), "days")
|
||||
await bk.paste(tip_image, (10, 167))
|
||||
await bk.paste(data_img, (220, 370))
|
||||
await bk.paste(date_img, (220, 370))
|
||||
await bk.paste(lik_text1_img, (220, 240))
|
||||
await bk.paste(lik_text2_img, (262, 234))
|
||||
await bk.paste(bar_bk, (225, 275))
|
||||
@@ -257,8 +282,8 @@ async def _generate_card(
|
||||
await bk.paste(today_sign_text_img, (550, 180))
|
||||
await bk.paste(today_data, (580, 220))
|
||||
await bk.paste(watermark, (15, 400))
|
||||
await bk.save(SIGN_TODAY_CARD_PATH / f"{user.user_id}_{_type}_{data}.png")
|
||||
return IMAGE_PATH / "sign" / "today_card" / f"{user.user_id}_{_type}_{data}.png"
|
||||
await bk.save(SIGN_TODAY_CARD_PATH / f"{user.user_id}_{_type}_{date}.png")
|
||||
return IMAGE_PATH / "sign" / "today_card" / f"{user.user_id}_{_type}_{date}.png"
|
||||
|
||||
|
||||
async def generate_progress_bar_pic():
|
||||
@@ -333,3 +358,98 @@ def clear_sign_data_pic():
|
||||
for file in os.listdir(SIGN_TODAY_CARD_PATH):
|
||||
if str(date) not in file:
|
||||
os.remove(SIGN_TODAY_CARD_PATH / file)
|
||||
|
||||
|
||||
async def _generate_html_card(
|
||||
user: SignUser,
|
||||
nickname: str,
|
||||
impression: float,
|
||||
gold: int | None,
|
||||
gift: str,
|
||||
is_double: bool = False,
|
||||
is_card_view: bool = False,
|
||||
) -> Path:
|
||||
"""生成签到卡片
|
||||
|
||||
参数:
|
||||
user: SignUser
|
||||
nickname: 用户昵称
|
||||
impression: 新增的好感度
|
||||
gold: 金币
|
||||
gift: 礼物
|
||||
is_double: 是否触发双倍.
|
||||
is_card_view: 是否展示好感度卡片.
|
||||
|
||||
返回:
|
||||
Path: 卡片路径
|
||||
"""
|
||||
user_console = await user.user_console
|
||||
if user_console and user_console.uid:
|
||||
uid = f"{user_console.uid}".rjust(12, "0")
|
||||
uid = uid[:4] + " " + uid[4:8] + " " + uid[8:]
|
||||
else:
|
||||
uid = "XXXX XXXX XXXX"
|
||||
level, next_impression, previous_impression = get_level_and_next_impression(
|
||||
impression
|
||||
)
|
||||
interpolation = next_impression - impression
|
||||
if level == "9":
|
||||
level = "8"
|
||||
interpolation = 0
|
||||
message = f"{NICKNAME}希望你天天开心!"
|
||||
hour = datetime.now().hour
|
||||
if hour > 6 and hour < 10:
|
||||
message = random.choice(MORNING_MESSAGE)
|
||||
elif hour >= 0 and hour < 6:
|
||||
message = random.choice(LG_MESSAGE)
|
||||
_impression = impression
|
||||
if is_double:
|
||||
_impression = f"{impression}(×2)"
|
||||
process = 1 - (next_impression - user.impression) / (
|
||||
next_impression - previous_impression
|
||||
)
|
||||
if next_impression == 0:
|
||||
process = 0
|
||||
now = datetime.now()
|
||||
data = {
|
||||
"ava": AVA_URL.format(user.user_id),
|
||||
"name": nickname,
|
||||
"uid": uid,
|
||||
"sign_count": f"{user.sign_count}",
|
||||
"message": message,
|
||||
"cur_impression": f"{user.impression:.2f}",
|
||||
"impression": f"好感度+{_impression}",
|
||||
"gold": f"金币+{gold}",
|
||||
"gift": gift,
|
||||
"level": f"{level} [{lik2relation[level]}]",
|
||||
"attitude": level2attitude[level],
|
||||
"interpolation": f"{interpolation:.2f}",
|
||||
"heart2": [1 for _ in range(int(level))],
|
||||
"heart1": [1 for _ in range(9 - int(level))],
|
||||
"process": process,
|
||||
"date": str(now.replace(microsecond=0)),
|
||||
}
|
||||
if is_card_view:
|
||||
value_list = (
|
||||
await SignUser.annotate()
|
||||
.order_by("-impression")
|
||||
.values_list("user_id", flat=True)
|
||||
)
|
||||
index = value_list.index(user.user_id) + 1 # type: ignore
|
||||
data["impression"] = f"好感度排名第 {index} 位"
|
||||
data["gold"] = f"总金币:{gold}"
|
||||
data["gift"] = ""
|
||||
pic = await template_to_pic(
|
||||
template_path=str((TEMPLATE_PATH / "sign").absolute()),
|
||||
template_name="main.html",
|
||||
templates={"data": data},
|
||||
pages={
|
||||
"viewport": {"width": 465, "height": 926},
|
||||
"base_url": f"file://{TEMPLATE_PATH}",
|
||||
},
|
||||
wait=2,
|
||||
)
|
||||
image = BuildImage.open(pic)
|
||||
date = now.date()
|
||||
await image.save(SIGN_TODAY_CARD_PATH / f"{user.user_id}_sign_{date}.png")
|
||||
return IMAGE_PATH / "sign" / "today_card" / f"{user.user_id}_sign_{date}.png"
|
||||
|
||||
Reference in New Issue
Block a user