Merge remote-tracking branch 'pr/main'
This commit is contained in:
commit
72baa5d095
84
command.py
84
command.py
@ -34,17 +34,89 @@ diuse_register = on_alconna(
|
|||||||
)
|
)
|
||||||
|
|
||||||
@diuse_register.handle()
|
@diuse_register.handle()
|
||||||
async def _(session: Uninfo):
|
async def handle_register(session: Uninfo):
|
||||||
uid = str(session.user.id)
|
uid = str(session.user.id)
|
||||||
|
|
||||||
user = await g_pSqlManager.getUserInfoByUid(uid)
|
user = await g_pSqlManager.getUserInfoByUid(uid)
|
||||||
|
|
||||||
if user:
|
if user:
|
||||||
await MessageUtils.build_message("你已经有啦").send(reply_to=True)
|
await MessageUtils.build_message("🎉 您已经开通农场啦~").send(reply_to=True)
|
||||||
else:
|
return
|
||||||
aaa = await g_pSqlManager.initUserInfoByUid(uid, str(session.user.name), 0, 100)
|
|
||||||
|
|
||||||
await MessageUtils.build_message(str(aaa)).send(reply_to=True)
|
try:
|
||||||
|
# 获取原始用户名并安全处理
|
||||||
|
raw_name = str(session.user.name)
|
||||||
|
safe_name = sanitize_username(raw_name)
|
||||||
|
|
||||||
|
# 初始化用户信息
|
||||||
|
success = await g_pSqlManager.initUserInfoByUid(
|
||||||
|
uid=uid,
|
||||||
|
name=safe_name,
|
||||||
|
exp=0,
|
||||||
|
point=100
|
||||||
|
)
|
||||||
|
|
||||||
|
msg = (
|
||||||
|
"✅ 农场开通成功!\n💼 初始资金:100农场币"
|
||||||
|
if success
|
||||||
|
else "⚠️ 开通失败,请稍后再试"
|
||||||
|
)
|
||||||
|
logger.info(f"用户注册 {'成功' if success else '失败'}:{uid}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
msg = "⚠️ 系统繁忙,请稍后再试"
|
||||||
|
logger.error(f"注册异常 | UID:{uid} | 错误:{str(e)}")
|
||||||
|
|
||||||
|
await MessageUtils.build_message(msg).send(reply_to=True)
|
||||||
|
|
||||||
|
def sanitize_username(username: str, max_length: int = 15) -> str:
|
||||||
|
"""
|
||||||
|
安全处理用户名
|
||||||
|
功能:
|
||||||
|
1. 移除首尾空白
|
||||||
|
2. 过滤危险字符
|
||||||
|
3. 转义单引号
|
||||||
|
4. 处理空值
|
||||||
|
5. 限制长度
|
||||||
|
"""
|
||||||
|
# 处理空值
|
||||||
|
if not username:
|
||||||
|
return "神秘农夫"
|
||||||
|
|
||||||
|
# 基础清洗
|
||||||
|
cleaned = username.strip()
|
||||||
|
|
||||||
|
# 允许的字符白名单(可自定义扩展)
|
||||||
|
safe_chars = {
|
||||||
|
'_', '-', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
|
||||||
|
'+', '=', '.', ',', '~', '·', ' ',
|
||||||
|
'a','b','c','d','e','f','g','h','i','j','k','l','m',
|
||||||
|
'n','o','p','q','r','s','t','u','v','w','x','y','z',
|
||||||
|
'A','B','C','D','E','F','G','H','I','J','K','L','M',
|
||||||
|
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
|
||||||
|
'0','1','2','3','4','5','6','7','8','9',
|
||||||
|
}
|
||||||
|
# 添加常用中文字符(Unicode范围)
|
||||||
|
safe_chars.update(chr(c) for c in range(0x4E00, 0x9FFF+1))
|
||||||
|
|
||||||
|
# 过滤危险字符
|
||||||
|
filtered = [
|
||||||
|
c if c in safe_chars or 0x4E00 <= ord(c) <= 0x9FFF
|
||||||
|
else ''
|
||||||
|
for c in cleaned
|
||||||
|
]
|
||||||
|
|
||||||
|
# 合并处理结果
|
||||||
|
safe_str = ''.join(filtered)
|
||||||
|
|
||||||
|
# 转义单引号(双重保障)
|
||||||
|
escaped = safe_str.replace("'", "''")
|
||||||
|
|
||||||
|
# 处理空结果
|
||||||
|
if not escaped:
|
||||||
|
return "神秘农夫"
|
||||||
|
|
||||||
|
# 长度限制
|
||||||
|
return escaped[:max_length]
|
||||||
|
|
||||||
diuse_farm = on_alconna(
|
diuse_farm = on_alconna(
|
||||||
Alconna(
|
Alconna(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user