zhenxun_bot/models/ban_user.py

120 lines
3.5 KiB
Python
Raw Normal View History

2021-05-20 18:37:51 +08:00
from services.db_context import db
import time
class BanUser(db.Model):
2021-07-30 21:21:51 +08:00
__tablename__ = "ban_users"
2021-05-20 18:37:51 +08:00
user_qq = db.Column(db.BigInteger(), nullable=False, primary_key=True)
ban_level = db.Column(db.Integer(), nullable=False)
ban_time = db.Column(db.BigInteger())
duration = db.Column(db.BigInteger())
2021-07-30 21:21:51 +08:00
_idx1 = db.Index("ban_group_users_idx1", "user_qq", unique=True)
2021-05-20 18:37:51 +08:00
@classmethod
2021-07-30 21:21:51 +08:00
async def check_ban_level(cls, user_qq: int, level: int) -> bool:
"""
说明
检测ban掉目标的用户与unban用户的权限等级大小
参数
:param user_qq: unban用户的qq号
:param level: ban掉目标用户的权限等级
"""
user = await cls.query.where((cls.user_qq == user_qq)).gino.first()
2021-05-20 18:37:51 +08:00
if not user:
return False
if user.ban_level > level:
return True
return False
@classmethod
2021-07-30 21:21:51 +08:00
async def check_ban_time(cls, user_qq: int) -> str:
"""
说明
检测用户被ban时长
参数
:param user_qq: qq号
"""
query = cls.query.where((cls.user_qq == user_qq))
2021-05-20 18:37:51 +08:00
user = await query.gino.first()
if not user:
2021-07-30 21:21:51 +08:00
return ""
2021-05-20 18:37:51 +08:00
if time.time() - (user.ban_time + user.duration) > 0 and user.duration != -1:
2021-07-30 21:21:51 +08:00
return ""
2021-05-20 18:37:51 +08:00
if user.duration == -1:
2021-07-30 21:21:51 +08:00
return ""
2021-05-20 18:37:51 +08:00
return time.time() - user.ban_time - user.duration
@classmethod
2021-07-30 21:21:51 +08:00
async def is_ban(cls, user_qq: int) -> bool:
"""
说明
判断用户是否被ban
参数
:param user_qq: qq号
"""
2021-05-20 18:37:51 +08:00
if await cls.check_ban_time(user_qq):
return True
else:
2021-06-15 10:57:08 +08:00
await cls.unban(user_qq)
2021-05-20 18:37:51 +08:00
return False
2021-09-05 02:21:38 +08:00
@classmethod
async def is_super_ban(cls, user_qq: int) -> bool:
"""
说明
判断用户是否被ban
参数
:param user_qq: qq号
"""
user = await cls.query.where((cls.user_qq == user_qq)).gino.first()
if not user:
return False
if user.ban_level == 10:
return True
2021-05-20 18:37:51 +08:00
@classmethod
2021-07-30 21:21:51 +08:00
async def ban(cls, user_qq: int, ban_level: int, duration: int) -> bool:
"""
说明
ban掉目标用户
参数
:param user_qq: 目标用户qq号
:param ban_level: 使用ban命令用户的权限
2021-11-23 21:44:59 +08:00
:param duration: ban时长
2021-07-30 21:21:51 +08:00
"""
query = cls.query.where((cls.user_qq == user_qq))
2021-06-15 10:57:08 +08:00
query = query.with_for_update()
2021-05-20 18:37:51 +08:00
user = await query.gino.first()
if not await cls.check_ban_time(user_qq):
await cls.unban(user_qq)
user = None
if user is None:
await cls.create(
user_qq=user_qq,
ban_level=ban_level,
ban_time=time.time(),
duration=duration,
)
return True
else:
return False
@classmethod
2021-07-30 21:21:51 +08:00
async def unban(cls, user_qq: int) -> bool:
"""
说明
unban用户
参数
:param user_qq: qq号
"""
query = cls.query.where((cls.user_qq == user_qq))
2021-06-15 10:57:08 +08:00
query = query.with_for_update()
2021-05-20 18:37:51 +08:00
user = await query.gino.first()
if user is None:
return False
else:
2021-07-30 21:21:51 +08:00
await cls.delete.where((cls.user_qq == user_qq)).gino.status()
2021-05-20 18:37:51 +08:00
return True