Files
zhenxun_bot/models/friend_user.py
T

66 lines
2.0 KiB
Python
Raw Normal View History

from tortoise import fields
2023-04-22 21:14:51 +08:00
from typing import Union
2021-12-01 14:03:34 +08:00
from configs.config import Config
from services.db_context import Model
2021-05-20 18:37:51 +08:00
class FriendUser(Model):
2021-05-20 18:37:51 +08:00
id = fields.IntField(pk=True, generated=True, auto_increment=True)
"""自增id"""
user_id = fields.CharField(255, unique=True)
"""用户id"""
user_name = fields.CharField(max_length=255, default="")
"""用户名称"""
nickname = fields.CharField(max_length=255, null=True)
"""私聊下自定义昵称"""
2021-05-20 18:37:51 +08:00
class Meta:
table = "friend_users"
table_description = "好友信息数据表"
2021-05-20 18:37:51 +08:00
@classmethod
2023-04-22 21:14:51 +08:00
async def get_user_name(cls, user_id: Union[int, str]) -> str:
2021-07-30 21:21:51 +08:00
"""
2022-07-23 14:09:17 +08:00
说明:
2021-07-30 21:21:51 +08:00
获取好友用户名称
2022-07-23 14:09:17 +08:00
参数:
:param user_id: 用户id
2021-07-30 21:21:51 +08:00
"""
if user := await cls.get_or_none(user_id=str(user_id)):
2021-05-20 18:37:51 +08:00
return user.user_name
return ""
2021-05-20 18:37:51 +08:00
@classmethod
2023-04-22 21:14:51 +08:00
async def get_user_nickname(cls, user_id: Union[int, str]) -> str:
2021-07-30 21:21:51 +08:00
"""
2022-07-23 14:09:17 +08:00
说明:
2021-07-30 21:21:51 +08:00
获取用户昵称
2022-07-23 14:09:17 +08:00
参数:
:param user_id: 用户id
2021-07-30 21:21:51 +08:00
"""
if user := await cls.get_or_none(user_id=str(user_id)):
2021-05-20 18:37:51 +08:00
if user.nickname:
2021-12-01 14:03:34 +08:00
_tmp = ""
if black_word := Config.get_config("nickname", "BLACK_WORD"):
2021-12-16 11:16:28 +08:00
for x in user.nickname:
_tmp += "*" if x in black_word else x
2021-12-01 14:03:34 +08:00
return _tmp
2021-07-30 21:21:51 +08:00
return ""
2021-05-20 18:37:51 +08:00
@classmethod
2023-04-22 21:14:51 +08:00
async def set_user_nickname(cls, user_id: Union[int, str], nickname: str):
2021-07-30 21:21:51 +08:00
"""
2022-07-23 14:09:17 +08:00
说明:
2021-07-30 21:21:51 +08:00
设置用户昵称
2022-07-23 14:09:17 +08:00
参数:
:param user_id: 用户id
2021-07-30 21:21:51 +08:00
:param nickname: 昵称
"""
await cls.update_or_create(user_id=str(user_id), defaults={"nickname": nickname})
@classmethod
async def _run_script(cls):
await cls.raw("ALTER TABLE friend_users ALTER COLUMN user_id TYPE character varying(255);")
# 将user_id字段类型改为character varying(255))