Allow registering handlers using decorators

This commit is contained in:
Dan 2018-04-06 20:38:34 +02:00
parent ede627de52
commit 90a4e4c411

View File

@ -36,6 +36,7 @@ from queue import Queue
from signal import signal, SIGINT, SIGTERM, SIGABRT
from threading import Event, Thread
import pyrogram
from pyrogram.api import functions, types
from pyrogram.api.core import Object
from pyrogram.api.errors import (
@ -188,6 +189,25 @@ class Client:
self.dispatcher = Dispatcher(self, workers)
self.update_handler = None
def on(self, handler, group: int):
def decorator(f):
self.add_handler(handler(f), group)
return f
return decorator
def on_message(self, group: int = 0):
return self.on(pyrogram.MessageHandler, group)
def on_edited_message(self, group: int = 0):
return self.on(pyrogram.EditedMessageHandler, group)
def on_channel_post(self, group: int = 0):
return self.on(pyrogram.ChannelPostHandler, group)
def on_edited_channel_post(self, group: int = 0):
return self.on(pyrogram.EditedChannelPostHandler, group)
def add_handler(self, handler: Handler, group: int = 0):
self.dispatcher.add_handler(handler, group)