U
    g                     @   s   d Z ddlZddlmZmZmZmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZmZ ddlmZ ddlmZ dd	lmZmZ erdd
lmZ e
dZG dd deeeef ZdS )z-This module contains the PrefixHandler class.    N)	TYPE_CHECKINGAnyDict	FrozenSetListOptionalTupleTypeVarUnion)Update)DEFAULT_TRUE)SCTDVType)filters)BaseHandler)CCTHandlerCallback)ApplicationRTc                       s   e Zd ZdZdZdefdee ee ee	e
ef eej ee d fddZeeeeeee eeeeeef f  f f  dd	d
Ze
e	deeeeee ee f f  ddddZ  ZS )PrefixHandlera  Handler class to handle custom prefix commands.

    This is an intermediate handler between :class:`MessageHandler` and :class:`CommandHandler`.
    It supports configurable commands with the same options as :class:`CommandHandler`. It will
    respond to every combination of :paramref:`prefix` and :paramref:`command`.
    It will add a :obj:`list` to the :class:`CallbackContext` named :attr:`CallbackContext.args`,
    containing a list of strings, which is the text following the command split on single or
    consecutive whitespace characters.

    Examples:

        Single prefix and command:

        .. code:: python

            PrefixHandler("!", "test", callback)  # will respond to '!test'.

        Multiple prefixes, single command:

        .. code:: python

            PrefixHandler(["!", "#"], "test", callback)  # will respond to '!test' and '#test'.

        Multiple prefixes and commands:

        .. code:: python

            PrefixHandler(
                ["!", "#"], ["test", "help"], callback
            )  # will respond to '!test', '#test', '!help' and '#help'.


    By default, the handler listens to messages as well as edited messages. To change this behavior
    use :attr:`~filters.UpdateType.EDITED_MESSAGE <telegram.ext.filters.UpdateType.EDITED_MESSAGE>`

    Note:
        * :class:`PrefixHandler` does *not* handle (edited) channel posts.

    Warning:
        When setting :paramref:`block` to :obj:`False`, you cannot rely on adding custom
        attributes to :class:`telegram.ext.CallbackContext`. See its docs for more info.

    .. versionchanged:: 20.0

        * :class:`PrefixHandler` is no longer a subclass of :class:`CommandHandler`.
        * Removed the attributes ``command`` and ``prefix``. Instead, the new :attr:`commands`
          contains all commands that this handler listens to as a :class:`frozenset`, which
          includes the prefixes.
        * Updating the prefixes and commands this handler listens to is no longer possible.

    Args:
        prefix (:obj:`str` | Collection[:obj:`str`]):
            The prefix(es) that will precede :paramref:`command`.
        command (:obj:`str` | Collection[:obj:`str`]):
            The command or list of commands this handler should listen for. Case-insensitive.
        callback (:term:`coroutine function`): The callback function for this handler. Will be
            called when :meth:`check_update` has determined that an update should be processed by
            this handler. Callback signature::

                async def callback(update: Update, context: CallbackContext)

            The return value of the callback is usually ignored except for the special case of
            :class:`telegram.ext.ConversationHandler`.
        filters (:class:`telegram.ext.filters.BaseFilter`, optional): A filter inheriting from
            :class:`telegram.ext.filters.BaseFilter`. Standard filters can be found in
            :mod:`telegram.ext.filters`. Filters can be combined using bitwise
            operators (``&`` for :keyword:`and`, ``|`` for :keyword:`or`, ``~`` for :keyword:`not`)
        block (:obj:`bool`, optional): Determines whether the return value of the callback should
            be awaited before processing the next handler in
            :meth:`telegram.ext.Application.process_update`. Defaults to :obj:`True`.

            .. seealso:: :wiki:`Concurrency`

    Attributes:
        commands (FrozenSet[:obj:`str`]): The commands that this handler will listen for, i.e. the
            combinations of :paramref:`prefix` and :paramref:`command`.
        callback (:term:`coroutine function`): The callback function for this handler.
        filters (:class:`telegram.ext.filters.BaseFilter`): Optional. Only allow updates with these
            Filters.
        block (:obj:`bool`): Determines whether the return value of the callback should be
            awaited before processing the next handler in
            :meth:`telegram.ext.Application.process_update`.

    )commandsr   NzPrefixHandler[CCT, RT])selfprefixcommandcallbackr   blockc                    s   t  j||d t|tr$| hndd |D }t|trF| hndd |D }tdd t||D | _|d k	r||nt	j
j| _d S )N)r   r   c                 S   s   h | ]}|  qS  lower.0xr   r   H/tmp/pip-unpacked-wheel-swnnwir2/telegram/ext/_handlers/prefixhandler.py	<setcomp>   s     z)PrefixHandler.__init__.<locals>.<setcomp>c                 S   s   h | ]}|  qS r   r   r   r   r   r"   r#      s     c                 s   s   | ]\}}|| V  qd S )Nr   )r    pcr   r   r"   	<genexpr>   s    z)PrefixHandler.__init__.<locals>.<genexpr>)super__init__
isinstancestrr   	frozenset	itertoolsproductr   filters_moduleZ
UpdateTypeZMESSAGESr   )r   r   r   r   r   r   prefixesr   	__class__r   r"   r(   }   s    ""

zPrefixHandler.__init__)updatereturnc                 C   sd   t |tr`|jr`|j}|jr`|j }|d  | jkr<dS | j|}|r\|dd |fS dS dS )a  Determines whether an update should be passed to this handler's :attr:`callback`.

        Args:
            update (:class:`telegram.Update` | :obj:`object`): Incoming update.

        Returns:
            :obj:`list`: The list of args for the handler.

        r   N   F)	r)   r   Zeffective_messagetextsplitr   r   r   check_update)r   r2   messageZ	text_listZfilter_resultr   r   r"   r7      s    
zPrefixHandler.check_updatez)Application[Any, CCT, Any, Any, Any, Any])contextr2   applicationcheck_resultr3   c                 C   s4   t |tr0|d |_t |d tr0||d  dS )zAdd text after the command to :attr:`CallbackContext.args` as list, split on single
        whitespaces and add output of data filters to :attr:`CallbackContext` as well.
        r   r4   N)r)   tupleargsdictr2   )r   r9   r2   r:   r;   r   r   r"   collect_additional_context   s    


z(PrefixHandler.collect_additional_context)__name__
__module____qualname____doc__	__slots__r   r   r*   r   r   r   r   r   r.   Z
BaseFilterr   boolr(   objectr
   r   r   r   r   r7   r?   __classcell__r   r   r0   r"   r   $   s*   V.r   )rC   r,   typingr   r   r   r   r   r   r   r	   r
   Ztelegramr   Ztelegram._utils.defaultvaluer   Ztelegram._utils.typesr   r   Ztelegram.extr   r.   Z"telegram.ext._handlers.basehandlerr   Ztelegram.ext._utils.typesr   r   r   r   r   r   r   r   r"   <module>   s   ,