Skip to content

Commit

Permalink
Pyrofork: Move ListenerTypes to pyrogram.enums
Browse files Browse the repository at this point in the history
Signed-off-by: wulan17 <wulan17@nusantararom.org>
  • Loading branch information
wulan17 committed Dec 13, 2023
1 parent d6e0a4f commit b36a46e
Show file tree
Hide file tree
Showing 18 changed files with 76 additions and 44 deletions.
8 changes: 8 additions & 0 deletions docs/source/api/enums/ListenerTypes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ListenerTypes
=============

.. autoclass:: pyrogram.enums.ListenerTypes()
:members:

.. raw:: html
:file: ./cleanup.html
6 changes: 3 additions & 3 deletions pyrogram/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
from pyrogram.errors import (
SessionPasswordNeeded,
VolumeLocNotFound, ChannelPrivate,
BadRequest, ListenerTimeout,
BadRequest
)
from pyrogram.handlers.handler import Handler
from pyrogram.methods import Methods
Expand All @@ -57,7 +57,7 @@
pass
else:
from pyrogram.storage import MongoStorage
from pyrogram.types import User, TermsOfService, ListenerTypes
from pyrogram.types import User, TermsOfService
from pyrogram.utils import ainput
from .dispatcher import Dispatcher
from .file_id import FileId, FileType, ThumbnailSource
Expand Down Expand Up @@ -332,7 +332,7 @@ def __init__(
self.updates_watchdog_task = None
self.updates_watchdog_event = asyncio.Event()
self.last_update_time = datetime.now()
self.listeners = {listener_type: [] for listener_type in ListenerTypes}
self.listeners = {listener_type: [] for listener_type in pyrogram.enums.ListenerTypes}
self.loop = asyncio.get_event_loop()

def __enter__(self):
Expand Down
2 changes: 2 additions & 0 deletions pyrogram/enums/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .chat_member_status import ChatMemberStatus
from .chat_members_filter import ChatMembersFilter
from .chat_type import ChatType
from .listerner_types import ListenerTypes
from .message_entity_type import MessageEntityType
from .message_media_type import MessageMediaType
from .message_service_type import MessageServiceType
Expand All @@ -39,6 +40,7 @@
'ChatMemberStatus',
'ChatMembersFilter',
'ChatType',
'ListenerTypes',
'MessageEntityType',
'MessageMediaType',
'MessageServiceType',
Expand Down
32 changes: 32 additions & 0 deletions pyrogram/enums/listerner_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Pyrofork - Telegram MTProto API Client Library for Python
# Copyright (C) 2020 Cezar H. <https://github.com/usernein>
# Copyright (C) 2022-present Mayuri-Chan <https://github.com/Mayuri-Chan>
#
# This file is part of Pyrofork.
#
# Pyrofork is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrofork is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrofork. If not, see <http://www.gnu.org/licenses/>.

from enum import auto

from .auto_name import AutoName


class ListenerTypes(AutoName):
"""Listener type enumeration used in :obj:`~pyrogram.types.Client`."""

MESSAGE = auto()
"A Message"

CALLBACK_QUERY = auto()
"A CallbackQuery"
4 changes: 2 additions & 2 deletions pyrogram/handlers/callback_query_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import pyrogram

from pyrogram.utils import PyromodConfig
from pyrogram.types import ListenerTypes, CallbackQuery, Identifier, Listener
from pyrogram.types import CallbackQuery, Identifier, Listener

from .handler import Handler

Expand Down Expand Up @@ -99,7 +99,7 @@ async def check_if_has_matching_listener(
data = self.compose_data_identifier(query)

listener = client.get_listener_matching_with_data(
data, ListenerTypes.CALLBACK_QUERY
data, pyrogram.enums.ListenerTypes.CALLBACK_QUERY
)

listener_does_match = False
Expand Down
4 changes: 2 additions & 2 deletions pyrogram/handlers/message_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from typing import Callable
import pyrogram

from pyrogram.types import ListenerTypes, Message, Identifier
from pyrogram.types import Message, Identifier

from .handler import Handler

Expand Down Expand Up @@ -73,7 +73,7 @@ async def check_if_has_matching_listener(self, client: "pyrogram.Client", messag
from_user_id=[from_user_id, from_user_username],
)

listener = client.get_listener_matching_with_data(data, ListenerTypes.MESSAGE)
listener = client.get_listener_matching_with_data(data, pyrogram.enums.ListenerTypes.MESSAGE)

listener_does_match = False

Expand Down
5 changes: 2 additions & 3 deletions pyrogram/methods/pyromod/ask.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@

from pyrogram.filters import Filter
from typing import List, Optional, Union
from pyrogram.types import ListenerTypes

class Ask:
async def ask(
self: "pyrogram.Client",
chat_id: Union[Union[int, str], List[Union[int, str]]],
text: str,
filters: Optional[Filter] = None,
listener_type: ListenerTypes = ListenerTypes.MESSAGE,
listener_type: "pyrogram.enums.ListenerTypes" = pyrogram.enums.ListenerTypes.MESSAGE,
timeout: Optional[int] = None,
unallowed_click_alert: bool = True,
user_id: Union[Union[int, str], List[Union[int, str]]] = None,
Expand Down Expand Up @@ -61,7 +60,7 @@ async def ask(
filters (:obj:`~pyrogram.filters`, *optional*):
A filter to check the incoming message against.
listener_type (:obj:`~pyrogram.types.ListenerTypes`, *optional*):
listener_type (:obj:`~pyrogram.enums.ListenerTypes`, *optional*):
The type of listener to listen for.
Default to Message.
Expand Down
6 changes: 3 additions & 3 deletions pyrogram/methods/pyromod/get_listener_matching_with_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import pyrogram

from typing import Optional
from pyrogram.types import ListenerTypes, Identifier, Listener
from pyrogram.types import Identifier, Listener

class GetListenerMatchingWithData:
def get_listener_matching_with_data(
self: "pyrogram.Client",
data: Identifier,
listener_type: ListenerTypes
listener_type: "pyrogram.enums.ListenerTypes"
) -> Optional[Listener]:
"""Gets a listener that matches the given data.
Expand All @@ -36,7 +36,7 @@ def get_listener_matching_with_data(
data (:obj:`~pyrogram.types.Identifier`):
The Identifier to match agains.
listener_type (:obj:`~pyrogram.types.ListenerTypes`):
listener_type (:obj:`~pyrogram.enums.ListenerTypes`):
The type of listener to get.
Returns:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import pyrogram

from typing import Optional
from pyrogram.types import ListenerTypes, Identifier, Listener
from pyrogram.types import Identifier, Listener

class GetListenerMatchingWithIdentifierPattern:
def get_listener_matching_with_identifier_pattern(
self: "pyrogram.Client",
pattern: Identifier,
listener_type: ListenerTypes
listener_type: "pyrogram.enums.ListenerTypes"
) -> Optional[Listener]:
"""Gets a listener that matches the given identifier pattern.
Expand All @@ -40,7 +40,7 @@ def get_listener_matching_with_identifier_pattern(
pattern (:obj:`~pyrogram.types.Identifier`):
The Identifier to match agains.
listener_type (:obj:`~pyrogram.types.ListenerTypes`):
listener_type (:obj:`~pyrogram.enums.ListenerTypes`):
The type of listener to get.
Returns:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import pyrogram

from typing import List
from pyrogram.types import ListenerTypes, Identifier, Listener
from pyrogram.types import Identifier, Listener

class GetManyListenersMatchingWithData:
def get_many_listeners_matching_with_data(
self: "pyrogram.Client",
data: Identifier,
listener_type: ListenerTypes,
listener_type: "pyrogram.enums.ListenerTypes",
) -> List[Listener]:
"""Gets multiple listener that matches the given data.
Expand All @@ -36,7 +36,7 @@ def get_many_listeners_matching_with_data(
data (:obj:`~pyrogram.types.Identifier`):
The Identifier to match agains.
listener_type (:obj:`~pyrogram.types.ListenerTypes`):
listener_type (:obj:`~pyrogram.enums.ListenerTypes`):
The type of listener to get.
Returns:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import pyrogram

from typing import List
from pyrogram.types import ListenerTypes, Identifier, Listener
from pyrogram.types import Identifier, Listener

class GetManyListenersMatchingWithIdentifierPattern:
def get_many_listeners_matching_with_identifier_pattern(
self: "pyrogram.Client",
pattern: Identifier,
listener_type: ListenerTypes,
listener_type: "pyrogram.enums.ListenerTypes",
) -> List[Listener]:
"""Gets multiple listener that matches the given identifier pattern.
Expand All @@ -40,7 +40,7 @@ def get_many_listeners_matching_with_identifier_pattern(
pattern (:obj:`~pyrogram.types.Identifier`):
The Identifier to match agains.
listener_type (:obj:`~pyrogram.types.ListenerTypes`):
listener_type (:obj:`~pyrogram.enums.ListenerTypes`):
The type of listener to get.
Returns:
Expand Down
6 changes: 3 additions & 3 deletions pyrogram/methods/pyromod/listen.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
from pyrogram.errors import ListenerTimeout
from pyrogram.filters import Filter
from typing import List, Optional, Union
from pyrogram.types import ListenerTypes, Identifier, Listener
from pyrogram.types import Identifier, Listener
from pyrogram.utils import PyromodConfig

class Listen:
async def listen(
self: "pyrogram.Client",
filters: Optional[Filter] = None,
listener_type: ListenerTypes = ListenerTypes.MESSAGE,
listener_type: "pyrogram.enums.ListenerTypes" = pyrogram.enums.ListenerTypes.MESSAGE,
timeout: Optional[int] = None,
unallowed_click_alert: bool = True,
chat_id: Union[Union[int, str], List[Union[int, str]]] = None,
Expand Down Expand Up @@ -59,7 +59,7 @@ async def listen(
filters (:obj:`~pyrogram.filters`, *optional*):
A filter to check the incoming message against.
listener_type (:obj:`~pyrogram.types.ListenerTypes`, *optional*):
listener_type (:obj:`~pyrogram.enums.ListenerTypes`, *optional*):
The type of listener to listen for.
Default to Message.
Expand Down
6 changes: 3 additions & 3 deletions pyrogram/methods/pyromod/register_next_step_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@

from pyrogram.filters import Filter
from typing import Callable, List, Optional, Union
from pyrogram.types import ListenerTypes, Identifier, Listener
from pyrogram.types import Identifier, Listener

class RegisterNextStepHandler:
def register_next_step_handler(
self: "pyrogram.Client",
callback: Callable,
filters: Optional[Filter] = None,
listener_type: ListenerTypes = ListenerTypes.MESSAGE,
listener_type: "pyrogram.enums.ListenerTypes" = pyrogram.enums.ListenerTypes.MESSAGE,
unallowed_click_alert: bool = True,
chat_id: Union[Union[int, str], List[Union[int, str]]] = None,
user_id: Union[Union[int, str], List[Union[int, str]]] = None,
Expand All @@ -49,7 +49,7 @@ def register_next_step_handler(
filters (:obj:`~pyrogram.filters`, *optional*):
A filter to check the incoming message against.
listener_type (:obj:`~pyrogram.types.ListenerTypes`, *optional*):
listener_type (:obj:`~pyrogram.enums.ListenerTypes`, *optional*):
The type of listener to listen for.
Default to Message.
Expand Down
6 changes: 3 additions & 3 deletions pyrogram/methods/pyromod/stop_listening.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import pyrogram

from typing import List, Union
from pyrogram.types import ListenerTypes, Identifier
from pyrogram.types import Identifier

class StopListening:
async def stop_listening(
self: "pyrogram.Client",
listener_type: ListenerTypes = ListenerTypes.MESSAGE,
listener_type: "pyrogram.enums.ListenerTypes" = pyrogram.enums.ListenerTypes.MESSAGE,
chat_id: Union[Union[int, str], List[Union[int, str]]] = None,
user_id: Union[Union[int, str], List[Union[int, str]]] = None,
message_id: Union[int, List[int]] = None,
Expand All @@ -38,7 +38,7 @@ async def stop_listening(
Uses :meth:`~pyrogram.Client.get_many_listeners_matching_with_identifier_pattern`.
Parameters:
listener_type (:obj:`~pyrogram.types.ListenerTypes`, *optional*):
listener_type (:obj:`~pyrogram.enums.ListenerTypes`, *optional*):
The type of listener to stop listening for.
Default to Message.
Expand Down
2 changes: 1 addition & 1 deletion pyrogram/types/messages_and_media/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ async def wait_for_click(

return await self._client.listen(
(self.chat.id, from_user_id, self.id),
listener_type=types.ListenerTypes.CALLBACK_QUERY,
listener_type=pyrogram.enums.ListenerTypes.CALLBACK_QUERY,
timeout=timeout,
filters=filters,
unallowed_click_alert=alert,
Expand Down
3 changes: 1 addition & 2 deletions pyrogram/types/pyromod/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .identifier import Identifier
from .listener import Listener
from .listener_types import ListenerTypes

__all__ = ["Identifier", "Listener", "ListenerTypes"]
__all__ = ["Identifier", "Listener"]
6 changes: 2 additions & 4 deletions pyrogram/types/pyromod/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@
import pyrogram

from .identifier import Identifier
from .listener_types import ListenerTypes


@dataclass
class Listener:
"""Designed to manage and handle different types of listeners used in pyromod.
It enables you to wait for specific events like messages or callback queries and provides mechanisms for defining the conditions and filters that trigger these listeners.
Parameters:
listener_type (:obj:`~pyrogram.types.ListenerTypes`):
listener_type (:obj:`~pyrogram.enums.ListenerTypes`):
The type of listener that specifies the event you want to listen for.
It can be either a “message” or a “callback_query.”
Expand All @@ -55,7 +53,7 @@ class Listener:
callback (``Callable``, *optional*):
The callback to call when the listener is fulfilled.
"""
listener_type: ListenerTypes
listener_type: pyrogram.enums.ListenerTypes
filters: "pyrogram.filters.Filter"
unallowed_click_alert: bool
identifier: Identifier
Expand Down
6 changes: 0 additions & 6 deletions pyrogram/types/pyromod/listener_types.py

This file was deleted.

0 comments on commit b36a46e

Please sign in to comment.