示例#1
0
    def deco(func) -> Callable:
        @wraps(func)
        async def wrapper(session: RequestSession):
            funcname = func.__module__ + '.' + func.__name__
            try:
                await func(session, bot)
                if use_default_infolog:
                    logger.info(
                        f'<Request> Call function: {funcname}, request information: {session.event}'
                    )
            except Warning as w:
                logger.warning(
                    f'<Request> Warning {type(w)} occured while {funcname} is running.'
                )
            except ApiNotAvailable as a:
                logger.error(
                    f'<Request> Error {type(a)} occured while {funcname} is running.'
                )
            except ActionFailed as a:
                logger.error(
                    f'<Request> Error {type(a)} occured while {funcname} is running, retcode = {a.retcode}.'
                )
            except Exception as e:
                logger.exception(
                    f'<Request> Error {type(e)} occured while {funcname} is running.'
                )

        return nonebot.on_request(*event)(debuglog(logger)(wrapper))
示例#2
0
    def deco(func) -> Callable:
        @wraps(func)
        async def wrapper(event: Event):
            if event['user_id'] in BLACKLIST['user']:
                return
            if event['message_type'] == 'group' and event[
                    'group_id'] in BLACKLIST['group']:
                return
            if checkfunc is not None:
                if not ((await checkfunc(event) if asyncio.iscoroutinefunction(
                        checkfunc) else checkfunc(event))):
                    return
            if wait_for is not None:
                count = 0
                while not (
                    (await wait_for()) if asyncio.iscoroutinefunction(wait_for)
                        else wait_for()):
                    await asyncio.sleep(1)
                    count += 1
                    if count >= _wait_for_maxtime:
                        raise WaitForTimeoutError
            funcname = func.__module__ + '.' + func.__name__
            if funcname in _cooldown_functions[event['self_id']].keys():
                return
            try:
                retcode = await func(event, bot)
                if use_default_infolog and retcode:
                    if event['message_type'] == 'group':
                        logger.info(
                            f'<Message> Group {event["group_id"]} user {event["user_id"]} call {funcname} successfully'
                        )
                    else:
                        logger.info(
                            f'<Message> Private user {event["user_id"]} call {funcname} successfully'
                        )
            except Warning as w:
                logger.warning(
                    f'<Message> Warning {type(w)} occured while {funcname} is running.'
                )
            except (ApiNotAvailable, RetryExhaustedError) as a:
                logger.error(
                    f'<Message> Error {type(a)} occured while {funcname} is running.'
                )
            except ActionFailed as a:
                logger.error(
                    f'<Message> Error {type(a)} occured while {funcname} is running, retcode = {a.retcode}.'
                )
            except Exception as e:
                logger.exception(
                    f'<Message> Error {type(e)} occured while {funcname} is running.'
                )
            if cooldown > 0:
                if funcname not in _cooldown_functions[event['self_id']]:
                    _cooldown_functions[event['self_id']][funcname] = cooldown

        return bot.on_message(*event)(debuglog(logger)(wrapper))
示例#3
0
    def deco(func) -> Callable:
        @wraps(func)
        async def wrapper(event: Event):
            if checkfunc is not None:
                if not ((await checkfunc(event) if asyncio.iscoroutinefunction(
                        checkfunc) else checkfunc(event))):
                    return
            if wait_for is not None:
                count = 0
                while not (
                    (await wait_for()) if asyncio.iscoroutinefunction(wait_for)
                        else wait_for()):
                    await asyncio.sleep(1)
                    count += 1
                    if count >= _wait_for_maxtime:
                        raise WaitForTimeoutError
            funcname = func.__module__ + '.' + func.__name__
            try:
                await func(event, bot)
                if use_default_infolog:
                    logger.info(
                        f'<Connect> Call function: {funcname}, connect information: {event}'
                    )
            except Warning as w:
                logger.warning(
                    f'<Connect> Warning {type(w)} occured while {funcname} is running.'
                )
            except ApiNotAvailable as a:
                logger.error(
                    f'<Connect> Error {type(a)} occured while {funcname} is running.'
                )
            except ActionFailed as a:
                logger.error(
                    f'<Scheduler> Error {type(a)} occured while {funcname} is running, retcode = {a.retcode}.'
                )
            except Exception as e:
                logger.exception(
                    f'<Connect> Error {type(e)} occured while {funcname} is running.'
                )

        return nonebot.on_websocket_connect(debuglog(logger)(wrapper))
示例#4
0
    def deco(func) -> Callable:
        @wraps(func)
        async def wrapper(*args, **kwargs):
            if wait_for is not None:
                count = 0
                while not (
                    (await wait_for()) if asyncio.iscoroutinefunction(wait_for)
                        else wait_for()):
                    await asyncio.sleep(1)
                    count += 1
                    if count >= _wait_for_maxtime:
                        raise WaitForTimeoutError
            funcname = func.__module__ + '.' + func.__name__
            try:
                retcode = await func(*args, **kwargs)
                if use_default_infolog and retcode:
                    logger.info(
                        f'<Scheduler> Azusa call {funcname} successfully')
            except Warning as w:
                logger.warning(
                    f'<Scheduler> Warning {type(w)} occured while {funcname} is running.'
                )
            except ApiNotAvailable as a:
                logger.error(
                    f'<Scheduler> Error {type(a)} occured while {funcname} is running.'
                )
            except ActionFailed as a:
                logger.error(
                    f'<Scheduler> Error {type(a)} occured while {funcname} is running, retcode = {a.retcode}.'
                )
            except Exception as e:
                logger.exception(
                    f'<Scheduler> Error {type(e)} occured while {funcname} is running.'
                )

        return nonebot.scheduler.scheduled_job(
            trigger, args, kwargs, **trigger_args)(debuglog(logger)(wrapper))
示例#5
0
    def deco(func) -> Callable:
        @wraps(func)
        async def wrapper(session: CommandSession):
            if session.event['user_id'] in BLACKLIST['user']:
                return
            if session.event['message_type'] == 'group' and session.event[
                    'group_id'] in BLACKLIST['group']:
                return
            if checkfunc is not None:
                if not ((await checkfunc(session)
                         if asyncio.iscoroutinefunction(checkfunc) else
                         checkfunc(session))):
                    return
            if wait_for is not None:
                count = 0
                while not (
                    (await wait_for()) if asyncio.iscoroutinefunction(wait_for)
                        else wait_for()):
                    await asyncio.sleep(1)
                    count += 1
                    if count >= _wait_for_maxtime:
                        raise WaitForTimeoutError
            funcname = func.__module__ + '.' + func.__name__
            if funcname in _cooldown_functions[session.self_id].keys():
                return
            try:
                await func(session, bot)
                if use_default_infolog:
                    if session.event['message_type'] == 'group':
                        logger.info(
                            f'<Command> Group {session.event["group_id"]} user {session.event["user_id"]} call {funcname} successfully'
                        )
                    else:
                        logger.info(
                            f'<Command> Private user {session.event["user_id"]} call {funcname} successfully'
                        )
            except (_PauseException, _FinishException, SwitchException) as e:
                raise e
            except Warning as w:
                logger.warning(
                    f'<Command> Warning {type(w)} occured while {funcname} is running.'
                )
            except (ApiNotAvailable, RetryExhaustedError) as a:
                logger.error(
                    f'<Command> Error {type(a)} occured while {funcname} is running.'
                )
            except ActionFailed as a:
                logger.error(
                    f'<Command> Error {type(a)} occured while {funcname} is running, retcode = {a.retcode}.'
                )
            except Exception as e:
                logger.exception(
                    f'<Command> Error {type(e)} occured while {funcname} is running.'
                )
            if cooldown > 0:
                if funcname not in _cooldown_functions[session.self_id]:
                    _cooldown_functions[session.self_id][funcname] = cooldown

        return nonebot.on_command(
            name,
            aliases=aliases,
            permission=permission,
            only_to_me=only_to_me,
            privileged=privileged,
            shell_like=shell_like,
        )(debuglog(logger)(wrapper))