예제 #1
0
async def guess_number(session: CommandSession):

    a = 0  # correct digit
    b = 0  # correct digit in wrong place

    print('target = ', session.state.get('target'))

    async def custom_validator(value):
        s = set(list(value))
        if len(value) != len(s):
            raise ValidateError('请不要輸入重复数字~')
        return value

    while not session.state['bingo'] and session.state['current_tries']:
        parm = 'guess' + str(session.state['current_tries'])
        guess = session.get(
            parm,
            prompt="请做出你的猜测",
            arg_filters=[
                extractors.extract_text,  # 提取纯文本部分
                str.strip,  # 去掉两边的空白
                validators.not_empty(),
                validators.match_regex(r'[0-9]{5}', '必须为5位数字'),
                custom_validator,
                # validators.ensure_true(lambda x: 9999 < int(x) < 100000, '必须猜测10000 ~ 99999中的数字')
            ],
            at_sender=True)

        guess_time = "这是第" + str(session.state['total_chances'] + 1 -
                                 session.state['current_tries']) + '次猜测:\n'
        for i in range(len(session.state.get('target'))):
            if guess[i] == session.state.get('target')[i]:
                a += 1
        for c in guess:
            if c in session.state.get('target'):
                b += 1
        b -= a
        # b = random.randint(1, 4)
        if a == session.state['length']:
            res = guess_time
            res += "恭喜你回答正确!\n"
            res += '你也可以百度搜索‘1A2B’获取移动端程式,祝您游戏愉快~'
            await session.send(res)
            session.state['bingo'] = True
            return
        res = guess_time
        res += "结果是:"
        res += str(a) + 'A ' + str(b) + 'B'
        await session.send(res)
        session.state['current_tries'] -= 1
        a = b = 0

        if session.state['current_tries'] == 0:
            res = guess_time
            res += "抱歉,挑战失败,正确答案是"
            res += session.state.get('target')
            res += '\n你也可以百度搜索‘1A2B’获取移动端程式,祝您游戏愉快~'
            await session.send(res)
    return
예제 #2
0
async def add_my_skill(session: CommandSession):
    group_id = session.event.group_id
    session_type = session.event.detail_type
    if session_type == 'group':
        if not has_command_permissions(group_id):
            await session.send('本群组没有执行命令的权限呢QAQ')
            log.logger.info(f'{__name__}: 群组: {group_id} 没有命令权限, 已中止命令执行')
            return
    elif session_type == 'private':
        await session.send('本命令不支持在私聊中使用QAQ')
        log.logger.info(
            f'{__name__}: 用户: {session.event.user_id} 在{session_type}中使用了命令, 已中止命令执行'
        )
        return
    else:
        log.logger.info(
            f'{__name__}: 用户: {session.event.user_id} 在{session_type}环境中使用了命令, 已中止命令执行'
        )
        return
    __user_qq = session.event.user_id
    __skill_list = await query_skill_list()
    skill_name = session.get('skill_name',
                             prompt='请输入你想设置的技能名称: ',
                             arg_filters=[
                                 controllers.handle_cancellation(session),
                                 validators.not_empty('输入不能为空')
                             ])
    if skill_name not in __skill_list:
        session.finish('没有这个技能哦, 请重新设置~')
    skill_level = session.get('skill_level',
                              prompt='请输入你想设置的技能等级: \n可设置等级有"普通"、"熟练"、"专业"',
                              arg_filters=[
                                  controllers.handle_cancellation(session),
                                  validators.not_empty('输入不能为空'),
                                  validators.match_regex(r'^(普通|熟练|专业)$',
                                                         '没有这个技能等级, 请重新输入~',
                                                         fullmatch=True)
                              ])
    try:
        __result = await add_user_skill_in_db(user_qq=__user_qq,
                                              skill_name=skill_name,
                                              skill_level=skill_level)
        if __result:
            await session.send(f'为你新增了技能: 【{skill_name}/{skill_level}】')
            log.logger.info(
                f'{__name__}: 群组: {group_id}, 用户: {__user_qq} 成功新增了自己的技能')
        else:
            await session.send('添加失败, 发生了意外的错误QAQ')
            log.logger.info(
                f'{__name__}: 群组: {group_id}, 用户: {__user_qq} 新增技能失败, add_user_skill_in_db错误'
            )
    except Exception as e:
        log.logger.warning(
            f'{__name__}: 群组: {group_id}, 用户: {__user_qq} 试图使用命令add_my_skill时发生了错误: {e}'
        )
예제 #3
0
async def announce(session: CommandSession):
    __group_id = session.get('group_id', prompt='请输入你想通知的群号: ',
                             arg_filters=[controllers.handle_cancellation(session),
                                          validators.not_empty('输入不能为空'),
                                          validators.match_regex(r'^\d+$', '格式不对, 请重新输入~', fullmatch=True)])
    __group_id = int(__group_id)
    if __group_id == 0:
        __announce_text = session.get('announce_text', prompt='【全局公告】请输入你想发布的公告内容: ',
                                      arg_filters=[controllers.handle_cancellation(session),
                                                   validators.not_empty('输入不能为空')])
        for group_id in query_all_notice_groups():
            try:
                await session.bot.send_group_msg(group_id=group_id, message=__announce_text)
                log.logger.info(f'{__name__}: 已向群组: {__group_id} 发送通知')
            except Exception as e:
                log.logger.error(f'{__name__}: 向群组: {__group_id} 发送通知失败, error info: {e}')
                continue
        return
    elif __group_id == 1:
        __announce_text = session.get('announce_text', prompt='【命令组公告】请输入你想发布的公告内容: ',
                                      arg_filters=[controllers.handle_cancellation(session),
                                                   validators.not_empty('输入不能为空')])
        for group_id in query_all_command_groups():
            try:
                await session.bot.send_group_msg(group_id=group_id, message=__announce_text)
                log.logger.info(f'{__name__}: 已向群组: {__group_id} 发送通知')
            except Exception as e:
                log.logger.error(f'{__name__}: 向群组: {__group_id} 发送通知失败, error info: {e}')
                continue
        return
    elif __group_id == 9:
        __announce_text = session.get('announce_text', prompt='【管理组公告】请输入你想发布的公告内容: ',
                                      arg_filters=[controllers.handle_cancellation(session),
                                                   validators.not_empty('输入不能为空')])
        for group_id in query_all_admin_groups():
            try:
                await session.bot.send_group_msg(group_id=group_id, message=__announce_text)
                log.logger.info(f'{__name__}: 已向群组: {__group_id} 发送通知')
            except Exception as e:
                log.logger.error(f'{__name__}: 向群组: {__group_id} 发送通知失败, error info: {e}')
                continue
        return
    elif __group_id not in query_all_notice_groups():
        log.logger.info(f'{__name__}: 群组: {__group_id} 没有通知权限, 无法发送通知')
        await session.send('群组未配置通知权限!')
        return
    __announce_text = session.get('announce_text', prompt='请输入你想发布的公告内容: ',
                                  arg_filters=[controllers.handle_cancellation(session),
                                               validators.not_empty('输入不能为空')])
    try:
        await session.bot.send_group_msg(group_id=__group_id, message=__announce_text)
        log.logger.info(f'{__name__}: 已向群组: {__group_id} 发送通知')
    except Exception as e:
        log.logger.error(f'{__name__}: 向群组: {__group_id} 发送通知失败, error info: {e}')
예제 #4
0
async def pixivision_sub(session: CommandSession):
    group_id = session.event.group_id
    session_type = session.event.detail_type
    if session_type == 'group':
        if not has_command_permissions(group_id):
            await session.send('本群组没有执行命令的权限呢QAQ')
            log.logger.info(f'{__name__}: 群组: {group_id} 没有命令权限, 已中止命令执行')
            return
    elif session_type == 'private':
        await session.send('本命令不支持在私聊中使用QAQ')
        log.logger.info(
            f'{__name__}: 用户: {session.event.user_id} 在{session_type}中使用了命令, 已中止命令执行'
        )
        return
    else:
        log.logger.info(
            f'{__name__}: 用户: {session.event.user_id} 在{session_type}环境中使用了命令, 已中止命令执行'
        )
        return
    mode = session.get('mode',
                       prompt='Pixivision\n【订阅】or【取消订阅】?',
                       arg_filters=[
                           controllers.handle_cancellation(session),
                           validators.not_empty('输入不能为空'),
                           validators.match_regex(r'^(订阅|取消订阅)$',
                                                  '指令不对哦, 请重新输入~',
                                                  fullmatch=True)
                       ])
    if mode == '订阅':
        __is_success = await add_pixivsion_sub_to_db(group_id=group_id)
        if not __is_success:
            await session.send('发生了意料之外的错误QAQ')
            log.logger.warning(
                f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} '
                f'试图订阅pixivision时发生了错误, 向数据库写入群组订阅失败.')
            return
        await session.send(f'已订阅Pixivision!')
        log.logger.info(
            f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} 订阅了pixivision.'
        )
    elif mode == '取消订阅':
        __is_success = await clean_group_pixivsion_sub_in_db(group_id=group_id)
        if not __is_success:
            await session.send('发生了意料之外的错误QAQ')
            log.logger.warning(
                f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} '
                f'试图取消订阅pixivision时发生了错误, 向数据库写入群组订阅失败.')
            return
        await session.send(f'已取消Pixivision订阅!')
        log.logger.info(
            f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} 取消了pixivision订阅.'
        )
예제 #5
0
async def set_my_status(session: CommandSession):
    group_id = session.event.group_id
    session_type = session.event.detail_type
    if session_type == 'group':
        if not has_command_permissions(group_id):
            await session.send('本群组没有执行命令的权限呢QAQ')
            log.logger.info(f'{__name__}: 群组: {group_id} 没有命令权限, 已中止命令执行')
            return
    elif session_type == 'private':
        await session.send('本命令不支持在私聊中使用QAQ')
        log.logger.info(
            f'{__name__}: 用户: {session.event.user_id} 在{session_type}中使用了命令, 已中止命令执行'
        )
        return
    else:
        log.logger.info(
            f'{__name__}: 用户: {session.event.user_id} 在{session_type}环境中使用了命令, 已中止命令执行'
        )
        return
    __user_qq = session.event.user_id
    __status = session.get('status',
                           prompt='请输入你想设置的状态: \n可选"空闲"、"工作"',
                           arg_filters=[
                               controllers.handle_cancellation(session),
                               validators.not_empty('输入不能为空'),
                               validators.match_regex(r'^(空闲|工作)$',
                                                      '没有这个状态, 请重新输入~',
                                                      fullmatch=True)
                           ])
    if __status == '空闲':
        __status = 0
    elif __status == '工作':
        __status = 2
    try:
        __result = await set_my_status_in_db(user_qq=__user_qq,
                                             user_status=__status)
        if __result:
            await session.send(f'为你设置了状态: 【{session.state["status"]}】')
            log.logger.info(
                f'{__name__}: 群组: {group_id}, 用户: {__user_qq} 为自己更新了状态')
        else:
            await session.send(f'添加失败, 发生了意外的错误QAQ\n您可能不在用户列表中, 请联系管理员处理')
            log.logger.info(
                f'{__name__}: 群组: {group_id}, 用户: {__user_qq} 设置状态失败, 用户不在用户列表中'
            )
    except Exception as e:
        log.logger.warning(
            f'{__name__}: 群组: {group_id}, 用户: {__user_qq} 试图使用命令set_my_status时发生了错误: {e}'
        )
예제 #6
0
async def gomoku(session: CommandSession):

    if not __enabled__:
        logger.warning("Gomoku plugin is disabled due to the failure of loading clib. See https://233a344a455.github.io/DeltaBot/setup.html#gomoku%E6%A8%A1%E5%9D%97-%E4%BA%94%E5%AD%90%E6%A3%8B-%E5%AE%89%E8%A3%85.")
        await session.send('插件未启用!')
        return

    await session.send("游戏开始!\n"
                       "玩家黑棋,机器人白棋\n"
                       "落子格式: 例如'e9'或'E9'\n"
                       "字母在前数字在后,不分大小写")

    g = GomokuGame(clib, context_id(session.ctx, use_hash=True))
    await session.send(MessageSegment.image(g.get_img()))
    await asyncio.sleep(0.5)
    while True:
        inp = await session.aget('input', force_update=True, prompt="请玩家落子",
                                 arg_filters=[
                                     str.strip,
                                     not_empty(message="输入不能为空!请重新输入"),
                                     handle_cancellation(session),
                                     match_regex(r'[a-oA-O](1[0-5]|[1-9])', fullmatch=True,
                                                 message="无效的输入!请重新输入\n落子格式: 例如'e9'或'E9',"
                                                         "字母在前数字在后,不分大小写\n【发送'取消'结束游戏】")])
        x, y = re.match(r'([a-oA-O])(1[0-5]|[1-9])', inp).groups()
        x, y = ALPHABET.index(x.upper()), int(y) - 1
        if not g.set_chess(x, y, USER):
            await session.send(f"位置'{inp}'已存在旗子,请重新输入")
            continue

        sco = -g.estimate()
        g.update_display()
        if sco > 1e6:
            await session.send("恭喜!你赢了" + MessageSegment.image(g.get_img()))
            return

        x, y = g.search()
        g.set_chess(x, y, BOT)
        g.display.draw_chess(x, y, BOT, high_lighted=True)
        sco = -g.estimate()
        if sco < -1e6:
            await session.send("你输啦 ~\(≧▽≦)/~" + MessageSegment.image(g.get_img()))
            return

        await session.send(f"估计分数: {sco}" + MessageSegment.image(g.get_img()))
        await asyncio.sleep(0.8)
예제 #7
0
async def MurphyisLaw(session: CommandSession):
    buildnum = session.get_optional('num', default=1)
    buildtype = session.get('type',
                            prompt='要建造哪个类型呢?',
                            arg_filters=[
                                match_regex(
                                    pattern=r'轻|轻型|重|重型|特|特型|限|限时|[a-dA-D1-4]',
                                    message='格式不正确,请重输',
                                    fullmatch=True),
                                handle_cancellation(session),
                            ])
    for i in TYPE:
        if buildtype in TYPE[i]:
            if buildnum > 10:
                buildnum = 10
            buildresult = await startshipbuilding(i, buildnum)
            break

    await session.finish("\n建造类型:{}\n建造次数:{}\n{}".format(
        buildtype, buildnum, buildresult),
                         at_sender=True)
예제 #8
0
async def set_group_permissions(session: CommandSession):
    try:
        __group_id = session.event.group_id
        __group_info = await session.bot.get_group_info(group_id=__group_id)
    except Exception as e:
        log.logger.warning(
            f'{__name__}: 用户{session.event.user_id} 使用命令set_group_permissions时发生错误: {e}, 该命令需要在群组中使用'
        )
        session.finish('请在群组内使用该命令!')
        return

    noitce_permissions = session.get(
        'noitce_permissions',
        prompt='是否配置【通知】权限?',
        arg_filters=[
            controllers.handle_cancellation(session),
            validators.not_empty('输入不能为空'),
            validators.match_regex(r'^(是|否)$', '请输入【是】或【否】', fullmatch=True)
        ])
    command_permissions = session.get(
        'command_permissions',
        prompt='是否配置【命令】权限?',
        arg_filters=[
            controllers.handle_cancellation(session),
            validators.not_empty('输入不能为空'),
            validators.match_regex(r'^(是|否)$', '请输入【是】或【否】', fullmatch=True)
        ])
    admin_permissions = session.get(
        'admin_permissions',
        prompt='是否配置【管理命令】权限?',
        arg_filters=[
            controllers.handle_cancellation(session),
            validators.not_empty('输入不能为空'),
            validators.match_regex(r'^(是|否)$', '请输入【是】或【否】', fullmatch=True)
        ])
    # 转换权限值
    if noitce_permissions == '是':
        noitce_permissions = 1
    elif noitce_permissions == '否':
        noitce_permissions = 0

    if command_permissions == '是':
        command_permissions = 1
    elif command_permissions == '否':
        command_permissions = 0

    if admin_permissions == '是':
        admin_permissions = 1
    elif admin_permissions == '否':
        admin_permissions = 0

    try:
        # 重新设置权限
        __is_success = \
            await set_group_permissions_to_db(group_id=__group_id, noitce_permissions=noitce_permissions,
                                              command_permissions=command_permissions,
                                              admin_permissions=admin_permissions)
        if not __is_success:
            await session.send('发生了意料之外的错误OvO')
            log.logger.warning(
                f'{__name__}: 用户{session.event.user_id} 使用set_group_permissions'
                f'更新群组: {session.event.group_id} 时发生错误, 数据库操作失败, 错误信息见日志.')
            return
        await session.send('已成功配置本群组权限~')
        log.logger.info(
            f'{__name__}: 用户{session.event.user_id} 使用set_group_permissions'
            f'配置了群组: {session.event.group_id} 的群组权限')
    except Exception as e:
        log.logger.warning(
            f'{__name__}: 用户{session.event.user_id} 使用命令set_group_permissions时发生错误: {e}, 设置群组权限失败'
        )
예제 #9
0
async def new_dy_sub(session: CommandSession):
    group_id = session.event.group_id
    session_type = session.event.detail_type
    if session_type == 'group':
        if not has_command_permissions(group_id):
            await session.send('本群组没有执行命令的权限呢QAQ')
            log.logger.info(f'{__name__}: 群组: {group_id} 没有命令权限, 已中止命令执行')
            return
    elif session_type == 'private':
        await session.send('本命令不支持在私聊中使用QAQ')
        log.logger.info(f'{__name__}: 用户: {session.event.user_id} 在{session_type}中使用了命令, 已中止命令执行')
        return
    else:
        log.logger.info(f'{__name__}: 用户: {session.event.user_id} 在{session_type}环境中使用了命令, 已中止命令执行')
        return
    # 从会话状态(session.state)中获取sub_id, 如果当前不存在, 则询问用户
    __sub_id = session.get('sub_id', prompt='请输入想要订阅的UP的UID: ',
                           arg_filters=[controllers.handle_cancellation(session),
                                        validators.not_empty('输入不能为空'),
                                        validators.match_regex(r'^\d+$', 'UID格式不对哦, 请重新输入~',
                                                               fullmatch=True)])
    # 获取UP的信息
    __up_info = await get_user_info(__sub_id)
    if __up_info['status'] == 'error':
        await session.send('似乎并没有这个UP呀QAQ')
        log.logger.info(f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} 添加动态订阅失败, '
                        f'没有找到该UID: {__sub_id} 对应的UP')
        return
    else:
        __up_name = __up_info['name']
        __sub_check = session.get('check', prompt=f'即将订阅【{__up_name}】的动态!\n确认吗?\n\n【是/否】',
                                  arg_filters=[controllers.handle_cancellation(session),
                                               validators.not_empty('输入不能为空'),
                                               validators.match_regex(r'^[是否]$', '输入不对哦, 请重新输入~',
                                                                      fullmatch=True)])
        if __sub_check == '否':
            await session.send('那就不订阅好了QAQ')
            log.logger.info(f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} 已取消添加动态订阅, 操作中止')
            return
        elif __sub_check == '是':
            try:
                # 首先更新动态订阅信息
                is_sub_dy_success = await add_dy_sub_to_db(sub_id=__sub_id, up_name=__up_name)
                if not is_sub_dy_success:
                    await session.send('发生了意料之外的错误QAQ')
                    log.logger.warning(f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} '
                                       f'试图添加: {__sub_id} 动态订阅时发生了错误, 更新订阅信息失败.')
                    return
                # 然后添加群组订阅
                is_add_group_sub_success = await add_group_dy_sub_to_db(sub_id=__sub_id, group_id=group_id)
                if not is_add_group_sub_success:
                    await session.send('发生了意料之外的错误QAQ')
                    log.logger.warning(f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} '
                                       f'试图添加: {__sub_id} 动态订阅时发生了错误, 向数据库写入群组订阅失败.')
                    return
                # 添加动态订阅时需要刷新该up的动态到数据库中
                __dy_info = await get_dynamic_info(__sub_id)
                for num in range(len(__dy_info)):
                    dy_id = __dy_info[num]['id']
                    dy_type = __dy_info[num]['type']
                    content = __dy_info[num]['content']
                    # 想数据库中写入动态信息
                    is_add_dy_info_success = \
                        await add_dy_info_to_db(user_id=__sub_id, dynamic_id=dy_id,
                                                dynamic_type=dy_type, content=content)
                    if not is_add_dy_info_success:
                        log.logger.warning(f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} '
                                           f'试图添加: {__sub_id} 动态订阅时发生了错误, 向数据库写入动态{dy_id}信息失败, 已跳过.')
                await session.send(f'已成功的订阅【{__up_name}】的动态!')
                log.logger.info(f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} '
                                f'已成功添加: {__up_name} 的动态订阅')
            except Exception as e:
                await session.send('订阅成功,但在更新动态时发生了未知的错误QAQ')
                log.logger.error(f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} '
                                 f'试图添加: {__sub_id} 动态订阅时发生了错误, error info: {e}.')
예제 #10
0
async def roll(session: CommandSession):
    group_id = session.event.group_id
    session_type = session.event.detail_type
    if session_type == 'group':
        if not has_command_permissions(group_id):
            await session.send('本群组没有执行命令的权限呢QAQ')
            log.logger.info(f'{__name__}: 群组: {group_id} 没有命令权限, 已中止命令执行')
            return
    elif session_type == 'private':
        await session.send('本命令不支持在私聊中使用QAQ')
        log.logger.info(
            f'{__name__}: 用户: {session.event.user_id} 在{session_type}中使用了命令, 已中止命令执行'
        )
        return
    else:
        log.logger.info(
            f'{__name__}: 用户: {session.event.user_id} 在{session_type}环境中使用了命令, 已中止命令执行'
        )
        return
    dice_num = session.get('dice_num',
                           prompt='请输入骰子个数: ',
                           arg_filters=[
                               controllers.handle_cancellation(session),
                               validators.not_empty('输入不能为空'),
                               validators.match_regex(r'^\d+$',
                                                      '只能是数字哦, 请重新输入~',
                                                      fullmatch=True)
                           ])
    dice_side = session.get('dice_side',
                            prompt='请输入骰子面数: ',
                            arg_filters=[
                                controllers.handle_cancellation(session),
                                validators.not_empty('输入不能为空'),
                                validators.match_regex(r'^\d+$',
                                                       '只能是数字哦, 请重新输入~',
                                                       fullmatch=True)
                            ])
    try:
        # 初始化随机种子
        random_seed = hash(
            str([
                session.event.user_id, session.event.group_id,
                datetime.datetime.now()
            ]))
        random.seed(random_seed)
        # 加入一个趣味的机制
        if random.randint(1, 101) == 100:
            await session.send(f'【彩蛋】骰子之神似乎不看好你, 你掷出的骰子全部消失了')
            return
        dice_num = int(dice_num)
        dice_side = int(dice_side)
        if dice_num < 0 or dice_side < 0:
            await session.send(f'【错误】你掷出了不存在的骰子, 只有上帝知道结果是多少')
            return
        dice_result = 0
        for i in range(dice_num):
            this_dice_result = random.choice(range(dice_side)) + 1
            dice_result += this_dice_result
        await session.send(f'你掷出了{dice_num}个{dice_side}面骰子, 点数为【{dice_result}】'
                           )
        log.logger.info(
            f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} '
            f'Roll了一次{dice_num}d{dice_side}, 结果为{dice_result}')
        return
    except Exception as e:
        await session.send(f'【错误】不知道发生了什么, 你掷出的骰子全部裂开了')
        log.logger.error(
            f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} Roll时发生了错误: {e}'
        )
        return
예제 #11
0
async def lottery(session: CommandSession):
    group_id = session.event.group_id
    session_type = session.event.detail_type
    if session_type == 'group':
        if not has_command_permissions(group_id):
            await session.send('本群组没有执行命令的权限呢QAQ')
            log.logger.info(f'{__name__}: 群组: {group_id} 没有命令权限, 已中止命令执行')
            return
    elif session_type == 'private':
        await session.send('本命令不支持在私聊中使用QAQ')
        log.logger.info(
            f'{__name__}: 用户: {session.event.user_id} 在{session_type}中使用了命令, 已中止命令执行'
        )
        return
    else:
        log.logger.info(
            f'{__name__}: 用户: {session.event.user_id} 在{session_type}环境中使用了命令, 已中止命令执行'
        )
        return
    # 初始化随机种子
    random_seed = hash(
        str([
            session.event.user_id, session.event.group_id,
            datetime.datetime.now()
        ]))
    random.seed(random_seed)

    people_num = session.get('people_num',
                             prompt='请输入抽奖的人数: ',
                             arg_filters=[
                                 controllers.handle_cancellation(session),
                                 validators.not_empty('输入不能为空'),
                                 validators.match_regex(r'^\d+$',
                                                        '只能是数字哦, 请重新输入~',
                                                        fullmatch=True)
                             ])
    people_num = int(people_num)

    group_user_list = await session.bot.get_group_member_list(group_id=group_id
                                                              )
    group_user_name_list = []

    for user_info in group_user_list:
        __user_qq = user_info['user_id']
        __user_group_info = await session.bot.get_group_member_info(
            group_id=group_id, user_id=__user_qq)
        __user_group_nickmane = __user_group_info['card']
        if not __user_group_nickmane:
            __user_group_nickmane = __user_group_info['nickname']
        group_user_name_list.append(__user_group_nickmane)

    if people_num > len(group_user_name_list):
        await session.send(f'抽奖人数大于群成员人数了QAQ, 请重新设置人数哦~')
        return

    lottery_result = random.sample(group_user_name_list, k=people_num)

    msg = ''
    for item in lottery_result:
        msg += f'\n【{item}】'
    await session.send(f'抽奖人数: 【{people_num}】\n以下是中奖名单:\n{msg}')
    log.logger.info(
        f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} 进行了一次抽奖')
    return
예제 #12
0
async def new_live_sub(session: CommandSession):
    group_id = session.event.group_id
    session_type = session.event.detail_type
    if session_type == 'group':
        if not has_command_permissions(group_id):
            await session.send('本群组没有执行命令的权限呢QAQ')
            log.logger.info(f'{__name__}: 群组: {group_id} 没有命令权限, 已中止命令执行')
            return
    elif session_type == 'private':
        await session.send('本命令不支持在私聊中使用QAQ')
        log.logger.info(f'{__name__}: 用户: {session.event.user_id} 在{session_type}中使用了命令, 已中止命令执行')
        return
    else:
        log.logger.info(f'{__name__}: 用户: {session.event.user_id} 在{session_type}环境中使用了命令, 已中止命令执行')
        return
    # 从会话状态(session.state)中获取sub_id, 如果当前不存在, 则询问用户
    __sub_id = session.get('sub_id', prompt='请输入直播间房间号: ',
                           arg_filters=[controllers.handle_cancellation(session),
                                        validators.not_empty('输入不能为空'),
                                        validators.match_regex(r'^\d+$', '房间号格式不对哦, 请重新输入~',
                                                               fullmatch=True)])
    # 获取直播间信息
    __live_info = await get_live_info(__sub_id)
    if __live_info['status'] == 'error':
        await session.send('似乎并没有这个直播间QAQ')
        log.logger.info(f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} 添加直播间订阅失败, '
                        f'没有找到该房间号: {__sub_id} 对应的直播间')
        return
    else:
        __up_info = await get_user_info(__live_info['uid'])
        __up_name = __up_info['name']
        __sub_check = session.get('check', prompt=f'即将订阅【{__up_name}】的直播间!\n确认吗?\n\n【是/否】',
                                  arg_filters=[controllers.handle_cancellation(session),
                                               validators.not_empty('输入不能为空'),
                                               validators.match_regex(r'^[是否]$', '输入不对哦, 请重新输入~',
                                                                      fullmatch=True)])
        if __sub_check == '否':
            await session.send('那就不订阅好了QAQ')
            log.logger.info(f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} 已取消添加直播间订阅, 操作中止')
            return
        elif __sub_check == '是':
            try:
                # 首先更新直播间信息
                __is_success = await add_live_sub_to_db(sub_id=__sub_id, up_name=__up_name)
                if not __is_success:
                    await session.send('发生了意料之外的错误OvO')
                    log.logger.warning(f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} '
                                       f'试图订阅直播间{__sub_id}时发生了错误, 更新直播间信息数据库操作失败, 错误信息见日志.')
                    return
                # 然后添加群组订阅
                __is_success = await add_group_live_sub_to_db(sub_id=__sub_id, group_id=group_id)
                if not __is_success:
                    await session.send('发生了意料之外的错误OvO')
                    log.logger.warning(f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} '
                                       f'试图订阅直播间{__sub_id}时发生了错误, 添加群组订阅数据库操作失败, 错误信息见日志.')
                    return
                # 添加直播间时需要刷新全局监控列表
                global live_status
                global live_title
                for __room_id in query_live_sub_list():
                    # 直播状态放入live_status全局变量中
                    live_status[__room_id] = await init_live_status(__room_id)
                for __room_id in query_live_sub_list():
                    # 直播间标题放入live_title全局变量中
                    live_title[__room_id] = await init_live_title(__room_id)
                await session.send(f'已成功的订阅【{__up_name}】的直播间!')
                log.logger.info(f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} 已成功添加: {__up_name} 的直播间订阅')
            except Exception as e:
                log.logger.warning(f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} '
                                   f'试图添加: {__sub_id} 直播间订阅时发生了错误: {e}')
                session.finish('发生了未知的错误QAQ')
예제 #13
0
async def searchimage(session: CommandSession):
    # 图片转base64
    async def pic_2_base64(url: str) -> str:
        async def get_image(pic_url: str):
            timeout_count = 0
            while timeout_count < 3:
                try:
                    timeout = aiohttp.ClientTimeout(total=2)
                    async with aiohttp.ClientSession(
                            timeout=timeout) as __session:
                        headers = {
                            'user-agent':
                            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                            'AppleWebKit/537.36 (KHTML, like Gecko) '
                            'Chrome/83.0.4103.116 Safari/537.36'
                        }
                        async with __session.get(url=pic_url,
                                                 headers=headers,
                                                 timeout=timeout) as resp:
                            image = await resp.read()
                    return image
                except Exception as __err:
                    log.logger.warning(
                        f'{__name__}: pic_2_base64: get_image ERROR: {__err}. '
                        f'Occurred in try {timeout_count + 1} using paras: {pic_url}'
                    )
                finally:
                    timeout_count += 1
            else:
                log.logger.warning(
                    f'{__name__}: pic_2_base64: '
                    f'get_image ERROR: Timeout {timeout_count}, using paras: {pic_url}'
                )
                return None

        origin_image_f = BytesIO()
        try:
            origin_image_f.write(await get_image(pic_url=url))
        except Exception as err:
            log.logger.warning(f'{__name__}: pic_2_base64 ERROR: {err}')
            return ''
        b64 = base64.b64encode(origin_image_f.getvalue())
        b64 = str(b64, encoding='utf-8')
        b64 = 'base64://' + b64
        origin_image_f.close()
        return b64

    # 获取识别结果 Saucenao模块
    async def get_identify_result(url: str) -> list:
        async def get_result(__url: str, paras: dict) -> dict:
            timeout_count = 0
            while timeout_count < 3:
                try:
                    timeout = aiohttp.ClientTimeout(total=10)
                    async with aiohttp.ClientSession(
                            timeout=timeout) as __session:
                        headers = {
                            'user-agent':
                            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                            'AppleWebKit/537.36 (KHTML, like Gecko) '
                            'Chrome/83.0.4103.116 Safari/537.36'
                        }
                        async with __session.get(url=__url,
                                                 params=paras,
                                                 headers=headers,
                                                 timeout=timeout) as resp:
                            json = await resp.json()
                    return json
                except Exception as err:
                    log.logger.warning(
                        f'{__name__}: get_identify_result ERROR: {err}. '
                        f'Occurred in try {timeout_count + 1} using paras: {paras}'
                    )
                finally:
                    timeout_count += 1
            else:
                log.logger.warning(
                    f'{__name__}: get_identify_result ERROR: '
                    f'Timeout {timeout_count}, using paras: {paras}')
                return {'header': {'status': 1}, 'results': []}

        __payload = {
            'output_type': 2,
            'api_key': API_KEY,
            'testmode': 1,
            'numres': 6,
            'db': 999,
            'url': url
        }
        __result_json = await get_result(__url=API_URL, paras=__payload)
        if __result_json['header']['status'] != 0:
            log.logger.warning(
                f"{__name__}: get_identify_result ERROR: "
                f"status code: {__result_json['header']['status']}, Sever or Client error"
            )
            return []
        __result = []
        for __item in __result_json['results']:
            try:
                if int(float(__item['header']['similarity'])) < 60:
                    continue
                else:
                    __result.append({
                        'similarity':
                        __item['header']['similarity'],
                        'thumbnail':
                        __item['header']['thumbnail'],
                        'index_name':
                        __item['header']['index_name'],
                        'ext_urls':
                        __item['data']['ext_urls']
                    })
            except Exception as res_err:
                log.logger.warning(
                    f"{__name__}: get_identify_result ERROR: {res_err}, can not resolve results"
                )
                continue
        return __result

    # 获取识别结果 Saucenao模块
    async def get_ascii2d_identify_result(url: str) -> list:
        async def get_ascii2d_redirects(__url: str) -> dict:
            timeout_count = 0
            while timeout_count < 3:
                try:
                    timeout = aiohttp.ClientTimeout(total=10)
                    async with aiohttp.ClientSession(
                            timeout=timeout) as __session:
                        headers = {
                            'user-agent':
                            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                            'AppleWebKit/537.36 (KHTML, like Gecko) '
                            'Chrome/83.0.4103.116 Safari/537.36',
                            'accept-language':
                            'zh-CN,zh;q=0.9'
                        }
                        async with __session.get(
                                url=__url,
                                headers=headers,
                                timeout=timeout,
                                allow_redirects=False) as resp:
                            res_headers = resp.headers
                            res_dict = {
                                'error': False,
                                'body': dict(res_headers)
                            }
                    return res_dict
                except Exception as err:
                    log.logger.warning(
                        f'{__name__}: get_ascii2d_redirects ERROR: {err}. '
                        f'Occurred in try {timeout_count + 1} using url: {__url}'
                    )
                finally:
                    timeout_count += 1
            else:
                log.logger.warning(
                    f'{__name__}: get_ascii2d_redirects ERROR: '
                    f'Timeout {timeout_count}, using url: {__url}')
                return {'error': True, 'body': None}

        async def get_ascii2d_result(__url: str) -> str:
            timeout_count = 0
            while timeout_count < 3:
                try:
                    timeout = aiohttp.ClientTimeout(total=10)
                    async with aiohttp.ClientSession(
                            timeout=timeout) as __session:
                        headers = {
                            'user-agent':
                            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                            'AppleWebKit/537.36 (KHTML, like Gecko) '
                            'Chrome/83.0.4103.116 Safari/537.36',
                            'accept-language':
                            'zh-CN,zh;q=0.9'
                        }
                        async with __session.get(url=__url,
                                                 headers=headers,
                                                 timeout=timeout) as resp:
                            res_headers = await resp.text()
                    return res_headers
                except Exception as err:
                    log.logger.warning(
                        f'{__name__}: get_ascii2d_result ERROR: {err}. '
                        f'Occurred in try {timeout_count + 1} using url: {__url}'
                    )
                finally:
                    timeout_count += 1
            else:
                log.logger.warning(
                    f'{__name__}: get_ascii2d_result ERROR: '
                    f'Timeout {timeout_count}, using url: {__url}')
                return ''

        search_url = f'{API_URL_ASCII2D}{url}'
        __result_json = await get_ascii2d_redirects(__url=search_url)
        if not __result_json['error']:
            ascii2d_color_url = __result_json['body']['Location']
            ascii2d_bovw_url = re.sub(r'https://ascii2d\.net/search/color/',
                                      r'https://ascii2d.net/search/bovw/',
                                      ascii2d_color_url)
        else:
            log.logger.warning(
                f'{__name__}: get_ascii2d_identify_result ERROR: 获取识别结果url发生错误, 错误信息详见日志.'
            )
            return []

        color_res = await get_ascii2d_result(ascii2d_color_url)
        bovw_res = await get_ascii2d_result(ascii2d_bovw_url)

        pre_bs_list = []
        if color_res:
            pre_bs_list.append(color_res)
        if bovw_res:
            pre_bs_list.append(bovw_res)
        if not pre_bs_list:
            log.logger.warning(
                f'{__name__}: get_ascii2d_identify_result ERROR: 获取识别结果异常, 错误信息详见日志.'
            )
            return []

        __result = []

        for result in pre_bs_list:
            try:
                gallery_soup = BeautifulSoup(result, 'lxml')
                # 模式
                search_mode = gallery_soup.find('h5', {
                    'class': 'p-t-1 text-xs-center'
                }).get_text(strip=True)
                # 每一个搜索结果
                row = gallery_soup.find_all('div', {'class': 'row item-box'})
            except Exception as page_err:
                log.logger.warning(
                    f'{__name__}: get_ascii2d_identify_result ERROR: {page_err}, 解析结果页时发生错误.'
                )
                continue
            # ascii2d搜索偏差过大,pixiv及twitter结果只取第一个
            pixiv_count = 0
            twitter_count = 0
            for row_item in row:
                # 对每个搜索结果进行解析
                try:
                    detail = row_item.find('div',
                                           {'class': 'detail-box gray-link'})
                    is_null = detail.get_text(strip=True)
                    if not is_null:
                        continue
                    # 来源部分,ascii2d网页css调整大概率导致此处解析失败,调试重点关注
                    source_type = detail.find('h6').find('small').get_text(
                        strip=True)
                    if source_type == 'pixiv':
                        if pixiv_count > 0:
                            break
                        else:
                            pixiv_count += 1
                    elif source_type == 'twitter':
                        if twitter_count > 0:
                            break
                        else:
                            twitter_count += 1
                    else:
                        continue
                    source = detail.find('h6').get_text('/', strip=True)
                    source_url = detail.find('h6').find(
                        'a', {
                            'title': None,
                            'style': None
                        }).get('href')
                    # 预览图部分,ascii2d网页css调整大概率导致此处解析失败,调试重点关注
                    preview_img_url = row_item. \
                        find('div', {'class': 'col-xs-12 col-sm-12 col-md-4 col-xl-4 text-xs-center image-box'}). \
                        find('img').get('src')
                    __result.append({
                        'similarity': 'null',
                        'thumbnail': f'https://ascii2d.net{preview_img_url}',
                        'index_name': f'ascii2d - {search_mode} - {source}',
                        'ext_urls': source_url
                    })
                except Exception as row_err:
                    log.logger.warning(
                        f'{__name__}: get_ascii2d_identify_result ERROR: {row_err}, 解搜索结果条目时发生错误.'
                    )
                    continue

        return __result

    # 命令主要部分
    group_id = session.event.group_id
    session_type = session.event.detail_type
    if session_type == 'group':
        if not has_command_permissions(group_id):
            await session.send('本群组没有执行命令的权限呢QAQ')
            log.logger.info(f'{__name__}: 群组: {group_id} 没有命令权限, 已中止命令执行')
            return
    elif session_type == 'private':
        log.logger.info(
            f'{__name__}: 用户: {session.event.user_id} 在{session_type}中使用了命令')
    else:
        log.logger.info(
            f'{__name__}: 用户: {session.event.user_id} 在{session_type}环境中使用了命令, 已中止命令执行'
        )
        return
    image_msg = f'请发送你想要识别的图片: '
    session.get('image',
                prompt=image_msg,
                arg_filters=[
                    controllers.handle_cancellation(session),
                    validators.not_empty('输入不能为空~'),
                    validators.match_regex(r'^(\[CQ\:image\,file\=)',
                                           '你发送的似乎不是图片呢, 请重新发送~',
                                           fullmatch=False)
                ])
    if session.current_key == 'image':
        # aiocqhttp 可直接获取url
        try:
            session.state['image_url'] = session.current_arg_images[0]
        except Exception as e:
            log.logger.debug(f'{__name__}: Searchimage: {e}. 在Mirai框架中运行')
            # mirai无法直接获取图片url
            # 针对mirai-native的cqhttp插件的cq码适配
            if session_type == 'group':
                imageid = re.sub(r'^(\[CQ:image,file={)', '',
                                 session.current_arg)
                imageid = re.sub(r'(}\.mirai\.mnimg])$', '', imageid)
                imageid = re.sub(r'-', '', imageid)
                imageurl = f'https://gchat.qpic.cn/gchatpic_new/0/0-0-{imageid}/0?term=2'
                session.state['image_url'] = imageurl
            elif session_type == 'private':
                imageid = re.sub(r'^(\[CQ:image,file=/)', '',
                                 session.current_arg)
                imageid = re.sub(r'(\.mnimg])$', '', imageid)
                imageurl = f'https://gchat.qpic.cn/gchatpic_new/0/{imageid}/0?term=2'
                session.state['image_url'] = imageurl
    image_url = session.state['image_url']
    try:
        await session.send('获取识别结果中, 请稍后~')
        identify_result = await get_identify_result(url=image_url)
        identify_ascii2d_result = await get_ascii2d_identify_result(
            url=image_url)
        # 合并搜索结果
        identify_result.extend(identify_ascii2d_result)
        if identify_result:
            for item in identify_result:
                try:
                    if type(item['ext_urls']) == list:
                        ext_urls = ''
                        for urls in item['ext_urls']:
                            ext_urls += f'{urls}\n'
                        ext_urls = ext_urls.strip()
                    else:
                        ext_urls = item['ext_urls']
                        ext_urls = ext_urls.strip()
                    img_b64 = await pic_2_base64(item['thumbnail'])
                    if img_b64 == '':
                        msg = f"识别结果: {item['index_name']}\n\n相似度: {item['similarity']}\n资源链接: {ext_urls}"
                        await session.send(msg)
                    else:
                        img_seg = MessageSegment.image(img_b64)
                        msg = f"识别结果: {item['index_name']}\n\n相似度: {item['similarity']}\n资源链接: {ext_urls}\n{img_seg}"
                        await session.send(msg)
                except Exception as e:
                    log.logger.warning(f'{__name__}: 处理和发送识别结果时发生了错误: {e}')
                    continue
            log.logger.info(
                f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} 使用searchimage成功搜索了一张图片'
            )
            return
        else:
            await session.send('没有找到相似度足够高的图片QAQ')
            log.logger.info(
                f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} 使用了searchimage, 但没有找到相似的图片'
            )
            return
    except Exception as e:
        await session.send('识图失败, 发生了意外的错误QAQ')
        log.logger.warning(
            f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} 试图使用命令searchimage时发生了错误: {e}'
        )
        return
예제 #14
0
async def vocation(session: CommandSession):
    group_id = session.event.group_id
    session_type = session.event.detail_type
    if session_type == 'group':
        if not has_command_permissions(group_id):
            await session.send('本群组没有执行命令的权限呢QAQ')
            log.logger.info(f'{__name__}: 群组: {group_id} 没有命令权限, 已中止命令执行')
            return
    elif session_type == 'private':
        await session.send('本命令不支持在私聊中使用QAQ')
        log.logger.info(
            f'{__name__}: 用户: {session.event.user_id} 在{session_type}中使用了命令, 已中止命令执行'
        )
        return
    else:
        log.logger.info(
            f'{__name__}: 用户: {session.event.user_id} 在{session_type}环境中使用了命令, 已中止命令执行'
        )
        return
    __user_qq = session.event.user_id
    __time = session.get('time',
                         prompt='请输入你想请假的时间: \n仅支持数字+周/天/小时/分/分钟/秒',
                         arg_filters=[
                             controllers.handle_cancellation(session),
                             validators.not_empty('输入不能为空'),
                             validators.match_regex(
                                 r'^\d+(周|天|小时|分|分钟|秒)$',
                                 '仅支持数字+周/天/小时/分/分钟/秒, 请重新输入~',
                                 fullmatch=True)
                         ])
    __reason = session.get(
        'reason',
        prompt='请输入你的请假理由: \n不想告诉我理由的话就输入无理由吧~',
        arg_filters=[controllers.handle_cancellation(session)])
    __add_time = timedelta()
    if re.match(r'^\d+周$', __time):
        __time = int(re.sub(r'周$', '', __time))
        __add_time = timedelta(weeks=__time)
    elif re.match(r'^\d+天$', __time):
        __time = int(re.sub(r'天$', '', __time))
        __add_time = timedelta(days=__time)
    elif re.match(r'^\d+小时$', __time):
        __time = int(re.sub(r'小时$', '', __time))
        __add_time = timedelta(hours=__time)
    elif re.match(r'^\d+(分|分钟)$', __time):
        __time = int(re.sub(r'(分|分钟)$', '', __time))
        __add_time = timedelta(minutes=__time)
    elif re.match(r'^\d+秒$', __time):
        __time = int(re.sub(r'秒$', '', __time))
        __add_time = timedelta(seconds=__time)
    __vocation_time = datetime.now() + __add_time
    try:
        __result = await set_my_vocation_in_db(user_qq=__user_qq,
                                               vocation_times=__vocation_time,
                                               reason=__reason)
        if __result:
            await session.send(
                f'请假成功!\n你的假期将持续到: \n【{__vocation_time.strftime("%Y-%m-%d %H:%M:%S")}】'
            )
            log.logger.info(
                f'{__name__}: 群组: {group_id}, 用户: {__user_qq} 请假成功')
        else:
            await session.send('请假失败, 发生了意外的错误QAQ')
            log.logger.info(
                f'{__name__}: 群组: {group_id}, 用户: {__user_qq} 请假失败, 用户不在用户列表中')
    except Exception as e:
        log.logger.warning(
            f'{__name__}: 群组: {group_id}, 用户: {__user_qq} 试图使用命令vocation时发生了错误: {e}'
        )
예제 #15
0
async def translate(session: CommandSession):
    subject = await session.aget(
        'subject',
        prompt='你想啥样的类型?',
        arg_filters=[
            extractors.extract_text,  # 取纯文本部分
            controllers.handle_cancellation(session),  # 处理用户可能的取消指令
            str.strip  # 去掉两边空白字符
        ])
    if subject == "数学":
        await session.finish("数学题目属于图片,暂时发不出来")
    elif subject not in ["语文", "数学", "计算机", "英语", "政治"]:
        await session.finish("不支持你所输入的科目,仅支持语文,数学,计算机,英语,政治")
        question = {}

    while True:
        try:
            question = await random_question(subject)
            while question == {}:
                question = await random_question(subject)
            print(question)
            print(question == {})
            types = question.get("type")
            # 随机题库
            anwser = "随机的题库为:《{}》\n{}\n \n".format(question.get("title"),
                                                   question.get("question"))
            gets = question.get("choose")

            if (types != 3):
                if (type(gets).__name__ == "list"):
                    if (len(gets)) != 0:
                        inx = 0
                        for choose in gets:
                            anwser = anwser + tran_numer_to_char(
                                inx) + ":" + choose + "\n"
                            inx = inx + 1
                else:
                    anwser = anwser + gets + "\n"

            await session.send(anwser)
            isFigth = False
            rigth_number = 0

            # 正确与错误判断逻辑
            if (types == 1):
                # 单选题逻辑
                user_anwser = await session.aget(
                    'ansser',
                    prompt='这是一道选择题,请输入你的答案 ',
                    arg_filters=[
                        extractors.extract_text,  # 取纯文本部分
                        controllers.handle_cancellation(
                            session),  # 处理用户可能的取消指令
                        str.strip,  # 去掉两边空白字符
                        validators.match_regex(r'^[a,b,c,d,A,B,C,D]$',
                                               '请输入a b c d'),
                    ])
                print(user_anwser)
                upper = str.upper(user_anwser)
                number_inx = int(question.get("question_standard_answer"))
                number = tran_numer_to_char(number_inx)
                print("答案是{}".format(number))
                if (upper == number):
                    isFigth = True
                else:
                    rigth_number = number
                # await session.send("这是一道选择题,请输入前面的索引 如1,2,3,4")
            elif (types == 2):
                print("多选")
                # 多选题逻辑
                user_anwsers = await session.aget(
                    'ansser',
                    prompt='这是一道多选题,请输入你的答案,以空格分割。 ',
                    arg_filters=[
                        extractors.extract_text,  # 取纯文本部分
                        controllers.handle_cancellation(
                            session),  # 处理用户可能的取消指令
                        str.strip,  # 去掉两边空白字符
                    ])
                tran_user_anwser = []
                # 正确答案转换为abc
                tran_str_answser = []
                str_anwsers = str.split(
                    replace_to_arry(question.get("question_standard_answer")),
                    ",")
                for str_anwser_one in str_anwsers:
                    tran_str_answser.append(
                        tran_numer_to_char(int(str_anwser_one)))

                # 将用户的回答转换,排序
                strip_anwser_list = user_anwsers.split(" ")
                print(strip_anwser_list)
                for strip_anwser in strip_anwser_list:
                    tran_user_anwser.append(str.upper(strip_anwser))

                tran_user_anwser.sort()

                if (tran_user_anwser == tran_str_answser):
                    isFigth = True
                else:
                    rigth_number = tran_str_answser

            elif (types == 3):
                # 填空题逻辑
                user_anwsers = await session.aget(
                    'ansser',
                    prompt='这是一道填空题,请输入你的答案 ',
                    arg_filters=[
                        extractors.extract_text,  # 取纯文本部分
                        controllers.handle_cancellation(
                            session),  # 处理用户可能的取消指令
                        str.strip,  # 去掉两边空白字符
                    ])
                replace = question.get("question_standard_answer")
                if (user_anwsers == replace):
                    isFigth = True
                else:
                    rigth_number = replace

            else:
                await session.send("sorry,现在只支持单选题")

            if isFigth:
                await session.send("恭喜您答对了,\n这道题目的解析是\n{}".format(
                    question.get("question_analyze")))
                sender = session.event.sender
                addRanking(sender.get("user_id"), sender.get("nickname"))
            else:
                await session.send("抱歉你回答错了,\n正确答案是{},\n这道题目的解析是\n{}".format(
                    rigth_number, question.get("question_analyze")))
            session.state.pop("ansser")
            time.sleep(2)
        except Exception:
            print(Exception.__name__)
            session.finish("让我休息0.00001秒")
예제 #16
0
async def stickermacker(session: CommandSession):
    group_id = session.event.group_id
    session_type = session.event.detail_type
    if session_type == 'group':
        if not has_command_permissions(group_id):
            await session.send('本群组没有执行命令的权限呢QAQ')
            log.logger.info(f'{__name__}: 群组: {group_id} 没有命令权限, 已中止命令执行')
            return
    elif session_type == 'private':
        log.logger.info(
            f'{__name__}: 用户: {session.event.user_id} 在{session_type}中使用了命令')
    else:
        log.logger.info(
            f'{__name__}: 用户: {session.event.user_id} 在{session_type}环境中使用了命令, 已中止命令执行'
        )
        return

    # 定义模板名称、类型, 处理模板正则
    sticker_temp = {
        '默认': {
            'name': 'default',
            'type': 'default',
            'text_part': 1,
            'help_msg': '该模板不支持gif'
        },
        '白底': {
            'name': 'whitebg',
            'type': 'default',
            'text_part': 1,
            'help_msg': '该模板不支持gif'
        },
        '小天使': {
            'name': 'littleangel',
            'type': 'default',
            'text_part': 1,
            'help_msg': '该模板不支持gif'
        },
        '有内鬼': {
            'name': 'traitor',
            'type': 'static',
            'text_part': 1,
            'help_msg': '该模板字数限制100(x)'
        },
        '王境泽': {
            'name': 'wangjingze',
            'type': 'gif',
            'text_part': 4,
            'help_msg': '请检查文本分段'
        },
        '为所欲为': {
            'name': 'sorry',
            'type': 'gif',
            'text_part': 9,
            'help_msg': '请检查文本分段'
        }
    }
    name_msg = ''
    name_re = r'^(Placeholder'
    for item in sticker_temp.keys():
        name_msg += f'\n【{item}】'
        name_re += fr'|{item}'
    name_re += r')$'

    temp_msg = f'请输入你想要制作的表情包模板: {name_msg}'

    # 从会话状态(session.state)中获取模板名称, 如果当前不存在, 则询问用户
    get_sticker_temp = session.get(
        'sticker_temp',
        prompt=temp_msg,
        arg_filters=[
            controllers.handle_cancellation(session),
            validators.not_empty('输入不能为空~'),
            validators.match_regex(fr'{name_re}',
                                   '没有这个模板, 请重新输入~',
                                   fullmatch=True)
        ])

    if get_sticker_temp == 'Placeholder':
        get_sticker_temp = 'default'

    # 获取模板名称、类型
    sticker_temp_name = sticker_temp[get_sticker_temp]['name']
    sticker_temp_type = sticker_temp[get_sticker_temp]['type']
    sticker_temp_text_part = sticker_temp[get_sticker_temp]['text_part']

    # 判断该模板表情图片来源
    if sticker_temp_type in ['static', 'gif']:
        sticker_image_url = 'null'
    else:
        image_msg = f'请发送你想要制作的表情包的图片: '
        session.get('sticker_image',
                    prompt=image_msg,
                    arg_filters=[
                        controllers.handle_cancellation(session),
                        validators.not_empty('输入不能为空~'),
                        validators.match_regex(r'^(\[CQ\:image\,file\=)',
                                               '你发送的似乎不是图片呢, 请重新发送~',
                                               fullmatch=False)
                    ])
        if session.current_key == 'sticker_image':
            # aiocqhttp 可直接获取url
            try:
                session.state[
                    'sticker_image_url'] = session.current_arg_images[0]
            except Exception as e:
                log.logger.debug(
                    f'{__name__}: stickermacker: {e}. 在Mirai框架中运行')
                # mirai无法直接获取图片url
                # 针对mirai-native的cqhttp插件的cq码适配
                if session_type == 'group':
                    imageid = re.sub(r'^(\[CQ:image,file={)', '',
                                     session.current_arg)
                    imageid = re.sub(r'(}\.mirai\.mnimg])$', '', imageid)
                    imageid = re.sub(r'-', '', imageid)
                    imageurl = f'https://gchat.qpic.cn/gchatpic_new/0/0-0-{imageid}/0?term=2'
                    session.state['sticker_image_url'] = imageurl
                elif session_type == 'private':
                    imageid = re.sub(r'^(\[CQ:image,file=/)', '',
                                     session.current_arg)
                    imageid = re.sub(r'(\.mnimg])$', '', imageid)
                    imageurl = f'https://gchat.qpic.cn/gchatpic_new/0/{imageid}/0?term=2'
                    session.state['sticker_image_url'] = imageurl
        sticker_image_url = session.state['sticker_image_url']

    # 获取制作表情包所需文字
    if sticker_temp_text_part > 1:
        text_msg = f'请输入你想要制作的表情包的文字: \n当前模板文本分段数:【{sticker_temp_text_part}】' \
                   f'\n\n注意: 请用【#】号分割文本不同段落,不同模板适用的文字字数及段落数有所区别'
    else:
        text_msg = f'请输入你想要制作的表情包的文字: \n注意: 不同模板适用的文字字数有所区别'
    sticker_text = session.get('sticker_text',
                               prompt=text_msg,
                               arg_filters=[
                                   controllers.handle_cancellation(session),
                                   validators.not_empty('输入不能为空~')
                               ])

    if len(sticker_text.strip().split('#')) != sticker_temp_text_part:
        eg_msg = r'我就是饿死#死外边 从这里跳下去#也不会吃你们一点东西#真香'
        await session.send(
            f"表情制作失败QAQ, 文本分段数错误\n当前模板文本分段数:【{sticker_temp_text_part}】\n\n示例: \n{eg_msg}"
        )
        log.logger.warning(
            f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} 制作表情时失败, 文本分段数错误.'
        )
        return

    try:
        sticker_path = await sticker_maker_main(
            url=sticker_image_url,
            temp=sticker_temp_name,
            text=sticker_text,
            sticker_temp_type=sticker_temp_type)
        if not sticker_path:
            await session.send(
                f"表情制作失败QAQ, 请注意模板要求\n小提示:{sticker_temp[get_sticker_temp]['help_msg']}"
            )
            log.logger.warning(
                f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} 制作表情时失败.'
            )
            return
        '''
        # 发送base64图片
        sticker_b64 = await pic_2_base64(sticker_path)
        sticker_seg = MessageSegment.image(sticker_b64)
        '''

        # 直接用文件构造消息段
        sticker_seg = MessageSegment.image(f'file:///{sticker_path}')

        # 发送图片
        await session.send(sticker_seg)
        log.logger.info(
            f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} 成功制作了一个表情'
        )
    except Exception as e:
        await session.send('表情制作失败, 发生了意外的错误QAQ')
        log.logger.warning(
            f'{__name__}: 群组: {group_id}, 用户: {session.event.user_id} 试图使用命令stickermacker时发生了错误: {e}'
        )
        return