mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-10 06:10:01 +08:00
版本更新
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
import time
|
||||
|
||||
from tortoise import fields
|
||||
from typing_extensions import Self
|
||||
|
||||
from zhenxun.services.db_context import Model
|
||||
from tortoise import fields
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
from zhenxun.services.db_context import Model
|
||||
from zhenxun.utils.exception import UserAndGroupIsNone
|
||||
|
||||
|
||||
class BanConsole(Model):
|
||||
|
||||
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
||||
"""自增id"""
|
||||
user_id = fields.CharField(255, null=True)
|
||||
@@ -25,9 +24,9 @@ class BanConsole(Model):
|
||||
operator = fields.CharField(255)
|
||||
"""使用Ban命令的用户"""
|
||||
|
||||
class Meta:
|
||||
class Meta: # type: ignore
|
||||
table = "ban_console"
|
||||
table_description = ".ban/b了 封禁人员/群组数据表"
|
||||
table_description = "封禁人员/群组数据表"
|
||||
|
||||
@classmethod
|
||||
async def _get_data(cls, user_id: str | None, group_id: str | None) -> Self | None:
|
||||
@@ -45,16 +44,14 @@ class BanConsole(Model):
|
||||
"""
|
||||
if not user_id and not group_id:
|
||||
raise UserAndGroupIsNone()
|
||||
user = None
|
||||
if user_id:
|
||||
if group_id:
|
||||
user = await cls.get_or_none(user_id=user_id, group_id=group_id)
|
||||
else:
|
||||
user = await cls.get_or_none(user_id=user_id, group_id__isnull=True)
|
||||
return (
|
||||
await cls.get_or_none(user_id=user_id, group_id=group_id)
|
||||
if group_id
|
||||
else await cls.get_or_none(user_id=user_id, group_id__isnull=True)
|
||||
)
|
||||
else:
|
||||
if group_id:
|
||||
user = await cls.get_or_none(user_id__isnull=True, group_id=group_id)
|
||||
return user
|
||||
return await cls.get_or_none(user_id="", group_id=group_id)
|
||||
|
||||
@classmethod
|
||||
async def check_ban_level(
|
||||
@@ -91,7 +88,7 @@ class BanConsole(Model):
|
||||
返回:
|
||||
int: ban剩余时长,-1时为永久ban,0表示未被ban
|
||||
"""
|
||||
logger.debug(f"获取用户ban时长", target=f"{group_id}:{user_id}")
|
||||
logger.debug("获取用户ban时长", target=f"{group_id}:{user_id}")
|
||||
user = await cls._get_data(user_id, group_id)
|
||||
if not user and user_id:
|
||||
user = await cls._get_data(user_id, None)
|
||||
@@ -99,9 +96,7 @@ class BanConsole(Model):
|
||||
if user.duration == -1:
|
||||
return -1
|
||||
_time = time.time() - (user.ban_time + user.duration)
|
||||
if _time > 0:
|
||||
return 0
|
||||
return int(time.time() - user.ban_time - user.duration)
|
||||
return 0 if _time > 0 else int(time.time() - user.ban_time - user.duration)
|
||||
return 0
|
||||
|
||||
@classmethod
|
||||
@@ -114,7 +109,7 @@ class BanConsole(Model):
|
||||
返回:
|
||||
bool: 是否被ban
|
||||
"""
|
||||
logger.debug(f"检测是否被ban", target=f"{group_id}:{user_id}")
|
||||
logger.debug("检测是否被ban", target=f"{group_id}:{user_id}")
|
||||
if await cls.check_ban_time(user_id, group_id):
|
||||
return True
|
||||
else:
|
||||
@@ -143,8 +138,8 @@ class BanConsole(Model):
|
||||
f"封禁用户/群组,等级:{ban_level},时长: {duration}",
|
||||
target=f"{group_id}:{user_id}",
|
||||
)
|
||||
user = await cls._get_data(user_id, group_id)
|
||||
if user:
|
||||
target = await cls._get_data(user_id, group_id)
|
||||
if target:
|
||||
await cls.unban(user_id, group_id)
|
||||
await cls.create(
|
||||
user_id=user_id,
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
from tortoise import fields
|
||||
|
||||
from zhenxun.services.db_context import Model
|
||||
|
||||
|
||||
class BotConnectLog(Model):
|
||||
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
||||
"""自增id"""
|
||||
bot_id = fields.CharField(255, description="Bot id")
|
||||
"""Bot id"""
|
||||
platform = fields.CharField(255, null=True, description="平台")
|
||||
"""平台"""
|
||||
connect_time = fields.DatetimeField(description="连接时间")
|
||||
"""日期"""
|
||||
type = fields.IntField(null=True, description="1: 连接, 0: 断开")
|
||||
"""1: 连接, 0: 断开"""
|
||||
create_time = fields.DatetimeField(auto_now_add=True)
|
||||
"""创建时间"""
|
||||
|
||||
class Meta: # type: ignore
|
||||
table = "bot_connect_log"
|
||||
table_description = "bot连接表"
|
||||
+170
-12
@@ -9,7 +9,6 @@ from zhenxun.services.db_context import Model
|
||||
|
||||
|
||||
class GroupConsole(Model):
|
||||
|
||||
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
||||
"""自增id"""
|
||||
group_id = fields.CharField(255, description="群组id")
|
||||
@@ -34,8 +33,14 @@ class GroupConsole(Model):
|
||||
"""群认证标记"""
|
||||
block_plugin = fields.TextField(default="", description="禁用插件")
|
||||
"""禁用插件"""
|
||||
superuser_block_plugin = fields.TextField(
|
||||
default="", description="超级用户禁用插件"
|
||||
)
|
||||
"""超级用户禁用插件"""
|
||||
block_task = fields.TextField(default="", description="禁用插件")
|
||||
"""禁用插件"""
|
||||
superuser_block_task = fields.TextField(default="", description="超级用户禁用被动")
|
||||
"""超级用户禁用被动"""
|
||||
platform = fields.CharField(255, default="qq", description="所属平台")
|
||||
"""所属平台"""
|
||||
|
||||
@@ -53,6 +58,7 @@ class GroupConsole(Model):
|
||||
if modules := await TaskInfo.filter(default_status=False).values_list(
|
||||
"module", flat=True
|
||||
):
|
||||
modules = [f"<{module}" for module in modules]
|
||||
group.block_task = ",".join(modules) + "," # type: ignore
|
||||
await group.save(using_db=using_db, update_fields=["block_task"])
|
||||
return group
|
||||
@@ -73,6 +79,7 @@ class GroupConsole(Model):
|
||||
"module", flat=True
|
||||
)
|
||||
):
|
||||
modules = [f"<{module}" for module in modules]
|
||||
group.block_task = ",".join(modules) + "," # type: ignore
|
||||
await group.save(using_db=using_db, update_fields=["block_task"])
|
||||
return group, is_create
|
||||
@@ -93,6 +100,7 @@ class GroupConsole(Model):
|
||||
"module", flat=True
|
||||
)
|
||||
):
|
||||
modules = [f"<{module}" for module in modules]
|
||||
group.block_task = ",".join(modules) + "," # type: ignore
|
||||
await group.save(using_db=using_db, update_fields=["block_task"])
|
||||
return group, is_create
|
||||
@@ -127,27 +135,94 @@ class GroupConsole(Model):
|
||||
return group.is_super if (group := await cls.get_group(group_id)) else False
|
||||
|
||||
@classmethod
|
||||
async def is_super_block_plugin(
|
||||
cls, group_id: str, module: str, channel_id: str | None = None
|
||||
) -> bool:
|
||||
async def is_superuser_block_plugin(cls, group_id: str, module: str) -> bool:
|
||||
"""查看群组是否超级用户禁用功能
|
||||
|
||||
参数:
|
||||
group_id: 群组id
|
||||
module: 模块名称
|
||||
channel_id: 频道id
|
||||
|
||||
返回:
|
||||
bool: 是否禁用被动
|
||||
"""
|
||||
return await cls.exists(
|
||||
group_id=group_id,
|
||||
channel_id=channel_id,
|
||||
block_plugin__contains=f"super:{module},",
|
||||
superuser_block_plugin__contains=f"<{module},",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def is_block_plugin(
|
||||
async def is_block_plugin(cls, group_id: str, module: str) -> bool:
|
||||
"""查看群组是否禁用插件
|
||||
|
||||
参数:
|
||||
group_id: 群组id
|
||||
plugin: 插件名称
|
||||
|
||||
返回:
|
||||
bool: 是否禁用插件
|
||||
"""
|
||||
return await cls.exists(
|
||||
group_id=group_id, block_plugin__contains=f"<{module},"
|
||||
) or await cls.exists(
|
||||
group_id=group_id, superuser_block_plugin__contains=f"<{module},"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def set_block_plugin(
|
||||
cls,
|
||||
group_id: str,
|
||||
module: str,
|
||||
is_superuser: bool = False,
|
||||
platform: str | None = None,
|
||||
):
|
||||
"""禁用群组插件
|
||||
|
||||
参数:
|
||||
group_id: 群组id
|
||||
task: 任务模块
|
||||
is_superuser: 是否为超级用户
|
||||
platform: 平台
|
||||
"""
|
||||
group, _ = await cls.get_or_create(
|
||||
group_id=group_id, defaults={"platform": platform}
|
||||
)
|
||||
if is_superuser:
|
||||
if f"<{module}," not in group.superuser_block_plugin:
|
||||
group.superuser_block_plugin += f"<{module},"
|
||||
elif f"<{module}," not in group.block_plugin:
|
||||
group.block_plugin += f"<{module},"
|
||||
await group.save(update_fields=["block_plugin", "superuser_block_plugin"])
|
||||
|
||||
@classmethod
|
||||
async def set_unblock_plugin(
|
||||
cls,
|
||||
group_id: str,
|
||||
module: str,
|
||||
is_superuser: bool = False,
|
||||
platform: str | None = None,
|
||||
):
|
||||
"""禁用群组插件
|
||||
|
||||
参数:
|
||||
group_id: 群组id
|
||||
task: 任务模块
|
||||
is_superuser: 是否为超级用户
|
||||
platform: 平台
|
||||
"""
|
||||
group, _ = await cls.get_or_create(
|
||||
group_id=group_id, defaults={"platform": platform}
|
||||
)
|
||||
if is_superuser:
|
||||
if f"<{module}," in group.superuser_block_plugin:
|
||||
group.superuser_block_plugin = group.superuser_block_plugin.replace(
|
||||
f"<{module},", ""
|
||||
)
|
||||
elif f"<{module}," in group.block_plugin:
|
||||
group.block_plugin = group.block_plugin.replace(f"<{module},", "")
|
||||
await group.save(update_fields=["block_plugin", "superuser_block_plugin"])
|
||||
|
||||
@classmethod
|
||||
async def is_normal_block_plugin(
|
||||
cls, group_id: str, module: str, channel_id: str | None = None
|
||||
) -> bool:
|
||||
"""查看群组是否禁用功能
|
||||
@@ -163,7 +238,23 @@ class GroupConsole(Model):
|
||||
return await cls.exists(
|
||||
group_id=group_id,
|
||||
channel_id=channel_id,
|
||||
block_plugin__contains=f"{module},",
|
||||
block_plugin__contains=f"<{module},",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def is_superuser_block_task(cls, group_id: str, task: str) -> bool:
|
||||
"""查看群组是否超级用户禁用被动
|
||||
|
||||
参数:
|
||||
group_id: 群组id
|
||||
task: 模块名称
|
||||
|
||||
返回:
|
||||
bool: 是否禁用被动
|
||||
"""
|
||||
return await cls.exists(
|
||||
group_id=group_id,
|
||||
superuser_block_task__contains=f"<{task},",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
@@ -184,12 +275,79 @@ class GroupConsole(Model):
|
||||
return await cls.exists(
|
||||
group_id=group_id,
|
||||
channel_id__isnull=True,
|
||||
block_task__contains=f"{task},",
|
||||
block_task__contains=f"<{task},",
|
||||
) or await cls.exists(
|
||||
group_id=group_id,
|
||||
channel_id__isnull=True,
|
||||
superuser_block_task__contains=f"<{task},",
|
||||
)
|
||||
return await cls.exists(
|
||||
group_id=group_id, channel_id=channel_id, block_task__contains=f"{task},"
|
||||
group_id=group_id, channel_id=channel_id, block_task__contains=f"<{task},"
|
||||
) or await cls.exists(
|
||||
group_id=group_id,
|
||||
channel_id__isnull=True,
|
||||
superuser_block_task__contains=f"<{task},",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def set_block_task(
|
||||
cls,
|
||||
group_id: str,
|
||||
task: str,
|
||||
is_superuser: bool = False,
|
||||
platform: str | None = None,
|
||||
):
|
||||
"""禁用群组插件
|
||||
|
||||
参数:
|
||||
group_id: 群组id
|
||||
task: 任务模块
|
||||
is_superuser: 是否为超级用户
|
||||
platform: 平台
|
||||
"""
|
||||
group, _ = await cls.get_or_create(
|
||||
group_id=group_id, defaults={"platform": platform}
|
||||
)
|
||||
if is_superuser:
|
||||
if f"<{task}," not in group.superuser_block_task:
|
||||
group.superuser_block_task += f"<{task},"
|
||||
elif f"<{task}," not in group.block_task:
|
||||
group.block_task += f"<{task},"
|
||||
await group.save(update_fields=["block_task", "superuser_block_task"])
|
||||
|
||||
@classmethod
|
||||
async def set_unblock_task(
|
||||
cls,
|
||||
group_id: str,
|
||||
task: str,
|
||||
is_superuser: bool = False,
|
||||
platform: str | None = None,
|
||||
):
|
||||
"""禁用群组插件
|
||||
|
||||
参数:
|
||||
group_id: 群组id
|
||||
task: 任务模块
|
||||
is_superuser: 是否为超级用户
|
||||
platform: 平台
|
||||
"""
|
||||
group, _ = await cls.get_or_create(
|
||||
group_id=group_id, defaults={"platform": platform}
|
||||
)
|
||||
if is_superuser:
|
||||
if f"<{task}," in group.superuser_block_task:
|
||||
group.superuser_block_task = group.superuser_block_task.replace(
|
||||
f"<{task},", ""
|
||||
)
|
||||
elif f"<{task}," in group.block_task:
|
||||
group.block_task = group.block_task.replace(f"<{task},", "")
|
||||
await group.save(update_fields=["block_task", "superuser_block_task"])
|
||||
|
||||
@classmethod
|
||||
def _run_script(cls):
|
||||
return []
|
||||
return [
|
||||
"ALTER TABLE group_console ADD superuser_block_plugin"
|
||||
" character varying(255) NOT NULL DEFAULT '';",
|
||||
"ALTER TABLE group_console ADD superuser_block_task"
|
||||
" character varying(255) NOT NULL DEFAULT '';",
|
||||
]
|
||||
|
||||
@@ -23,7 +23,7 @@ class GroupInfo(Model):
|
||||
platform = fields.CharField(255, default="qq", description="所属平台")
|
||||
"""所属平台"""
|
||||
|
||||
class Meta:
|
||||
class Meta: # type: ignore
|
||||
table = "group_info"
|
||||
table_description = "群聊信息表"
|
||||
|
||||
@@ -40,12 +40,81 @@ class GroupInfo(Model):
|
||||
"""
|
||||
return await cls.exists(group_id=group_id, block_task__contains=f"{task},")
|
||||
|
||||
@classmethod
|
||||
async def is_block_plugin(cls, group_id: str, module: str) -> bool:
|
||||
"""查看群组是否禁用插件
|
||||
|
||||
参数:
|
||||
group_id: 群组id
|
||||
plugin: 插件名称
|
||||
|
||||
返回:
|
||||
bool: 是否禁用插件
|
||||
"""
|
||||
return await cls.exists(
|
||||
group_id=group_id, block_plugin__contains=f"{module},"
|
||||
) or await cls.exists(
|
||||
group_id=group_id, superuser_block_plugin__contains=f"{module},"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def set_block_plugin(
|
||||
cls,
|
||||
group_id: str,
|
||||
module: str,
|
||||
is_superuser: bool = False,
|
||||
platform: str | None = None,
|
||||
):
|
||||
"""禁用群组插件
|
||||
|
||||
参数:
|
||||
group_id: 群组id
|
||||
task: 任务模块
|
||||
"""
|
||||
group, _ = await cls.get_or_create(
|
||||
group_id=group_id, defaults={"platform": platform}
|
||||
)
|
||||
if is_superuser:
|
||||
if "module," not in group.superuser_block_plugin:
|
||||
group.superuser_block_plugin += f"{module},"
|
||||
elif "module," not in group.block_plugin:
|
||||
group.block_plugin += f"{module},"
|
||||
await group.save(update_fields=["block_plugin", "superuser_block_plugin"])
|
||||
|
||||
@classmethod
|
||||
async def set_unblock_plugin(
|
||||
cls,
|
||||
group_id: str,
|
||||
module: str,
|
||||
is_superuser: bool = False,
|
||||
platform: str | None = None,
|
||||
):
|
||||
"""禁用群组插件
|
||||
|
||||
参数:
|
||||
group_id: 群组id
|
||||
task: 任务模块
|
||||
"""
|
||||
group, _ = await cls.get_or_create(
|
||||
group_id=group_id, defaults={"platform": platform}
|
||||
)
|
||||
if is_superuser:
|
||||
if "module," in group.superuser_block_plugin:
|
||||
group.superuser_block_plugin = group.superuser_block_plugin.replace(
|
||||
f"{module},", ""
|
||||
)
|
||||
elif "module," in group.block_plugin:
|
||||
group.block_plugin = group.block_plugin.replace(f"{module},", "")
|
||||
await group.save(update_fields=["block_plugin", "superuser_block_plugin"])
|
||||
|
||||
@classmethod
|
||||
def _run_script(cls):
|
||||
return [
|
||||
"ALTER TABLE group_info ADD group_flag Integer NOT NULL DEFAULT 0;", # group_info表添加一个group_flag
|
||||
"ALTER TABLE group_info ADD group_flag Integer NOT NULL DEFAULT 0;",
|
||||
# group_info表添加一个group_flag
|
||||
"ALTER TABLE group_info ALTER COLUMN group_id TYPE character varying(255);",
|
||||
"ALTER TABLE group_info ADD block_plugin Text NOT NULL DEFAULT '';",
|
||||
"ALTER TABLE group_info ADD block_task Text NOT NULL DEFAULT '';",
|
||||
"ALTER TABLE group_info ADD platform character varying(255) NOT NULL DEFAULT 'qq';",
|
||||
"ALTER TABLE group_info ADD platform character varying(255) NOT NULL"
|
||||
" DEFAULT 'qq';",
|
||||
]
|
||||
|
||||
@@ -4,7 +4,6 @@ from zhenxun.services.db_context import Model
|
||||
|
||||
|
||||
class Statistics(Model):
|
||||
|
||||
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
||||
"""自增id"""
|
||||
user_id = fields.CharField(255)
|
||||
@@ -15,15 +14,19 @@ class Statistics(Model):
|
||||
"""插件名称"""
|
||||
create_time = fields.DatetimeField(auto_now=True)
|
||||
"""添加日期"""
|
||||
bot_id = fields.CharField(255, null=True)
|
||||
"""Bot Id"""
|
||||
|
||||
class Meta:
|
||||
class Meta: # type: ignore
|
||||
table = "statistics"
|
||||
table_description = "插件调用统计数据库"
|
||||
|
||||
@classmethod
|
||||
async def _run_script(cls):
|
||||
return [
|
||||
"ALTER TABLE statistics RENAME COLUMN user_qq TO user_id;", # 将user_qq改为user_id
|
||||
"ALTER TABLE statistics RENAME COLUMN user_qq TO user_id;",
|
||||
# 将user_qq改为user_id
|
||||
"ALTER TABLE statistics ALTER COLUMN user_id TYPE character varying(255);",
|
||||
"ALTER TABLE statistics ALTER COLUMN group_id TYPE character varying(255);",
|
||||
"ALTER TABLE statistics ADD bot_id Text DEFAULT '';",
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user