🐛 修复权限管理失效和不跟着复读的问题

This commit is contained in:
CMHopeSunshine 2022-11-29 10:58:02 +08:00
parent 801f06b34c
commit 5e3d5aea8e
6 changed files with 41 additions and 46 deletions

View File

@ -29,18 +29,17 @@ async def handle_command_alias(event: MessageEvent):
def combine_msg(new_command: str, extra_msg: str, is_reverse: bool):
return (new_command + extra_msg) if not is_reverse else (extra_msg + new_command)
return extra_msg + new_command if is_reverse else new_command + extra_msg
def modify_msg(all_alias: List[CommandAlias], msg: str):
for alias in all_alias:
if alias.is_regex:
msg = re.sub(alias.alias, alias.command, msg)
else:
if alias.mode == AliasMode.prefix and msg.startswith(alias.alias):
msg = combine_msg(alias.command, msg[len(alias.alias):], alias.is_reverse)
elif alias.mode == AliasMode.suffix and msg.endswith(alias.alias):
msg = combine_msg(msg[:-len(alias.alias)], alias.command, alias.is_reverse)
elif alias.mode == AliasMode.full_match and msg == alias.alias:
msg = alias.command
elif alias.mode == AliasMode.prefix and msg.startswith(alias.alias):
msg = combine_msg(alias.command, msg[len(alias.alias):], alias.is_reverse)
elif alias.mode == AliasMode.suffix and msg.endswith(alias.alias):
msg = combine_msg(msg[:-len(alias.alias)], alias.command, alias.is_reverse)
elif alias.mode == AliasMode.full_match and msg == alias.alias:
msg = alias.command
return msg

View File

@ -14,8 +14,8 @@ class ConfigManager:
def set_config(cls, config_name: str, value: any):
"""
设置派蒙配置项
:param config_name: 配置名
:param value: 新配置值
:param config_name: 配置名
:param value: 新配置值
"""
if config_name not in cls.config.dict(by_alias=True).keys():
return f'没有配置项为{config_name}'

View File

@ -60,7 +60,7 @@
"10000072": ["坎蒂丝", "潘森"],
"10000073": ["纳西妲", "草神", "艹神", "小草王", "小吉祥草王", "草萝莉", "艹萝莉", "羽毛球", "布耶尔"],
"10000074": ["莱依拉", "莱伊拉"],
"10000075": ["流浪者", "散兵", "伞兵", "国崩", "卢本伟", "大炮", "sb"],
"10000075": ["流浪者", "散兵", "国崩", "大炮"],
"10000076": ["珐露珊", "法露珊", "珐妹","初音", "miku"],
"10000103": ["迪希雅"],
"10000104": ["艾尔海森", "苏", "海哥"],

View File

@ -54,9 +54,8 @@ class PluginManager:
for ban_user in perm.ban:
await PluginDisable.update_or_create(name=perm.name, group_id=perm.session_id,
user_id=ban_user)
else:
if not perm.status:
await PluginDisable.update_or_create(name=perm.name, user_id=perm.session_id)
elif not perm.status:
await PluginDisable.update_or_create(name=perm.name, user_id=perm.session_id)
await PluginPermission.all().delete()
await PluginDisable.filter(global_disable=False, group_id=None, user_id=None).delete()
for plugin in plugin_list:
@ -98,21 +97,16 @@ class PluginManager:
plugin_list = sorted(cls.plugins.values(), key=lambda x: x.priority).copy()
plugin_list = [p for p in plugin_list if p.show and p.module_name in load_plugins]
for plugin in plugin_list:
if not await PluginDisable.filter(name=plugin.module_name, global_disable=True).exists():
if message_type != 'guild':
# plugin_info = await PluginPermission.get_or_none(name=plugin.module_name, session_id=session_id,
# session_type=message_type)
# plugin.status = True if plugin_info is None else plugin_info.status
if message_type == 'group':
plugin.status = not await PluginDisable.filter(name=plugin.module_name,
group_id=session_id).exists()
else:
plugin.status = not await PluginDisable.filter(name=plugin.module_name,
user_id=session_id).exists()
else:
plugin.status = True
else:
if await PluginDisable.filter(name=plugin.module_name, global_disable=True).exists():
plugin.status = True
elif message_type == 'group':
plugin.status = not await PluginDisable.filter(name=plugin.module_name,
group_id=session_id, user_id=None).exists()
elif message_type == 'guild':
plugin.status = True
else:
plugin.status = not await PluginDisable.filter(name=plugin.module_name,
user_id=session_id).exists()
if plugin.matchers:
plugin.matchers.sort(key=lambda x: x.pm_priority)
plugin.matchers = [m for m in plugin.matchers if m.pm_show and m.pm_usage]
@ -135,25 +129,31 @@ class PluginManager:
@run_preprocessor
async def _(event: MessageEvent, matcher: Matcher):
try:
if event.user_id in SUPERUSERS:
return
if not matcher.plugin_name or matcher.plugin_name in HIDDEN_PLUGINS:
return
if not isinstance(event, (PrivateMessageEvent, GroupMessageEvent)):
return
if event.user_id in SUPERUSERS:
return
if not matcher.plugin_name or matcher.plugin_name in HIDDEN_PLUGINS:
return
if not isinstance(event, (PrivateMessageEvent, GroupMessageEvent)):
return
# 权限检查
# 权限检查
is_ignored = False
try:
if await PluginDisable.get_or_none(name=matcher.plugin_name, global_disable=True):
raise IgnoredException('插件使用权限已禁用')
is_ignored = True
if await PluginDisable.get_or_none(name=matcher.plugin_name, user_id=event.user_id, group_id=None):
raise IgnoredException('插件使用权限已禁用')
is_ignored = True
elif isinstance(event, GroupMessageEvent) and (
perms := await PluginDisable.filter(name=matcher.plugin_name, group_id=event.group_id)):
user_ids = [p.user_id for p in perms]
if None in user_ids or event.user_id in user_ids:
raise IgnoredException('插件使用权限已禁用')
is_ignored = True
except Exception as e:
logger.info('插件管理器', f'插件权限检查<r>失败:{e}</r>')
if is_ignored:
raise IgnoredException('插件使用权限已禁用')
with contextlib.suppress(Exception):
# 命令调用统计
if matcher.plugin_name in PluginManager.plugins and 'pm_name' in matcher.state:
if matcher_info := list(filter(lambda x: x.pm_name == matcher.state['pm_name'],
@ -166,7 +166,3 @@ async def _(event: MessageEvent, matcher: Matcher):
user_id=event.user_id,
message_type=event.message_type,
time=datetime.datetime.now())
except IgnoredException:
pass
except Exception as e:
logger.info('插件管理器', f'插件权限检查<r>失败:{e}</r>')

View File

@ -181,7 +181,7 @@ class LearningChat:
# 如果达到阈值,且不是全都为同一个人在说,则进行复读
if len(messages) >= self.config.repeat_threshold and all(
message.message == self.data.message
for message in messages) and all(message.user_id != messages[0].user_id
for message in messages) and any(message.user_id != self.data.user_id
for message in messages):
if random.random() < self.config.break_probability:
logger.debug('群聊学习', '➤➤达到复读阈值,打断复读!')
@ -337,7 +337,7 @@ class LearningChat:
today_time = time.mktime(datetime.date.today().timetuple())
# 获取两小时内消息超过10条的群列表
groups = await ChatMessage.filter(time__gte=today_time).annotate(count=Count('id')).group_by('group_id'). \
filter(count__gte=10).values_list('group_id', flat=True)
filter(count__gte=10).values_list('group_id', flat=True)
if not groups:
return None
total_messages = {}

View File

@ -30,6 +30,6 @@ async def get_match_card(name: str):
return difflib.get_close_matches(name, card_list, cutoff=0.6, n=10) if card_list else None
@scheduler.scheduled_job('cron', hour='*/2')
@scheduler.scheduled_job('cron', minute='*/30')
async def _():
await update_card_list()