Example #1
0
async def handle_list_node(bot: Bot, event: Event, state: T_State):
    sub_command = state["sub_command"]
    if sub_command == 'list':
        detail_type = event.dict().get(f'{event.get_type()}_type')
        if detail_type == 'group':
            group_id = event.dict().get('group_id')
            _res = DBAuth.list(auth_type='group', auth_id=group_id)
            if _res.success():
                node_text = '\n'.join('/'.join(map(str, n))
                                      for n in _res.result)
                msg = f'当前群组权限列表为:\n\n{node_text}'
                await omegaauth.finish(msg)
            else:
                await omegaauth.finish('发生了意外的错误QAQ, 请稍后再试')
        elif detail_type == 'private':
            user_id = event.dict().get('user_id')
            _res = DBAuth.list(auth_type='user', auth_id=user_id)
            if _res.success():
                node_text = '\n'.join('/'.join(map(str, n))
                                      for n in _res.result)
                msg = f'当前用户权限列表为:\n\n{node_text}'
                await omegaauth.finish(msg)
            else:
                await omegaauth.finish('发生了意外的错误QAQ, 请稍后再试')
        else:
            await omegaauth.finish('非授权会话, 操作中止')
Example #2
0
    async def _has_level_or_node(bot: Bot, event: Event, state: T_State) -> bool:
        auth_node = '.'.join(auth_nodes)
        detail_type = event.dict().get(f'{event.get_type()}_type')
        group_id = event.dict().get('group_id')
        user_id = event.dict().get('user_id')

        # level检查部分
        if detail_type != 'group':
            level_checker = False
        else:
            if DBGroup(group_id=group_id).permission_level().result >= level:
                level_checker = True
            else:
                level_checker = False

        # node检查部分
        if detail_type == 'private':
            allow_tag = DBAuth(auth_id=user_id, auth_type='user', auth_node=auth_node).allow_tag().result
            deny_tag = DBAuth(auth_id=user_id, auth_type='user', auth_node=auth_node).deny_tag().result
        elif detail_type == 'group':
            allow_tag = DBAuth(auth_id=group_id, auth_type='group', auth_node=auth_node).allow_tag().result
            deny_tag = DBAuth(auth_id=group_id, auth_type='group', auth_node=auth_node).deny_tag().result
        else:
            allow_tag = 0
            deny_tag = 0

        if allow_tag == 1 and deny_tag == 0:
            return True
        elif allow_tag == -2 and deny_tag == -2:
            return level_checker
        else:
            return False
Example #3
0
 async def _has_command_permission(bot: Bot, event: Event, state: T_State) -> bool:
     detail_type = event.dict().get(f'{event.get_type()}_type')
     group_id = event.dict().get('group_id')
     # 检查当前消息类型
     if detail_type != 'group':
         return False
     else:
         if DBGroup(group_id=group_id).permission_level().result >= level:
             return True
         else:
             return False
Example #4
0
async def handle_message(bot: Bot, event: Event, state: T_State):
    try:
        user_name = event.dict().get('sender').get('card')
        if not user_name:
            user_name = event.dict().get('sender').get('nickname')
        time = event.dict().get('time')
        self_id = event.dict().get('self_id')
        post_type = event.get_type()
        detail_type = event.dict().get(f'{event.get_type()}_type')
        sub_type = event.dict().get('sub_type')
        group_id = event.dict().get('group_id')
        user_id = event.dict().get('user_id')
        raw_data = repr(event)
        msg_data = str(event.dict().get('message'))
        new_event = DBHistory(time=time,
                              self_id=self_id,
                              post_type=post_type,
                              detail_type=detail_type)
        new_event.add(sub_type=sub_type,
                      group_id=group_id,
                      user_id=user_id,
                      user_name=user_name,
                      raw_data=raw_data,
                      msg_data=msg_data)
    except Exception as e:
        logger.error(f'Message history recording Failed, error: {repr(e)}')
Example #5
0
    async def _has_auth_node(bot: Bot, event: Event, state: T_State) -> bool:
        auth_node = '.'.join(auth_nodes)
        detail_type = event.dict().get(f'{event.get_type()}_type')
        group_id = event.dict().get('group_id')
        user_id = event.dict().get('user_id')
        # 检查当前消息类型
        if detail_type == 'private':
            allow_tag = DBAuth(auth_id=user_id, auth_type='user', auth_node=auth_node).allow_tag().result
            deny_tag = DBAuth(auth_id=user_id, auth_type='user', auth_node=auth_node).deny_tag().result
        elif detail_type == 'group' or detail_type == 'group_upload':
            allow_tag = DBAuth(auth_id=group_id, auth_type='group', auth_node=auth_node).allow_tag().result
            deny_tag = DBAuth(auth_id=group_id, auth_type='group', auth_node=auth_node).deny_tag().result
        else:
            allow_tag = 0
            deny_tag = 0

        if allow_tag == 1 and deny_tag == 0:
            return True
        else:
            return False
Example #6
0
 def from_payload(cls, payload: NoneBotEvent) -> "Event":
     """
     从 CQHTTP 事件数据构造 `Event` 对象。
     """
     payload_dict = payload.dict()
     if isinstance(payload, MessageEvent):
         payload_dict["message"] = Message([
             MessageSegment(type_=segment.type, data=segment.data)
             for segment in payload.message
         ])
     return cls(payload_dict)
Example #7
0
async def handle_plugin_cooldown(matcher: Matcher, bot: Bot, event: Event,
                                 state: T_State):
    group_id = event.dict().get('group_id')
    user_id = event.dict().get('user_id')

    global_config = get_driver().config
    superusers = global_config.superusers

    # 忽略超级用户
    if user_id in [int(x) for x in superusers]:
        return

    # 只处理message事件
    if matcher.type != 'message':
        return

    # 处理插件冷却
    # 冷却处理优先级: 全局>插件>群组>用户
    # 冷却限制优先级: 用户>群组>插件>全局
    plugin_name = matcher.module
    plugin = get_plugin(plugin_name)
    plugin_cool_down_list = plugin.export.get('cool_down')

    # 只处理声明了__plugin_cool_down__的插件
    if not plugin_cool_down_list:
        return

    # 检查用户或群组是否有skip_cd权限, 跳过冷却检查
    skip_cd_auth_node = f'{plugin_name}.{PluginCoolDown.skip_auth_node}'
    user_auth_res = DBAuth(auth_id=user_id,
                           auth_type='user',
                           auth_node=skip_cd_auth_node)
    if user_auth_res.allow_tag().result == 1 and user_auth_res.deny_tag(
    ).result == 0:
        return

    group_auth_res = DBAuth(auth_id=group_id,
                            auth_type='group',
                            auth_node=skip_cd_auth_node)
    if group_auth_res.allow_tag().result == 1 and group_auth_res.deny_tag(
    ).result == 0:
        return

    # 检查冷却情况
    global_check = DBCoolDownEvent.check_global_cool_down_event()
    plugin_check = DBCoolDownEvent.check_plugin_cool_down_event(
        plugin=plugin_name)
    group_check = DBCoolDownEvent.check_group_cool_down_event(
        plugin=plugin_name, group_id=group_id)
    user_check = DBCoolDownEvent.check_user_cool_down_event(plugin=plugin_name,
                                                            user_id=user_id)

    # 处理全局冷却
    # 先检查是否已有全局冷却
    if plugin_check.result == 1 or group_check.result == 1 or user_check.result == 1:
        pass
    elif global_check.result == 1:
        await bot.send(
            event=event,
            message=Message(
                f'{MessageSegment.at(user_id=user_id)}命令冷却中!\n{global_check.info}'
            ))
        raise IgnoredException('全局命令冷却中')
    elif global_check.result == 0:
        pass
    else:
        logger.error(
            f'全局冷却事件异常! group: {group_id}, user: {user_id}, error: {global_check.info}'
        )
    # 然后再处理命令中存在的全局冷却
    for time in [
            x.cool_down_time for x in plugin_cool_down_list
            if x.type == 'global'
    ]:
        # 若有插件、群组或用户冷却则交由其处理
        if plugin_check.result == 1 or group_check.result == 1 or user_check.result == 1:
            break

        res = check_and_set_global_cool_down(minutes=time)
        if res.result == 1:
            await bot.send(
                event=event,
                message=Message(
                    f'{MessageSegment.at(user_id=user_id)}命令冷却中!\n{res.info}'))
            raise IgnoredException('全局命令冷却中')
        elif res.result == 0:
            pass
        else:
            logger.error(
                f'全局冷却事件异常! group: {group_id}, user: {user_id}, error: {res.info}'
            )

    # 处理插件冷却
    for time in [
            x.cool_down_time for x in plugin_cool_down_list
            if x.type == 'plugin'
    ]:
        # 若有群组或用户冷却则交由其处理
        if group_check.result == 1 or user_check.result == 1:
            break

        res = check_and_set_plugin_cool_down(minutes=time, plugin=plugin_name)
        if res.result == 1:
            await bot.send(
                event=event,
                message=Message(
                    f'{MessageSegment.at(user_id=user_id)}命令冷却中!\n{res.info}'))
            raise IgnoredException('插件命令冷却中')
        elif res.result == 0:
            pass
        else:
            logger.error(
                f'插件冷却事件异常! group: {group_id}, user: {user_id}, plugin: {plugin_name}, error: {res.info}'
            )

    # 处理群组冷却
    for time in [
            x.cool_down_time for x in plugin_cool_down_list
            if x.type == 'group'
    ]:
        if not group_id:
            break

        # 若有用户冷却则交由其处理
        if user_check.result == 1:
            break

        res = check_and_set_group_cool_down(minutes=time,
                                            plugin=plugin_name,
                                            group_id=group_id)
        if res.result == 1:
            await bot.send(
                event=event,
                message=Message(
                    f'{MessageSegment.at(user_id=user_id)}命令冷却中!\n{res.info}'))
            raise IgnoredException('群组命令冷却中')
        elif res.result == 0:
            pass
        else:
            logger.error(
                f'群组冷却事件异常! group: {group_id}, user: {user_id}, plugin: {plugin_name}, error: {res.info}'
            )

    # 处理用户冷却
    for time in [
            x.cool_down_time for x in plugin_cool_down_list if x.type == 'user'
    ]:
        if not user_id:
            break

        res = check_and_set_user_cool_down(minutes=time,
                                           plugin=plugin_name,
                                           user_id=user_id)
        if res.result == 1:
            await bot.send(
                event=event,
                message=Message(
                    f'{MessageSegment.at(user_id=user_id)}命令冷却中!\n{res.info}'))
            raise IgnoredException('用户命令冷却中')
        elif res.result == 0:
            pass
        else:
            logger.error(
                f'用户冷却事件异常! group: {group_id}, user: {user_id}, plugin: {plugin_name}, error: {res.info}'
            )