<filename>src/plugins/group_manager/data_source.py
import asyncio
from typing import Optional, Tuple

from nonebot.adapters.cqhttp import Bot, GroupRequestEvent, PrivateMessageEvent
from nonebot.adapters.cqhttp.message import Message
from nonebot.rule import Rule
from nonebot.typing import T_State
from src.modules.ban_list import BanList
from src.modules.exam_list import ExamList
from src.modules.token_info import TokenInfo
from src.utils.config import config

from .model import ExamLoader

Exam = ExamLoader()
'''试题库'''


def check_group(group_id: int):
    '''
    检查群id
    '''
    async def _check_group(bot: "Bot", event: "GroupRequestEvent", state: T_State) -> bool:
        if isinstance(event, GroupRequestEvent):
            return (event.group_id == group_id)
        return False

    return Rule(_check_group)


def check_user():
    '''检查是否在考试'''
    async def _check_user(bot: "Bot", event: "PrivateMessageEvent", state: T_State) -> bool:
        user_id = event.user_id
        return await ExamList.check_user(user_id)

    return Rule(_check_user)


async def check_banned(user_id: int) -> bool:
    '''检查是否被ban'''
    return await BanList.check_someone(user_id)


async def check_token(user_id: int, token: str) -> Tuple[Optional[bool], str]:
    '''检查token'''
    # 验证绑定用户
    return await TokenInfo.check_token(user_id, token)


async def get_user(token: str) -> Optional[int]:
    '''获取token绑定的id'''
    return await TokenInfo.get_token_user(token)


async def ban_someone(user_id: int, keep_time: int, count_flag: bool):
    '''ban掉一位用户'''
    await BanList.ban_someone(user_id, keep_time, count_flag)


async def create_one_exam(user_id: int) -> bool:
    '''创建试卷记录'''
    return await ExamList.create_one_exam(user_id)


async def get_question(user_id: int, is_next: bool) -> Message:
    '''
    :说明
        获取试题

    :参数
        * user_id：用户QQ
        * is_next：是否加载下一道
    '''
    index = await ExamList.send_question(user_id, is_next)
    return Exam.send_question(index)


async def timer_shoutdown(bot: Bot, user_id: int):
    '''
    :说明
        计划任务，回答到期，删除试卷并踢出角色

    :参数
        * bot：机器人类
        * user_id：用户id
    '''
    # 删除试卷
    await ExamList.remove_one_exam(user_id)
    # ban人
    keep_time = config['outtime-ban']
    await BanList.ban_someone(user_id, keep_time, False)
    msg = "未在规定时间内回答问题，问卷结束！"
    await bot.send_private_msg(user_id=user_id, message=msg)
    # 删除好友
    await asyncio.sleep(1)
    await bot.call_api(api="delete_friend", id=user_id)


async def answer_exam(user_id: int, answer: str) -> bool:
    '''
    :说明
        回答问题并加分

    :参数
        * user_id：用户id
        * answer：回答的答案

    :返回
        * bool：是否最后一题
    '''
    index = await ExamList.get_exam_index(user_id)
    score = Exam.get_source(index, answer)
    # 加分
    await ExamList.add_exam_score(user_id, score)
    # 判断是否最后一题
    return (index == Exam.max_index)


async def get_exam_score(user_id: int) -> Optional[int]:
    '''获取考试分数'''
    return await ExamList.get_exam_score(user_id)


async def exam_passed(user_id: int) -> str:
    '''通过考试，返回绑定的token'''
    # 删除考试记录
    await ExamList.remove_one_exam(user_id)
    return await TokenInfo.create_token(user_id, True)


async def get_ban_times(user_id: int) -> int:
    '''获取被ban次数'''
    return await BanList.get_ban_times(user_id)

<filename>src/plugins/group_manager/data_source.py
import asyncio
from typing import Optional, Tuple

from nonebot.adapters.cqhttp import Bot, GroupRequestEvent, PrivateMessageEvent
from nonebot.adapters.cqhttp.message import Message
from nonebot.rule import Rule
from nonebot.typing import T_State
from src.modules.ban_list import BanList
from src.modules.exam_list import ExamList
from src.modules.token_info import TokenInfo
from src.utils.config import config

from .model import ExamLoader

Exam = ExamLoader()
'''试题库'''


def check_group(group_id: int):
    '''
    检查群id
    '''
    async def _check_group(bot: "Bot", event: "GroupRequestEvent", state: T_State) -> bool:
        if isinstance(event, GroupRequestEvent):
            return (event.group_id == group_id)
        return False

    return Rule(_check_group)


def check_user():
    '''检查是否在考试'''
    async def _check_user(bot: "Bot", event: "PrivateMessageEvent", state: T_State) -> bool:
        user_id = event.user_id
        return await ExamList.check_user(user_id)

    return Rule(_check_user)


async def check_banned(user_id: int) -> bool:
    '''检查是否被ban'''
    return await BanList.check_someone(user_id)


async def check_token(user_id: int, token: str) -> Tuple[Optional[bool], str]:
    '''检查token'''
    # 验证绑定用户
    return await TokenInfo.check_token(user_id, token)


async def get_user(token: str) -> Optional[int]:
    '''获取token绑定的id'''
    return await TokenInfo.get_token_user(token)


async def ban_someone(user_id: int, keep_time: int, count_flag: bool):
    '''ban掉一位用户'''
    await BanList.ban_someone(user_id, keep_time, count_flag)


async def create_one_exam(user_id: int) -> bool:
    '''创建试卷记录'''
    return await ExamList.create_one_exam(user_id)


async def get_question(user_id: int, is_next: bool) -> Message:
    '''
    :说明
        获取试题

    :参数
        * user_id：用户QQ
        * is_next：是否加载下一道
    '''
    index = await ExamList.send_question(user_id, is_next)
    return Exam.send_question(index)


async def timer_shoutdown(bot: Bot, user_id: int):
    '''
    :说明
        计划任务，回答到期，删除试卷并踢出角色

    :参数
        * bot：机器人类
        * user_id：用户id
    '''
    # 删除试卷
    await ExamList.remove_one_exam(user_id)
    # ban人
    keep_time = config['outtime-ban']
    await BanList.ban_someone(user_id, keep_time, False)
    msg = "未在规定时间内回答问题，问卷结束！"
    await bot.send_private_msg(user_id=user_id, message=msg)
    # 删除好友
    await asyncio.sleep(1)
    await bot.call_api(api="delete_friend", id=user_id)


async def answer_exam(user_id: int, answer: str) -> bool:
    '''
    :说明
        回答问题并加分

    :参数
        * user_id：用户id
        * answer：回答的答案

    :返回
        * bool：是否最后一题
    '''
    index = await ExamList.get_exam_index(user_id)
    score = Exam.get_source(index, answer)
    # 加分
    await ExamList.add_exam_score(user_id, score)
    # 判断是否最后一题
    return (index == Exam.max_index)


async def get_exam_score(user_id: int) -> Optional[int]:
    '''获取考试分数'''
    return await ExamList.get_exam_score(user_id)


async def exam_passed(user_id: int) -> str:
    '''通过考试，返回绑定的token'''
    # 删除考试记录
    await ExamList.remove_one_exam(user_id)
    return await TokenInfo.create_token(user_id, True)


async def get_ban_times(user_id: int) -> int:
    '''获取被ban次数'''
    return await BanList.get_ban_times(user_id)

试题库 检查群id 检查是否在考试 检查是否被ban 检查token # 验证绑定用户 获取token绑定的id ban掉一位用户 创建试卷记录 :说明         获取试题      :参数         * user_id：用户QQ         * is_next：是否加载下一道 :说明         计划任务，回答到期，删除试卷并踢出角色      :参数         * bot：机器人类         * user_id：用户id # 删除试卷 # ban人 # 删除好友 :说明         回答问题并加分      :参数         * user_id：用户id         * answer：回答的答案      :返回         * bool：是否最后一题 # 加分 # 判断是否最后一题 获取考试分数 通过考试，返回绑定的token # 删除考试记录 获取被ban次数