mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-08 05:10:01 +08:00
数据库中所有user_qq改名以及user_id和group_id改为字符串
This commit is contained in:
@@ -96,7 +96,7 @@ Config.add_plugin_config(
|
||||
"BAN_3_DURATION",
|
||||
7,
|
||||
help_="Union[int, List[int, int]]Ban时长(天),三级惩罚,可以为指定数字或指定列表区间(随机),例如 [7, 30]",
|
||||
default_value=360,
|
||||
default_value=7,
|
||||
type=int,
|
||||
)
|
||||
|
||||
@@ -171,8 +171,8 @@ async def _(
|
||||
and event.is_tome()
|
||||
and not msg.startswith("原神绑定")
|
||||
):
|
||||
# if str(event.user_id) not in bot.config.superusers:
|
||||
# return logger.debug(f"超级用户跳过黑名单词汇检查 Message: {msg}", target=event.user_id)
|
||||
if str(event.user_id) in bot.config.superusers:
|
||||
return logger.debug(f"超级用户跳过黑名单词汇检查 Message: {msg}", target=event.user_id)
|
||||
if (
|
||||
event.is_tome()
|
||||
and matcher.plugin_name == "black_word"
|
||||
@@ -184,8 +184,8 @@ async def _(
|
||||
and group_manager.get_group_level(event.group_id) < 0
|
||||
):
|
||||
return
|
||||
user_id = event.user_id
|
||||
group_id = event.group_id if isinstance(event, GroupMessageEvent) else None
|
||||
user_id = str(event.user_id)
|
||||
group_id = str(event.group_id) if isinstance(event, GroupMessageEvent) else None
|
||||
msg = get_message_text(event.json())
|
||||
if await black_word_manager.check(
|
||||
user_id, group_id, msg
|
||||
@@ -206,8 +206,8 @@ async def _(bot: Bot, reg_group: Tuple[Any, ...] = RegexGroup()):
|
||||
await show_black.finish("日期格式错误,需要:年-月-日")
|
||||
pic = await show_black_text_image(
|
||||
bot,
|
||||
int(user_id.split(":")[1]) if user_id else None,
|
||||
int(group_id.split(":")[1]) if group_id else None,
|
||||
user_id.split(":")[1] if user_id else None,
|
||||
group_id.split(":")[1] if group_id else None,
|
||||
date,
|
||||
date_type,
|
||||
)
|
||||
@@ -266,11 +266,13 @@ async def _(event: MessageEvent, arg: Message = CommandArg()):
|
||||
or not is_number(msg[2])
|
||||
):
|
||||
await set_punish.finish("参数错误,请查看帮助...", at_sender=True)
|
||||
uid = int(msg[0])
|
||||
uid = msg[0]
|
||||
id_ = int(msg[1])
|
||||
punish_level = int(msg[2])
|
||||
rst = await set_user_punish(uid, id_, punish_level)
|
||||
await set_punish.send(rst)
|
||||
logger.info(
|
||||
f"USER {event.user_id} 设置惩罚 uid:{uid} id_:{id_} punish_level:{punish_level} --> {rst}"
|
||||
f"设置惩罚 uid:{uid} id_:{id_} punish_level:{punish_level} --> {rst}",
|
||||
"设置惩罚",
|
||||
event.user_id,
|
||||
)
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
from nonebot.adapters.onebot.v11 import Bot
|
||||
from utils.image_utils import BuildImage, text2image
|
||||
from services.log import logger
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from nonebot.adapters.onebot.v11 import Bot
|
||||
|
||||
from services.log import logger
|
||||
from utils.image_utils import BuildImage, text2image
|
||||
|
||||
from .model import BlackWord
|
||||
from .utils import _get_punish, Config
|
||||
from .utils import Config, _get_punish
|
||||
|
||||
|
||||
async def show_black_text_image(
|
||||
bot: Bot,
|
||||
user: Optional[int],
|
||||
group_id: Optional[int],
|
||||
user_id: Optional[str],
|
||||
group_id: Optional[str],
|
||||
date: Optional[datetime],
|
||||
data_type: str = "=",
|
||||
) -> BuildImage:
|
||||
@@ -23,7 +26,7 @@ async def show_black_text_image(
|
||||
:param data_type: 日期搜索类型
|
||||
:return:
|
||||
"""
|
||||
data = await BlackWord.get_black_data(user, group_id, date, data_type)
|
||||
data = await BlackWord.get_black_data(user_id, group_id, date, data_type)
|
||||
A = BuildImage(0, 0, color="#f9f6f2", font_size=20)
|
||||
image_list = []
|
||||
friend_str = await bot.get_friend_list()
|
||||
@@ -41,21 +44,21 @@ async def show_black_text_image(
|
||||
if x.group_id:
|
||||
user_name = (
|
||||
await bot.get_group_member_info(
|
||||
group_id=x.group_id, user_id=x.user_qq
|
||||
group_id=int(x.group_id), user_id=int(x.user_id)
|
||||
)
|
||||
)["card"]
|
||||
else:
|
||||
user_name = [
|
||||
u["nickname"] for u in friend_str if u["user_id"] == x.user_qq
|
||||
u["nickname"] for u in friend_str if u["user_id"] == int(x.user_id)
|
||||
][0]
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
f"show_black_text_image 获取 USER {x.user_qq} user_name 失败 {type(e)}:{e}"
|
||||
f"show_black_text_image 获取 USER {x.user_id} user_name 失败", e=e
|
||||
)
|
||||
user_name = x.user_qq
|
||||
user_name = x.user_id
|
||||
id_str += f"{i}\n"
|
||||
uname_str += f"{user_name}\n"
|
||||
uid_str += f"{x.user_qq}\n"
|
||||
uid_str += f"{x.user_id}\n"
|
||||
gid_str += f"{x.group_id}\n"
|
||||
plant_text = " ".join(x.plant_text.split("\n"))
|
||||
if A.getsize(plant_text)[0] > 200:
|
||||
@@ -97,7 +100,7 @@ async def show_black_text_image(
|
||||
return A
|
||||
|
||||
|
||||
async def set_user_punish(user_id: int, id_: int, punish_level: int) -> str:
|
||||
async def set_user_punish(user_id: str, id_: int, punish_level: int) -> str:
|
||||
"""
|
||||
设置惩罚
|
||||
:param user_id: 用户id
|
||||
|
||||
+28
-21
@@ -7,13 +7,12 @@ from services.db_context import Model
|
||||
|
||||
|
||||
class BlackWord(Model):
|
||||
# __tablename__ = "black_word"
|
||||
|
||||
id = fields.IntField(pk=True, generated=True, auto_increment=True)
|
||||
"""自增id"""
|
||||
user_qq = fields.BigIntField()
|
||||
user_id = fields.CharField(255)
|
||||
"""用户id"""
|
||||
group_id = fields.BigIntField(null=True)
|
||||
group_id = fields.CharField(255, null=True)
|
||||
"""群聊id"""
|
||||
plant_text = fields.TextField()
|
||||
"""检测文本"""
|
||||
@@ -33,7 +32,7 @@ class BlackWord(Model):
|
||||
@classmethod
|
||||
async def set_user_punish(
|
||||
cls,
|
||||
user_qq: int,
|
||||
user_id: str,
|
||||
punish: str,
|
||||
black_word: Optional[str] = None,
|
||||
id_: Optional[int] = None,
|
||||
@@ -42,22 +41,22 @@ class BlackWord(Model):
|
||||
说明:
|
||||
设置处罚
|
||||
参数:
|
||||
:param user_qq: 用户id
|
||||
:param user_id: 用户id
|
||||
:param punish: 处罚
|
||||
:param black_word: 黑名单词汇
|
||||
:param id_: 记录下标
|
||||
"""
|
||||
user = None
|
||||
if (not black_word and not id_) or not punish:
|
||||
if (not black_word and id_ is None) or not punish:
|
||||
return False
|
||||
if black_word:
|
||||
user = (
|
||||
await cls.filter(user_qq=user_qq, black_word=black_word)
|
||||
await cls.filter(user_id=user_id, black_word=black_word, punish="")
|
||||
.order_by("id")
|
||||
.first()
|
||||
)
|
||||
elif id_:
|
||||
user_list = await cls.filter(user_qq=user_qq).order_by("id").all()
|
||||
elif id_ is not None:
|
||||
user_list = await cls.filter(user_id=user_id).order_by("id").all()
|
||||
if len(user_list) == 0 or (id_ < 0 or id_ > len(user_list)):
|
||||
return False
|
||||
user = user_list[id_]
|
||||
@@ -69,18 +68,18 @@ class BlackWord(Model):
|
||||
|
||||
@classmethod
|
||||
async def get_user_count(
|
||||
cls, user_qq: int, days: int = 7, punish_level: Optional[int] = None
|
||||
cls, user_id: str, days: int = 7, punish_level: Optional[int] = None
|
||||
) -> int:
|
||||
"""
|
||||
说明:
|
||||
获取用户规定周期内的犯事次数
|
||||
参数:
|
||||
:param user_qq: 用户qq
|
||||
:param user_id: 用户id
|
||||
:param days: 周期天数
|
||||
:param punish_level: 惩罚等级
|
||||
"""
|
||||
query = cls.filter(
|
||||
user_qq=user_qq,
|
||||
user_id=user_id,
|
||||
create_time__gte=datetime.now() - timedelta(days=days),
|
||||
punish_level__not_in=[-1],
|
||||
)
|
||||
@@ -89,17 +88,17 @@ class BlackWord(Model):
|
||||
return await query.count()
|
||||
|
||||
@classmethod
|
||||
async def get_user_punish_level(cls, user_qq: int, days: int = 7) -> Optional[int]:
|
||||
async def get_user_punish_level(cls, user_id: str, days: int = 7) -> Optional[int]:
|
||||
"""
|
||||
说明:
|
||||
获取用户最近一次的惩罚记录等级
|
||||
参数:
|
||||
:param user_qq: 用户qq
|
||||
:param user_id: 用户id
|
||||
:param days: 周期天数
|
||||
"""
|
||||
if (
|
||||
user := await cls.filter(
|
||||
user_qq=user_qq,
|
||||
user_id=user_id,
|
||||
create_time__gte=datetime.now() - timedelta(days=days),
|
||||
)
|
||||
.order_by("id")
|
||||
@@ -111,8 +110,8 @@ class BlackWord(Model):
|
||||
@classmethod
|
||||
async def get_black_data(
|
||||
cls,
|
||||
user_qq: Optional[int],
|
||||
group_id: Optional[int],
|
||||
user_id: Optional[str],
|
||||
group_id: Optional[str],
|
||||
date: Optional[datetime],
|
||||
date_type: str = "=",
|
||||
) -> List["BlackWord"]:
|
||||
@@ -120,14 +119,14 @@ class BlackWord(Model):
|
||||
说明:
|
||||
通过指定条件查询数据
|
||||
参数:
|
||||
:param user_qq: 用户qq
|
||||
:param user_id: 用户id
|
||||
:param group_id: 群号
|
||||
:param date: 日期
|
||||
:param date_type: 日期查询类型
|
||||
"""
|
||||
query = cls
|
||||
if user_qq:
|
||||
query = query.filter(user_qq=user_qq)
|
||||
if user_id:
|
||||
query = query.filter(user_id=user_id)
|
||||
if group_id:
|
||||
query = query.filter(group_id=group_id)
|
||||
if date:
|
||||
@@ -139,4 +138,12 @@ class BlackWord(Model):
|
||||
query = query.filter(create_time__gte=date)
|
||||
elif date_type == "<":
|
||||
query = query.filter(create_time__lte=date)
|
||||
return await query.order_by("id").all()
|
||||
return await query.all().order_by("id") # type: ignore
|
||||
|
||||
@classmethod
|
||||
async def _run_script(cls):
|
||||
return [
|
||||
"ALTER TABLE black_word RENAME COLUMN user_qq TO user_id;", # 将user_qq改为user_id
|
||||
"ALTER TABLE black_word ALTER COLUMN user_id TYPE character varying(255);",
|
||||
"ALTER TABLE black_word ALTER COLUMN group_id TYPE character varying(255);",
|
||||
]
|
||||
|
||||
+30
-23
@@ -83,7 +83,7 @@ class BlackWordManager:
|
||||
)
|
||||
|
||||
async def check(
|
||||
self, user_id: int, group_id: Optional[int], message: str
|
||||
self, user_id: str, group_id: Optional[str], message: str
|
||||
) -> Optional[Union[str, bool]]:
|
||||
"""
|
||||
检查是否包含黑名单词汇
|
||||
@@ -91,6 +91,7 @@ class BlackWordManager:
|
||||
:param group_id: 群号
|
||||
:param message: 消息
|
||||
"""
|
||||
logger.debug(f"检查文本是否含有黑名单词汇: {message}", "敏感词检测", user_id, group_id)
|
||||
if data := self._check(message):
|
||||
if data[0]:
|
||||
await _add_user_black_word(
|
||||
@@ -117,7 +118,7 @@ class BlackWordManager:
|
||||
for x in [self._word_list, self._py_list]:
|
||||
for level in x:
|
||||
if message in x[level] or py_msg in x[level]:
|
||||
return message if message in x[level] else py_msg, level
|
||||
return message if message in x[level] else py_msg, int(level)
|
||||
# 模糊匹配
|
||||
for x in [self._word_list, self._py_list]:
|
||||
for level in x:
|
||||
@@ -128,8 +129,8 @@ class BlackWordManager:
|
||||
|
||||
|
||||
async def _add_user_black_word(
|
||||
user_id: int,
|
||||
group_id: Optional[int],
|
||||
user_id: str,
|
||||
group_id: Optional[str],
|
||||
black_word: str,
|
||||
message: str,
|
||||
punish_level: int,
|
||||
@@ -144,13 +145,17 @@ async def _add_user_black_word(
|
||||
"""
|
||||
cycle_days = Config.get_config("black_word", "CYCLE_DAYS") or 7
|
||||
user_count = await BlackWord.get_user_count(user_id, cycle_days, punish_level)
|
||||
add_punish_level_to_count = Config.get_config(
|
||||
"black_word", "ADD_PUNISH_LEVEL_TO_COUNT"
|
||||
)
|
||||
# 周期内超过次数直接提升惩罚
|
||||
if Config.get_config(
|
||||
"black_word", "AUTO_ADD_PUNISH_LEVEL"
|
||||
) and user_count > Config.get_config("black_word", "ADD_PUNISH_LEVEL_TO_COUNT"):
|
||||
if (
|
||||
Config.get_config("black_word", "AUTO_ADD_PUNISH_LEVEL")
|
||||
and add_punish_level_to_count
|
||||
):
|
||||
punish_level -= 1
|
||||
await BlackWord.create(
|
||||
user_qq=user_id,
|
||||
user_id=user_id,
|
||||
group_id=group_id,
|
||||
plant_text=message,
|
||||
black_word=black_word,
|
||||
@@ -165,7 +170,7 @@ async def _add_user_black_word(
|
||||
|
||||
|
||||
async def _punish_handle(
|
||||
user_id: int, group_id: Optional[int], punish_level: int, black_word: str
|
||||
user_id: str, group_id: Optional[str], punish_level: int, black_word: str
|
||||
):
|
||||
"""
|
||||
惩罚措施,级别越低惩罚越严
|
||||
@@ -218,7 +223,7 @@ async def _punish_handle(
|
||||
|
||||
|
||||
async def _get_punish(
|
||||
id_: int, user_id: int, group_id: Optional[int] = None
|
||||
id_: int, user_id: str, group_id: Optional[str] = None
|
||||
) -> Optional[Union[int, str]]:
|
||||
"""
|
||||
通过id_获取惩罚
|
||||
@@ -230,12 +235,12 @@ async def _get_punish(
|
||||
# 忽略的群聊
|
||||
# _ignore_group = Config.get_config("black_word", "IGNORE_GROUP")
|
||||
# 处罚 id 4 ban 时间:int,List[int]
|
||||
ban_3_duration = Config.get_config("black_word", "BAN_3_DURATION")
|
||||
ban_3_duration = Config.get_config("black_word", "BAN_3_DURATION") or 7
|
||||
# 处罚 id 4 ban 时间:int,List[int]
|
||||
ban_4_duration = Config.get_config("black_word", "BAN_4_DURATION")
|
||||
ban_4_duration = Config.get_config("black_word", "BAN_4_DURATION") or 360
|
||||
# 口头警告内容
|
||||
warning_result = Config.get_config("black_word", "WARNING_RESULT")
|
||||
if user := await GroupInfoUser.get_or_none(user_id=str(user_id), group_id=str(group_id)):
|
||||
if user := await GroupInfoUser.get_or_none(user_id=user_id, group_id=group_id):
|
||||
uname = user.user_name
|
||||
else:
|
||||
uname = user_id
|
||||
@@ -285,28 +290,30 @@ async def _get_punish(
|
||||
# 口头警告
|
||||
elif id_ == 5:
|
||||
if group_id:
|
||||
await bot.send_group_msg(group_id=group_id, message=warning_result)
|
||||
await bot.send_group_msg(group_id=int(group_id), message=warning_result)
|
||||
else:
|
||||
await bot.send_private_msg(user_id=user_id, message=warning_result)
|
||||
await bot.send_private_msg(user_id=int(user_id), message=warning_result)
|
||||
logger.info(f"BlackWord 口头警告 USER {user_id}")
|
||||
return warning_result
|
||||
return None
|
||||
|
||||
|
||||
async def send_msg(user_id: int, group_id: Optional[int], message: str):
|
||||
async def send_msg(
|
||||
user_id: Union[str, int], group_id: Optional[Union[str, int]], message: str
|
||||
):
|
||||
"""
|
||||
发送消息
|
||||
:param user_id: user_id
|
||||
:param group_id: group_id
|
||||
:param message: message
|
||||
"""
|
||||
bot = get_bot()
|
||||
if not user_id:
|
||||
user_id = int(list(bot.config.superusers)[0])
|
||||
if group_id:
|
||||
await bot.send_group_msg(group_id=group_id, message=message)
|
||||
else:
|
||||
await bot.send_private_msg(user_id=user_id, message=message)
|
||||
if bot := get_bot():
|
||||
if not user_id:
|
||||
user_id = list(bot.config.superusers)[0]
|
||||
if group_id:
|
||||
await bot.send_group_msg(group_id=int(group_id), message=message)
|
||||
else:
|
||||
await bot.send_private_msg(user_id=int(user_id), message=message)
|
||||
|
||||
|
||||
async def check_text(text: str) -> bool:
|
||||
|
||||
Reference in New Issue
Block a user