Allow decorators in plugins to be stacked (#642)

This allows registering the same callback function more than once by using different handlers.
This commit is contained in:
Jonathan 2021-03-17 21:07:54 +02:00 committed by GitHub
parent c72bbcf9e1
commit f0b1cc41f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 69 additions and 30 deletions

View File

@ -717,8 +717,7 @@ class Client(Methods, Scaffold):
for name in vars(module).keys(): for name in vars(module).keys():
# noinspection PyBroadException # noinspection PyBroadException
try: try:
handler, group = getattr(module, name).handler for handler, group in getattr(module, name).handlers:
if isinstance(handler, Handler) and isinstance(group, int): if isinstance(handler, Handler) and isinstance(group, int):
self.add_handler(handler, group) self.add_handler(handler, group)

View File

@ -47,10 +47,15 @@ class OnCallbackQuery(Scaffold):
if isinstance(self, pyrogram.Client): if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.handlers.CallbackQueryHandler(func, filters), group) self.add_handler(pyrogram.handlers.CallbackQueryHandler(func, filters), group)
elif isinstance(self, Filter) or self is None: elif isinstance(self, Filter) or self is None:
func.handler = ( if not hasattr(func, "handlers"):
func.handlers = []
func.handlers.append(
(
pyrogram.handlers.CallbackQueryHandler(func, self), pyrogram.handlers.CallbackQueryHandler(func, self),
group if filters is None else filters group if filters is None else filters
) )
)
return func return func

View File

@ -47,10 +47,15 @@ class OnChosenInlineResult(Scaffold):
if isinstance(self, pyrogram.Client): if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.handlers.ChosenInlineResultHandler(func, filters), group) self.add_handler(pyrogram.handlers.ChosenInlineResultHandler(func, filters), group)
elif isinstance(self, Filter) or self is None: elif isinstance(self, Filter) or self is None:
func.handler = ( if not hasattr(func, "handlers"):
func.handlers = []
func.handlers.append(
(
pyrogram.handlers.ChosenInlineResultHandler(func, self), pyrogram.handlers.ChosenInlineResultHandler(func, self),
group if filters is None else filters group if filters is None else filters
) )
)
return func return func

View File

@ -47,10 +47,15 @@ class OnDeletedMessages(Scaffold):
if isinstance(self, pyrogram.Client): if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.handlers.DeletedMessagesHandler(func, filters), group) self.add_handler(pyrogram.handlers.DeletedMessagesHandler(func, filters), group)
elif isinstance(self, Filter) or self is None: elif isinstance(self, Filter) or self is None:
func.handler = ( if not hasattr(func, "handlers"):
func.handlers = []
func.handlers.append(
(
pyrogram.handlers.DeletedMessagesHandler(func, self), pyrogram.handlers.DeletedMessagesHandler(func, self),
group if filters is None else filters group if filters is None else filters
) )
)
return func return func

View File

@ -47,10 +47,15 @@ class OnInlineQuery(Scaffold):
if isinstance(self, pyrogram.Client): if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.handlers.InlineQueryHandler(func, filters), group) self.add_handler(pyrogram.handlers.InlineQueryHandler(func, filters), group)
elif isinstance(self, Filter) or self is None: elif isinstance(self, Filter) or self is None:
func.handler = ( if not hasattr(func, "handlers"):
func.handlers = []
func.handlers.append(
(
pyrogram.handlers.InlineQueryHandler(func, self), pyrogram.handlers.InlineQueryHandler(func, self),
group if filters is None else filters group if filters is None else filters
) )
)
return func return func

View File

@ -47,10 +47,15 @@ class OnMessage(Scaffold):
if isinstance(self, pyrogram.Client): if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.handlers.MessageHandler(func, filters), group) self.add_handler(pyrogram.handlers.MessageHandler(func, filters), group)
elif isinstance(self, Filter) or self is None: elif isinstance(self, Filter) or self is None:
func.handler = ( if not hasattr(func, "handlers"):
func.handlers = []
func.handlers.append(
(
pyrogram.handlers.MessageHandler(func, self), pyrogram.handlers.MessageHandler(func, self),
group if filters is None else filters group if filters is None else filters
) )
)
return func return func

View File

@ -47,10 +47,15 @@ class OnPoll(Scaffold):
if isinstance(self, pyrogram.Client): if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.handlers.PollHandler(func, filters), group) self.add_handler(pyrogram.handlers.PollHandler(func, filters), group)
elif isinstance(self, Filter) or self is None: elif isinstance(self, Filter) or self is None:
func.handler = ( if not hasattr(func, "handlers"):
func.handlers = []
func.handlers.append(
(
pyrogram.handlers.PollHandler(func, self), pyrogram.handlers.PollHandler(func, self),
group if filters is None else filters group if filters is None else filters
) )
)
return func return func

View File

@ -41,10 +41,15 @@ class OnRawUpdate(Scaffold):
if isinstance(self, pyrogram.Client): if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.handlers.RawUpdateHandler(func), group) self.add_handler(pyrogram.handlers.RawUpdateHandler(func), group)
else: else:
func.handler = ( if not hasattr(func, "handlers"):
func.handlers = []
func.handlers.append(
(
pyrogram.handlers.RawUpdateHandler(func), pyrogram.handlers.RawUpdateHandler(func),
group if self is None else group group if self is None else group
) )
)
return func return func

View File

@ -45,10 +45,15 @@ class OnUserStatus(Scaffold):
if isinstance(self, pyrogram.Client): if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.handlers.UserStatusHandler(func, filters), group) self.add_handler(pyrogram.handlers.UserStatusHandler(func, filters), group)
elif isinstance(self, Filter) or self is None: elif isinstance(self, Filter) or self is None:
func.handler = ( if not hasattr(func, "handlers"):
func.handlers = []
func.handlers.append(
(
pyrogram.handlers.UserStatusHandler(func, self), pyrogram.handlers.UserStatusHandler(func, self),
group if filters is None else filters group if filters is None else filters
) )
)
return func return func