async def on_process_message(self, message: types.Message):
        """
        Этот обработчик вызывается, когда диспетчер получает сообщение
        """
        # Получить текущий обработчик
        handler = context.get_value('handler')

        # Получить диспетчер из контекста
        dispatcher = ctx.get_dispatcher()

        # Если обработчик был настроен, получить ограничение скорости и ключ от обработчика
        if handler:
            limit = getattr(handler, 'throttling_rate_limit', self.rate_limit)
            key = getattr(handler, 'throttling_key',
                          f"{self.prefix}_{handler.__name__}")
        else:
            limit = self.rate_limit
            key = f"{self.prefix}_message"

        # Использовать Dispatcher.throttle метод
        try:
            await dispatcher.throttle(key, rate=limit)
        except Throttled as t:
            response = await bot.get_chat_member(message.chat.id,
                                                 message.from_user.id)
            if response.status not in admins:
                # Выполнять действия
                await self.message_throttled(message, t)

                # Отменить текущий обработчик
                raise CancelHandler()
Example #2
0
    async def on_process_message(self, message: types.Message):
        """
        This handler is called when dispatcher receives a message

        :param message:
        """
        # Get current handler
        handler = context.get_value('handler')

        # Get dispatcher from context
        dispatcher = ctx.get_dispatcher()

        # If handler was configured, get rate limit and key from handler
        if handler:
            limit = getattr(handler, 'throttling_rate_limit', self.rate_limit)
            key = getattr(handler, 'throttling_key',
                          f"{self.prefix}_{handler.__name__}")
        else:
            limit = self.rate_limit
            key = f"{self.prefix}_message"

        # Use Dispatcher.throttle method.
        try:
            await dispatcher.throttle(key, rate=limit)
        except Throttled as t:
            # Execute action
            await self.message_throttled(message, t)

            # Cancel current handler
            raise CancelHandler()
Example #3
0
    async def message_throttled(self, message: types.Message,
                                throttled: Throttled):
        """
        Notify user only on first exceed and notify about unlocking only on last exceed

        :param message:
        :param throttled:
        """
        from engine import moder

        chat = message.chat
        user = message.from_user
        handler = context.get_value('handler')
        dispatcher = ctx.get_dispatcher()

        if handler:
            key = getattr(handler, 'throttling_key',
                          f"{self.prefix}_{handler.__name__}")
        else:
            key = f"{self.prefix}_message"

        # Calculate how many time is left till the block ends
        delta = throttled.rate - throttled.delta

        # Prevent flooding
        if throttled.exceeded_count <= 2:
            await moder.restrict_user(chat.id, user.id, FLOOD_MUTE_TIME)
            await message.reply(FLOOD_LOCK_MESSAGE)

        # Sleep.
        await asyncio.sleep(delta)

        # Check lock status
        thr = await dispatcher.check_key(key)
    async def message_throttled(self, message: types.Message,
                                throttled: Throttled):
        """
        Notify user only on first exceed and notify about unlocking only on last exceed

        :param message:
        :param throttled:
        """
        handler = context.get_value('handler')
        dispatcher = ctx.get_dispatcher()
        if handler:
            key = getattr(handler, 'throttling_key',
                          f"{self.prefix}_{handler.__name__}")
        else:
            key = f"{self.prefix}_message"

        # Calculate how many time left to the end of block.
        delta = throttled.rate - throttled.delta

        # Prevent flooding
        if throttled.exceeded_count <= 2:
            await message.reply('Too many requests! ')

        # Sleep.
        await asyncio.sleep(delta)

        # Check lock status
        thr = await dispatcher.check_key(key)

        # If current message is not last with current key - do not send message
        if thr.exceeded_count == throttled.exceeded_count:
            await message.reply('Unlocked.')