Skip to content

Commit

Permalink
add filters at first
Browse files Browse the repository at this point in the history
  • Loading branch information
z44d committed Jul 14, 2024
1 parent f3f8594 commit ebab462
Showing 1 changed file with 60 additions and 22 deletions.
82 changes: 60 additions & 22 deletions tgram/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def decorator(func: Callable) -> Callable:
def on_message(self=None, filters: Filter = None):
def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func, type=Handlers.MESSAGE, filters=filters or all
callback=func,
type=Handlers.MESSAGE,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand All @@ -41,7 +43,9 @@ def decorator(func: Callable) -> Callable:
def on_edited_message(self=None, filters: Filter = None):
def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func, type=Handlers.EDITED_MESSAGE, filters=filters or all
callback=func,
type=Handlers.EDITED_MESSAGE,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand All @@ -58,7 +62,9 @@ def decorator(func: Callable) -> Callable:
def on_channel_post(self=None, filters: Filter = None):
def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func, type=Handlers.CHANNEL_POST, filters=filters or all
callback=func,
type=Handlers.CHANNEL_POST,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand All @@ -75,7 +81,9 @@ def decorator(func: Callable) -> Callable:
def on_edited_channel_post(self=None, filters: Filter = None):
def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func, type=Handlers.EDITED_CHANNEL_POST, filters=filters or all
callback=func,
type=Handlers.EDITED_CHANNEL_POST,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand All @@ -92,7 +100,9 @@ def decorator(func: Callable) -> Callable:
def on_business_connection(self=None, filters: Filter = None):
def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func, type=Handlers.BUSINESS_CONNECTION, filters=filters or all
callback=func,
type=Handlers.BUSINESS_CONNECTION,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand All @@ -109,7 +119,9 @@ def decorator(func: Callable) -> Callable:
def on_business_message(self=None, filters: Filter = None):
def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func, type=Handlers.BUSINESS_MESSAGE, filters=filters or all
callback=func,
type=Handlers.BUSINESS_MESSAGE,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand All @@ -128,7 +140,7 @@ def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func,
type=Handlers.EDITED_BUSINESS_MESSAGE,
filters=filters or all,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand All @@ -147,7 +159,7 @@ def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func,
type=Handlers.DELETED_BUSINESS_MESSAGES,
filters=filters or all,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand All @@ -164,7 +176,9 @@ def decorator(func: Callable) -> Callable:
def on_message_reaction(self=None, filters: Filter = None):
def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func, type=Handlers.MESSAGE_REACTION, filters=filters or all
callback=func,
type=Handlers.MESSAGE_REACTION,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand All @@ -183,7 +197,7 @@ def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func,
type=Handlers.MESSAGE_REACTION_COUNT,
filters=filters or all,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand All @@ -200,7 +214,9 @@ def decorator(func: Callable) -> Callable:
def on_inline_query(self=None, filters: Filter = None):
def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func, type=Handlers.INLINE_QUERY, filters=filters or all
callback=func,
type=Handlers.INLINE_QUERY,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand All @@ -219,7 +235,7 @@ def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func,
type=Handlers.CHOSEN_INLINE_RESULT,
filters=filters or all,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand All @@ -236,7 +252,9 @@ def decorator(func: Callable) -> Callable:
def on_callback_query(self=None, filters: Filter = None):
def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func, type=Handlers.CALLBACK_QUERY, filters=filters or all
callback=func,
type=Handlers.CALLBACK_QUERY,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand All @@ -253,7 +271,9 @@ def decorator(func: Callable) -> Callable:
def on_shipping_query(self=None, filters: Filter = None):
def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func, type=Handlers.SHIPPING_QUERY, filters=filters or all
callback=func,
type=Handlers.SHIPPING_QUERY,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand All @@ -270,7 +290,9 @@ def decorator(func: Callable) -> Callable:
def on_pre_checkout_query(self=None, filters: Filter = None):
def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func, type=Handlers.PRE_CHECKOUT_QUERY, filters=filters or all
callback=func,
type=Handlers.PRE_CHECKOUT_QUERY,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand All @@ -286,7 +308,11 @@ def decorator(func: Callable) -> Callable:

def on_poll(self=None, filters: Filter = None):
def decorator(func: Callable) -> Callable:
handler = Handler(callback=func, type=Handlers.POLL, filters=filters or all)
handler = Handler(
callback=func,
type=Handlers.POLL,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
else:
Expand All @@ -302,7 +328,9 @@ def decorator(func: Callable) -> Callable:
def on_poll_answer(self=None, filters: Filter = None):
def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func, type=Handlers.POLL_ANSWER, filters=filters or all
callback=func,
type=Handlers.POLL_ANSWER,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand All @@ -319,7 +347,9 @@ def decorator(func: Callable) -> Callable:
def on_my_chat_member(self=None, filters: Filter = None):
def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func, type=Handlers.MY_CHAT_MEMBER, filters=filters or all
callback=func,
type=Handlers.MY_CHAT_MEMBER,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand All @@ -336,7 +366,9 @@ def decorator(func: Callable) -> Callable:
def on_chat_member(self=None, filters: Filter = None):
def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func, type=Handlers.CHAT_MEMBER, filters=filters or all
callback=func,
type=Handlers.CHAT_MEMBER,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand All @@ -353,7 +385,9 @@ def decorator(func: Callable) -> Callable:
def on_chat_join_request(self=None, filters: Filter = None):
def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func, type=Handlers.CHAT_JOIN_REQUEST, filters=filters or all
callback=func,
type=Handlers.CHAT_JOIN_REQUEST,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand All @@ -370,7 +404,9 @@ def decorator(func: Callable) -> Callable:
def on_chat_boost(self=None, filters: Filter = None):
def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func, type=Handlers.CHAT_BOOST, filters=filters or all
callback=func,
type=Handlers.CHAT_BOOST,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand All @@ -387,7 +423,9 @@ def decorator(func: Callable) -> Callable:
def on_removed_chat_boost(self=None, filters: Filter = None):
def decorator(func: Callable) -> Callable:
handler = Handler(
callback=func, type=Handlers.REMOVED_CHAT_BOOST, filters=filters or all
callback=func,
type=Handlers.REMOVED_CHAT_BOOST,
filters=self if isinstance(self, Filter) else (filters or all),
)
if isinstance(self, tgram.TgBot):
self.add_handler(handler)
Expand Down

0 comments on commit ebab462

Please sign in to comment.