예제 #1
0
    async def run(self, bot: "Bot", event: "Event", state: T_State):
        b_t = current_bot.set(bot)
        e_t = current_event.set(event)
        try:
            # Refresh preprocess state
            state_ = await self._default_state_factory(
                bot, event) if self._default_state_factory else self.state
            state_.update(state)

            for _ in range(len(self.handlers)):
                handler = self.handlers.pop(0)
                await self.run_handler(handler, bot, event, state_)

        except RejectedException:
            self.handlers.insert(0, handler)  # type: ignore
            Matcher.new(
                "message",
                Rule(),
                USER(event.get_session_id(),
                     perm=self.permission),  # type:ignore
                self.handlers,
                temp=True,
                priority=0,
                block=True,
                module=self.module,
                default_state=self.state,
                expire_time=datetime.now() + bot.config.session_expire_timeout)
        except PausedException:
            Matcher.new(
                "message",
                Rule(),
                USER(event.get_session_id(),
                     perm=self.permission),  # type:ignore
                self.handlers,
                temp=True,
                priority=0,
                block=True,
                module=self.module,
                default_state=self.state,
                expire_time=datetime.now() + bot.config.session_expire_timeout)
        except FinishedException:
            pass
        except StopPropagation:
            self.block = True
        finally:
            logger.info(f"Matcher {self} running complete")
            current_bot.reset(b_t)
            current_event.reset(e_t)
예제 #2
0
    async def run(self, bot: Bot, event: Event, state: dict):
        b_t = current_bot.set(bot)
        e_t = current_event.set(event)
        try:
            # Refresh preprocess state
            self.state.update(state)

            for _ in range(len(self.handlers)):
                handler = self.handlers.pop(0)
                annotation = typing.get_type_hints(handler)
                BotType = annotation.get("bot")
                if BotType and inspect.isclass(BotType) and not isinstance(
                        bot, BotType):
                    continue
                await handler(bot, event, self.state)

        except RejectedException:
            self.handlers.insert(0, handler)  # type: ignore
            Matcher.new(
                self.type,
                Rule(),
                USER(event.user_id, perm=self.permission),  # type:ignore
                self.handlers,
                temp=True,
                priority=0,
                block=True,
                module=self.module,
                default_state=self.state,
                expire_time=datetime.now() + bot.config.session_expire_timeout)
        except PausedException:
            Matcher.new(
                self.type,
                Rule(),
                USER(event.user_id, perm=self.permission),  # type:ignore
                self.handlers,
                temp=True,
                priority=0,
                block=True,
                module=self.module,
                default_state=self.state,
                expire_time=datetime.now() + bot.config.session_expire_timeout)
        except FinishedException:
            pass
        finally:
            logger.info(f"Matcher {self} running complete")
            current_bot.reset(b_t)
            current_event.reset(e_t)
예제 #3
0
    async def run(self, bot: Bot, event: Event, state: dict):
        try:
            # Refresh preprocess state
            self.state.update(state)

            for _ in range(len(self.handlers)):
                handler = self.handlers.pop(0)
                annotation = typing.get_type_hints(handler)
                BotType = annotation.get("bot")
                if BotType and not isinstance(bot, BotType):
                    continue
                await handler(bot, event, self.state)

        except RejectedException:
            self.handlers.insert(0, handler)  # type: ignore
            matcher = Matcher.new(
                self.type,
                self.rule,
                USER(event.user_id, perm=self.permission),  # type:ignore
                self.handlers,
                temp=True,
                priority=0,
                default_state=self.state,
                expire_time=datetime.now() + bot.config.session_expire_timeout)
            matchers[0].append(matcher)
            return
        except PausedException:
            matcher = Matcher.new(
                self.type,
                self.rule,
                USER(event.user_id, perm=self.permission),  # type:ignore
                self.handlers,
                temp=True,
                priority=0,
                default_state=self.state,
                expire_time=datetime.now() + bot.config.session_expire_timeout)
            matchers[0].append(matcher)
            return
        except FinishedException:
            return
예제 #4
0
파일: matcher.py 프로젝트: SK-415/nonebot2
    async def run(self, bot: "Bot", event: "Event", state: T_State):
        b_t = current_bot.set(bot)
        e_t = current_event.set(event)
        s_t = current_state.set(self.state)
        try:
            # Refresh preprocess state
            self.state = await self._default_state_factory(
                bot, event) if self._default_state_factory else self.state
            self.state.update(state)

            while self.handlers:
                handler = self.handlers.pop(0)
                logger.debug(f"Running handler {handler}")
                await handler(self, bot, event, self.state)

        except RejectedException:
            self.handlers.insert(0, handler)  # type: ignore
            updater = self.__class__._default_type_updater
            if updater:
                type_ = await updater(
                    bot,
                    event,
                    self.state,  # type: ignore
                    self.type)
            else:
                type_ = "message"

            updater = self.__class__._default_permission_updater
            if updater:
                permission = await updater(
                    bot,
                    event,
                    self.state,  # type: ignore
                    self.permission)
            else:
                permission = USER(event.get_session_id(), perm=self.permission)

            Matcher.new(
                type_,
                Rule(),
                permission,
                self.handlers,
                temp=True,
                priority=0,
                block=True,
                module=self.module,
                expire_time=datetime.now() + bot.config.session_expire_timeout,
                default_state=self.state,
                default_parser=self.__class__._default_parser,
                default_type_updater=self.__class__._default_type_updater,
                default_permission_updater=self.__class__.
                _default_permission_updater)
        except PausedException:
            updater = self.__class__._default_type_updater
            if updater:
                type_ = await updater(
                    bot,
                    event,
                    self.state,  # type: ignore
                    self.type)
            else:
                type_ = "message"

            updater = self.__class__._default_permission_updater
            if updater:
                permission = await updater(
                    bot,
                    event,
                    self.state,  # type: ignore
                    self.permission)
            else:
                permission = USER(event.get_session_id(), perm=self.permission)

            Matcher.new(
                type_,
                Rule(),
                permission,
                self.handlers,
                temp=True,
                priority=0,
                block=True,
                module=self.module,
                expire_time=datetime.now() + bot.config.session_expire_timeout,
                default_state=self.state,
                default_parser=self.__class__._default_parser,
                default_type_updater=self.__class__._default_type_updater,
                default_permission_updater=self.__class__.
                _default_permission_updater)
        except FinishedException:
            pass
        except StopPropagation:
            self.block = True
        finally:
            logger.info(f"Matcher {self} running complete")
            current_bot.reset(b_t)
            current_event.reset(e_t)
            current_state.reset(s_t)
예제 #5
0
import asyncio

from nonebot import on_message
from nonebot.typing import T_State
from nonebot.permission import USER
from nonebot.adapters import Bot, Event

a = on_message(priority=0, permission=USER("123123123"), temp=True)


@a.handle()
async def test_a(bot: Bot, event: Event, state: T_State):
    print("======== A Received ========")
    print("======== A Running Completed ========")


b = on_message(priority=0, permission=USER("123456789"), temp=True)


@b.handle()
async def test_b(bot: Bot, event: Event, state: T_State):
    print("======== B Received ========")
    await asyncio.sleep(10)
    print("======== B Running Completed ========")


c = on_message(priority=0, permission=USER("1111111111"))


@c.handle()
async def test_c(bot: Bot, event: Event, state: T_State):
예제 #6
0
import asyncio

from nonebot import on_message
from nonebot.permission import USER
from nonebot.typing import Bot, Event

a = on_message(priority=0, permission=USER(123123123), temp=True)


@a.handle()
async def test_a(bot: Bot, event: Event, state: dict):
    print("======== A Received ========")
    print("======== A Running Completed ========")


b = on_message(priority=0, permission=USER(123456789), temp=True)


@b.handle()
async def test_b(bot: Bot, event: Event, state: dict):
    print("======== B Received ========")
    await asyncio.sleep(10)
    print("======== B Running Completed ========")


c = on_message(priority=0, permission=USER(1111111111))


@c.handle()
async def test_c(bot: Bot, event: Event, state: dict):
    print("======== C Received ========")
예제 #7
0
 async def update_permission(self, bot: Bot, event: Event) -> Permission:
     updater = self.__class__._default_permission_updater
     if not updater:
         return USER(event.get_session_id(), perm=self.permission)
     return await updater(bot=bot, event=event, state=self.state, matcher=self)