mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-29 13:27:47 +00:00
Optimize dispatcher.py code
This commit is contained in:
parent
d580a78787
commit
a315c30336
@ -25,7 +25,7 @@ from threading import Thread
|
|||||||
import pyrogram
|
import pyrogram
|
||||||
from pyrogram.api import types
|
from pyrogram.api import types
|
||||||
from ..ext import utils
|
from ..ext import utils
|
||||||
from ..handlers import RawUpdateHandler, CallbackQueryHandler, MessageHandler, DeletedMessagesHandler, UserStatusHandler
|
from ..handlers import CallbackQueryHandler, MessageHandler, DeletedMessagesHandler, UserStatusHandler, RawUpdateHandler
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -88,56 +88,6 @@ class Dispatcher:
|
|||||||
"Handler was not removed.".format(group))
|
"Handler was not removed.".format(group))
|
||||||
self.groups[group].remove(handler)
|
self.groups[group].remove(handler)
|
||||||
|
|
||||||
def dispatch(self, update, users: dict = None, chats: dict = None, is_raw: bool = False):
|
|
||||||
for group in self.groups.values():
|
|
||||||
try:
|
|
||||||
for handler in group:
|
|
||||||
if is_raw:
|
|
||||||
if not isinstance(handler, RawUpdateHandler):
|
|
||||||
continue
|
|
||||||
|
|
||||||
args = (self.client, update, users, chats)
|
|
||||||
else:
|
|
||||||
message = (update.message
|
|
||||||
or update.channel_post
|
|
||||||
or update.edited_message
|
|
||||||
or update.edited_channel_post)
|
|
||||||
|
|
||||||
deleted_messages = (update.deleted_channel_posts
|
|
||||||
or update.deleted_messages)
|
|
||||||
|
|
||||||
callback_query = update.callback_query
|
|
||||||
|
|
||||||
user_status = update.user_status
|
|
||||||
|
|
||||||
if message and isinstance(handler, MessageHandler):
|
|
||||||
if not handler.check(message):
|
|
||||||
continue
|
|
||||||
|
|
||||||
args = (self.client, message)
|
|
||||||
elif deleted_messages and isinstance(handler, DeletedMessagesHandler):
|
|
||||||
if not handler.check(deleted_messages):
|
|
||||||
continue
|
|
||||||
|
|
||||||
args = (self.client, deleted_messages)
|
|
||||||
elif callback_query and isinstance(handler, CallbackQueryHandler):
|
|
||||||
if not handler.check(callback_query):
|
|
||||||
continue
|
|
||||||
|
|
||||||
args = (self.client, callback_query)
|
|
||||||
elif user_status and isinstance(handler, UserStatusHandler):
|
|
||||||
if not handler.check(user_status):
|
|
||||||
continue
|
|
||||||
|
|
||||||
args = (self.client, user_status)
|
|
||||||
else:
|
|
||||||
continue
|
|
||||||
|
|
||||||
handler.callback(*args)
|
|
||||||
break
|
|
||||||
except Exception as e:
|
|
||||||
log.error(e, exc_info=True)
|
|
||||||
|
|
||||||
def update_worker(self):
|
def update_worker(self):
|
||||||
name = threading.current_thread().name
|
name = threading.current_thread().name
|
||||||
log.debug("{} started".format(name))
|
log.debug("{} started".format(name))
|
||||||
@ -153,80 +103,93 @@ class Dispatcher:
|
|||||||
chats = {i.id: i for i in update[2]}
|
chats = {i.id: i for i in update[2]}
|
||||||
update = update[0]
|
update = update[0]
|
||||||
|
|
||||||
self.dispatch(update, users=users, chats=chats, is_raw=True)
|
self.dispatch_raw(update, users=users, chats=chats, handler_class=RawUpdateHandler)
|
||||||
|
|
||||||
if isinstance(update, Dispatcher.MESSAGE_UPDATES):
|
if isinstance(update, Dispatcher.MESSAGE_UPDATES):
|
||||||
if isinstance(update.message, types.MessageEmpty):
|
if isinstance(update.message, types.MessageEmpty):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
message = utils.parse_messages(
|
message = utils.parse_messages(self.client, update.message, users, chats)
|
||||||
self.client,
|
is_edited = isinstance(update, Dispatcher.EDIT_MESSAGE_UPDATES)
|
||||||
update.message,
|
is_channel = message.chat.type == "channel"
|
||||||
users,
|
|
||||||
chats
|
|
||||||
)
|
|
||||||
|
|
||||||
is_edited_message = isinstance(update, Dispatcher.EDIT_MESSAGE_UPDATES)
|
update = pyrogram.Update(
|
||||||
|
message=message if not is_channel and not is_edited else None,
|
||||||
self.dispatch(
|
edited_message=message if not is_channel and is_edited else None,
|
||||||
pyrogram.Update(
|
channel_post=message if is_channel and not is_edited else None,
|
||||||
message=((message if message.chat.type != "channel"
|
edited_channel_post=message if is_channel and is_edited else None
|
||||||
else None) if not is_edited_message
|
|
||||||
else None),
|
|
||||||
edited_message=((message if message.chat.type != "channel"
|
|
||||||
else None) if is_edited_message
|
|
||||||
else None),
|
|
||||||
channel_post=((message if message.chat.type == "channel"
|
|
||||||
else None) if not is_edited_message
|
|
||||||
else None),
|
|
||||||
edited_channel_post=((message if message.chat.type == "channel"
|
|
||||||
else None) if is_edited_message
|
|
||||||
else None)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
elif isinstance(update, Dispatcher.DELETE_MESSAGE_UPDATES):
|
elif isinstance(update, Dispatcher.DELETE_MESSAGE_UPDATES):
|
||||||
is_channel = hasattr(update, 'channel_id')
|
is_channel = hasattr(update, "channel_id")
|
||||||
|
|
||||||
messages = utils.parse_deleted_messages(
|
messages = utils.parse_deleted_messages(
|
||||||
update.messages,
|
update.messages,
|
||||||
(update.channel_id if is_channel else None)
|
update.channel_id if is_channel else None
|
||||||
)
|
)
|
||||||
|
|
||||||
self.dispatch(
|
update = pyrogram.Update(
|
||||||
pyrogram.Update(
|
deleted_messages=messages if not is_channel else None,
|
||||||
deleted_messages=(messages if not is_channel else None),
|
deleted_channel_posts=messages if is_channel else None
|
||||||
deleted_channel_posts=(messages if is_channel else None)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
elif isinstance(update, types.UpdateBotCallbackQuery):
|
elif isinstance(update, types.UpdateBotCallbackQuery):
|
||||||
self.dispatch(
|
update = pyrogram.Update(
|
||||||
pyrogram.Update(
|
callback_query=utils.parse_callback_query(
|
||||||
callback_query=utils.parse_callback_query(
|
self.client, update, users
|
||||||
self.client, update, users
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
elif isinstance(update, types.UpdateInlineBotCallbackQuery):
|
elif isinstance(update, types.UpdateInlineBotCallbackQuery):
|
||||||
self.dispatch(
|
update = pyrogram.Update(
|
||||||
pyrogram.Update(
|
callback_query=utils.parse_inline_callback_query(
|
||||||
callback_query=utils.parse_inline_callback_query(
|
self.client, update, users
|
||||||
self.client, update, users
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
elif isinstance(update, types.UpdateUserStatus):
|
elif isinstance(update, types.UpdateUserStatus):
|
||||||
self.dispatch(
|
update = pyrogram.Update(
|
||||||
pyrogram.Update(
|
user_status=utils.parse_user_status(
|
||||||
user_status=utils.parse_user_status(
|
update.status, update.user_id
|
||||||
update.status, update.user_id
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
self.dispatch(update)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error(e, exc_info=True)
|
log.error(e, exc_info=True)
|
||||||
|
|
||||||
log.debug("{} stopped".format(name))
|
log.debug("{} stopped".format(name))
|
||||||
|
|
||||||
|
def dispatch_raw(self, update, users: dict, chats: dict, handler_class):
|
||||||
|
for group in self.groups.values():
|
||||||
|
for handler in group:
|
||||||
|
if isinstance(handler, handler_class):
|
||||||
|
try:
|
||||||
|
handler.callback(self.client, update, users, chats)
|
||||||
|
except Exception as e:
|
||||||
|
log.error(e, exc_info=True)
|
||||||
|
|
||||||
|
# noinspection PyShadowingBuiltins
|
||||||
|
def dispatch(self, update):
|
||||||
|
message = update.message or update.channel_post or update.edited_message or update.edited_channel_post
|
||||||
|
deleted_messages = update.deleted_channel_posts or update.deleted_messages
|
||||||
|
callback_query = update.callback_query
|
||||||
|
user_status = update.user_status
|
||||||
|
|
||||||
|
update = message or deleted_messages or callback_query or user_status
|
||||||
|
|
||||||
|
type = (
|
||||||
|
MessageHandler if message
|
||||||
|
else DeletedMessagesHandler if deleted_messages
|
||||||
|
else CallbackQueryHandler if callback_query
|
||||||
|
else UserStatusHandler if user_status
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
|
||||||
|
for group in self.groups.values():
|
||||||
|
for handler in group:
|
||||||
|
if isinstance(handler, type):
|
||||||
|
if handler.check(update):
|
||||||
|
try:
|
||||||
|
handler.callback(self.client, update)
|
||||||
|
except Exception as e:
|
||||||
|
log.error(e, exc_info=True)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user