1
0
forked from bot/app
LiteyukiBot/liteyuki/plugins/liteyuki_user/profile_manager.py

151 lines
5.1 KiB
Python
Raw Normal View History

2024-03-22 12:41:38 +08:00
from typing import Optional
2024-03-26 12:20:08 +08:00
import pytz
2024-03-24 22:04:51 +08:00
from nonebot import require
2024-03-19 00:27:40 +08:00
2024-05-12 02:47:14 +08:00
from liteyuki.utils.base.data import LiteModel, Database
from liteyuki.utils.base.data_manager import User, user_db, group_db
from liteyuki.utils.base.language import Language, change_user_lang, get_all_lang, get_user_lang
from liteyuki.utils.base.ly_typing import T_Bot, T_MessageEvent
from liteyuki.utils.message.message import MarkdownMessage as md
2024-03-26 12:20:08 +08:00
from .const import representative_timezones_list
2024-05-16 21:28:18 +08:00
from ...utils import satori_utils
2024-03-19 00:27:40 +08:00
2024-03-24 22:04:51 +08:00
require("nonebot_plugin_alconna")
from nonebot_plugin_alconna import Alconna, Args, Arparma, Subcommand, on_alconna
2024-03-22 12:41:38 +08:00
profile_alc = on_alconna(
Alconna(
"profile",
2024-03-22 12:41:38 +08:00
Subcommand(
"set",
2024-03-22 13:39:01 +08:00
Args["key", str]["value", str, None],
2024-03-22 12:41:38 +08:00
alias=["s", "设置"],
),
Subcommand(
"get",
Args["key", str],
alias=["g", "查询"],
),
),
aliases={"用户信息"}
2024-03-22 12:41:38 +08:00
)
2024-03-19 00:27:40 +08:00
2024-03-22 12:41:38 +08:00
# json储存
class Profile(LiteModel):
lang: str = "zh-CN"
nickname: str = ""
timezone: str = "Asia/Shanghai"
location: str = ""
2024-03-19 00:27:40 +08:00
2024-03-22 12:41:38 +08:00
@profile_alc.handle()
async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot):
2024-05-16 21:28:18 +08:00
user: User = user_db.where_one(User(), "user_id = ?", satori_utils.get_user_id(event),
default=User(user_id=str(satori_utils.get_user_id(event))))
ulang = get_user_lang(str(satori_utils.get_user_id(event)))
2024-03-22 12:41:38 +08:00
if result.subcommands.get("set"):
if result.subcommands["set"].args.get("value"):
2024-03-22 13:39:01 +08:00
# 对合法性进行校验后设置
2024-05-16 21:28:18 +08:00
r = set_profile(result.args["key"], result.args["value"], str(satori_utils.get_user_id(event)))
2024-03-22 13:39:01 +08:00
if r:
user.profile[result.args["key"]] = result.args["value"]
2024-04-27 02:20:44 +08:00
user_db.save(user) # 数据库保存
2024-03-22 13:39:01 +08:00
await profile_alc.finish(
ulang.get(
"user.profile.set_success",
ATTR=ulang.get(f"user.profile.{result.args['key']}"),
VALUE=result.args["value"]
)
)
else:
await profile_alc.finish(ulang.get("user.profile.set_failed", ATTR=ulang.get(f"user.profile.{result.args['key']}")))
2024-03-22 12:41:38 +08:00
else:
2024-03-22 13:39:01 +08:00
# 未输入值,尝试呼出菜单
menu = get_profile_menu(result.args["key"], ulang)
if menu:
2024-03-31 06:22:53 +08:00
await md.send_md(menu, bot, event=event)
2024-03-22 13:39:01 +08:00
else:
await profile_alc.finish(ulang.get("user.profile.input_value", ATTR=ulang.get(f"user.profile.{result.args['key']}")))
2024-03-22 12:41:38 +08:00
user.profile[result.args["key"]] = result.args["value"]
elif result.subcommands.get("get"):
if result.args["key"] in user.profile:
await profile_alc.finish(user.profile[result.args["key"]])
else:
await profile_alc.finish("无此键值")
2024-03-19 00:27:40 +08:00
else:
2024-03-22 12:41:38 +08:00
profile = Profile(**user.profile)
2024-03-22 13:39:01 +08:00
for k, v in user.profile.items():
2024-03-22 12:41:38 +08:00
profile.__setattr__(k, v)
2024-03-22 13:39:01 +08:00
reply = f"# {ulang.get('user.profile.info')}\n***\n"
2024-03-22 12:41:38 +08:00
hidden_attr = ["id", "TABLE_NAME"]
2024-03-22 12:41:38 +08:00
enter_attr = ["lang", "timezone"]
for key in sorted(profile.dict().keys()):
if key in hidden_attr:
continue
val = profile.dict()[key]
key_text = ulang.get(f"user.profile.{key}")
btn_set = md.btn_cmd(ulang.get("user.profile.edit"), f"profile set {key}",
enter=True if key in enter_attr else False)
2024-03-22 12:41:38 +08:00
reply += (f"\n**{key_text}** **{val}**\n"
2024-03-26 12:20:08 +08:00
f"\n> {ulang.get(f'user.profile.{key}.desc')}"
f"\n> {btn_set} \n\n***\n")
2024-03-31 06:22:53 +08:00
await md.send_md(reply, bot, event=event)
2024-03-22 12:41:38 +08:00
2024-03-22 13:39:01 +08:00
def get_profile_menu(key: str, ulang: Language) -> Optional[str]:
2024-03-22 12:41:38 +08:00
"""获取属性的markdown菜单
Args:
2024-03-22 13:39:01 +08:00
ulang: 用户语言
key: 属性键
2024-03-22 12:41:38 +08:00
Returns:
"""
2024-03-22 13:39:01 +08:00
setting_name = ulang.get(f"user.profile.{key}")
no_menu = ["id", "nickname", "location"]
if key in no_menu:
return None
2024-03-26 08:35:41 +08:00
reply = f"**{setting_name} {ulang.get('user.profile.settings')}**\n***\n"
2024-03-22 13:39:01 +08:00
if key == "lang":
for lang_code, lang_name in get_all_lang().items():
2024-04-23 07:45:38 +08:00
btn_set_lang = md.btn_cmd(f"{lang_name}({lang_code})", f"profile set {key} {lang_code}")
2024-04-23 07:42:56 +08:00
reply += f"\n{btn_set_lang}\n***\n"
2024-03-26 12:20:08 +08:00
elif key == "timezone":
for tz in representative_timezones_list:
btn_set_tz = md.btn_cmd(tz, f"profile set {key} {tz}")
2024-04-23 07:42:56 +08:00
reply += f"{btn_set_tz}\n***\n"
2024-03-22 13:39:01 +08:00
return reply
2024-03-22 12:41:38 +08:00
def set_profile(key: str, value: str, user_id: str) -> bool:
2024-03-22 12:41:38 +08:00
"""设置属性使用if分支对每一个合法性进行检查
Args:
user_id:
2024-03-22 12:41:38 +08:00
key:
value:
Returns:
是否成功设置输入合法性不通过返回False
"""
2024-03-26 17:14:41 +08:00
if key == "lang":
2024-03-22 13:39:01 +08:00
if value in get_all_lang():
change_user_lang(user_id, value)
2024-03-22 13:39:01 +08:00
return True
2024-03-26 17:14:41 +08:00
elif key == "timezone":
2024-03-26 12:20:08 +08:00
if value in pytz.all_timezones:
return True
2024-03-27 08:16:53 +08:00
elif key == "nickname":
return True