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,13 +717,12 @@ 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):
self.add_handler(handler, group)
if isinstance(handler, Handler) and isinstance(group, int): log.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format(
self.add_handler(handler, group) self.session_name, type(handler).__name__, name, group, module_path))
log.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format(
self.session_name, type(handler).__name__, name, group, module_path))
count += 1 count += 1
except Exception: except Exception:

View File

@ -47,9 +47,14 @@ 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"):
pyrogram.handlers.CallbackQueryHandler(func, self), func.handlers = []
group if filters is None else filters
func.handlers.append(
(
pyrogram.handlers.CallbackQueryHandler(func, self),
group if filters is None else filters
)
) )
return func return func

View File

@ -47,9 +47,14 @@ 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"):
pyrogram.handlers.ChosenInlineResultHandler(func, self), func.handlers = []
group if filters is None else filters
func.handlers.append(
(
pyrogram.handlers.ChosenInlineResultHandler(func, self),
group if filters is None else filters
)
) )
return func return func

View File

@ -47,9 +47,14 @@ 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"):
pyrogram.handlers.DeletedMessagesHandler(func, self), func.handlers = []
group if filters is None else filters
func.handlers.append(
(
pyrogram.handlers.DeletedMessagesHandler(func, self),
group if filters is None else filters
)
) )
return func return func

View File

@ -47,9 +47,14 @@ 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"):
pyrogram.handlers.InlineQueryHandler(func, self), func.handlers = []
group if filters is None else filters
func.handlers.append(
(
pyrogram.handlers.InlineQueryHandler(func, self),
group if filters is None else filters
)
) )
return func return func

View File

@ -47,9 +47,14 @@ 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"):
pyrogram.handlers.MessageHandler(func, self), func.handlers = []
group if filters is None else filters
func.handlers.append(
(
pyrogram.handlers.MessageHandler(func, self),
group if filters is None else filters
)
) )
return func return func

View File

@ -47,9 +47,14 @@ 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"):
pyrogram.handlers.PollHandler(func, self), func.handlers = []
group if filters is None else filters
func.handlers.append(
(
pyrogram.handlers.PollHandler(func, self),
group if filters is None else filters
)
) )
return func return func

View File

@ -41,9 +41,14 @@ 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"):
pyrogram.handlers.RawUpdateHandler(func), func.handlers = []
group if self is None else group
func.handlers.append(
(
pyrogram.handlers.RawUpdateHandler(func),
group if self is None else group
)
) )
return func return func

View File

@ -45,9 +45,14 @@ 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"):
pyrogram.handlers.UserStatusHandler(func, self), func.handlers = []
group if filters is None else filters
func.handlers.append(
(
pyrogram.handlers.UserStatusHandler(func, self),
group if filters is None else filters
)
) )
return func return func