From 7f78d7ed2f356da48461eb88b1cc569582297eac Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Thu, 19 Jul 2018 01:47:32 +0200 Subject: [PATCH] Use classmethod for all Event.build --- telethon/events/callbackquery.py | 6 +-- telethon/events/chataction.py | 78 +++++++++++++++---------------- telethon/events/common.py | 4 +- telethon/events/inlinequery.py | 6 +-- telethon/events/messagedeleted.py | 8 ++-- telethon/events/messageedited.py | 6 +-- telethon/events/messageread.py | 24 +++++----- telethon/events/newmessage.py | 14 +++--- telethon/events/raw.py | 4 +- telethon/events/userupdate.py | 8 ++-- 10 files changed, 79 insertions(+), 79 deletions(-) diff --git a/telethon/events/callbackquery.py b/telethon/events/callbackquery.py index d9051347..ef01a341 100644 --- a/telethon/events/callbackquery.py +++ b/telethon/events/callbackquery.py @@ -40,11 +40,11 @@ class CallbackQuery(EventBuilder): else: raise TypeError('Invalid data type given') - @staticmethod - def build(update): + @classmethod + def build(cls, update): if isinstance(update, (types.UpdateBotCallbackQuery, types.UpdateInlineBotCallbackQuery)): - event = CallbackQuery.Event(update) + event = cls.Event(update) else: return diff --git a/telethon/events/chataction.py b/telethon/events/chataction.py index 1a36b931..59300317 100644 --- a/telethon/events/chataction.py +++ b/telethon/events/chataction.py @@ -8,24 +8,24 @@ class ChatAction(EventBuilder): """ Represents an action in a chat (such as user joined, left, or new pin). """ - @staticmethod - def build(update): + @classmethod + def build(cls, update): if isinstance(update, types.UpdateChannelPinnedMessage) and update.id == 0: # Telegram does not always send # UpdateChannelPinnedMessage for new pins # but always for unpin, with update.id = 0 - event = ChatAction.Event(types.PeerChannel(update.channel_id), - unpin=True) + event = cls.Event(types.PeerChannel(update.channel_id), + unpin=True) elif isinstance(update, types.UpdateChatParticipantAdd): - event = ChatAction.Event(types.PeerChat(update.chat_id), - added_by=update.inviter_id or True, - users=update.user_id) + event = cls.Event(types.PeerChat(update.chat_id), + added_by=update.inviter_id or True, + users=update.user_id) elif isinstance(update, types.UpdateChatParticipantDelete): - event = ChatAction.Event(types.PeerChat(update.chat_id), - kicked_by=True, - users=update.user_id) + event = cls.Event(types.PeerChat(update.chat_id), + kicked_by=True, + users=update.user_id) elif (isinstance(update, ( types.UpdateNewMessage, types.UpdateNewChannelMessage)) @@ -33,46 +33,46 @@ class ChatAction(EventBuilder): msg = update.message action = update.message.action if isinstance(action, types.MessageActionChatJoinedByLink): - event = ChatAction.Event(msg, - added_by=True, - users=msg.from_id) + event = cls.Event(msg, + added_by=True, + users=msg.from_id) elif isinstance(action, types.MessageActionChatAddUser): # If an user adds itself, it means they joined added_by = ([msg.from_id] == action.users) or msg.from_id - event = ChatAction.Event(msg, - added_by=added_by, - users=action.users) + event = cls.Event(msg, + added_by=added_by, + users=action.users) elif isinstance(action, types.MessageActionChatDeleteUser): - event = ChatAction.Event(msg, - kicked_by=msg.from_id or True, - users=action.user_id) + event = cls.Event(msg, + kicked_by=msg.from_id or True, + users=action.user_id) elif isinstance(action, types.MessageActionChatCreate): - event = ChatAction.Event(msg, - users=action.users, - created=True, - new_title=action.title) + event = cls.Event(msg, + users=action.users, + created=True, + new_title=action.title) elif isinstance(action, types.MessageActionChannelCreate): - event = ChatAction.Event(msg, - created=True, - users=msg.from_id, - new_title=action.title) + event = cls.Event(msg, + created=True, + users=msg.from_id, + new_title=action.title) elif isinstance(action, types.MessageActionChatEditTitle): - event = ChatAction.Event(msg, - users=msg.from_id, - new_title=action.title) + event = cls.Event(msg, + users=msg.from_id, + new_title=action.title) elif isinstance(action, types.MessageActionChatEditPhoto): - event = ChatAction.Event(msg, - users=msg.from_id, - new_photo=action.photo) + event = cls.Event(msg, + users=msg.from_id, + new_photo=action.photo) elif isinstance(action, types.MessageActionChatDeletePhoto): - event = ChatAction.Event(msg, - users=msg.from_id, - new_photo=True) + event = cls.Event(msg, + users=msg.from_id, + new_photo=True) elif isinstance(action, types.MessageActionPinMessage): # Telegram always sends this service message for new pins - event = ChatAction.Event(msg, - users=msg.from_id, - new_pin=msg.reply_to_msg_id) + event = cls.Event(msg, + users=msg.from_id, + new_pin=msg.reply_to_msg_id) else: return else: diff --git a/telethon/events/common.py b/telethon/events/common.py index 6af00a63..23b018e1 100644 --- a/telethon/events/common.py +++ b/telethon/events/common.py @@ -59,9 +59,9 @@ class EventBuilder(abc.ABC): self.blacklist_chats = blacklist_chats self._self_id = None - @staticmethod + @classmethod @abc.abstractmethod - def build(update): + def build(cls, update): """Builds an event for the given update if possible, or returns None""" async def resolve(self, client): diff --git a/telethon/events/inlinequery.py b/telethon/events/inlinequery.py index ea6f45c2..3c163137 100644 --- a/telethon/events/inlinequery.py +++ b/telethon/events/inlinequery.py @@ -43,10 +43,10 @@ class InlineQuery(EventBuilder): else: raise TypeError('Invalid pattern type given') - @staticmethod - def build(update): + @classmethod + def build(cls, update): if isinstance(update, types.UpdateBotInlineQuery): - event = InlineQuery.Event(update) + event = cls.Event(update) else: return diff --git a/telethon/events/messagedeleted.py b/telethon/events/messagedeleted.py index ffcb5c23..f13ed6ee 100644 --- a/telethon/events/messagedeleted.py +++ b/telethon/events/messagedeleted.py @@ -7,15 +7,15 @@ class MessageDeleted(EventBuilder): """ Event fired when one or more messages are deleted. """ - @staticmethod - def build(update): + @classmethod + def build(cls, update): if isinstance(update, types.UpdateDeleteMessages): - event = MessageDeleted.Event( + event = cls.Event( deleted_ids=update.messages, peer=None ) elif isinstance(update, types.UpdateDeleteChannelMessages): - event = MessageDeleted.Event( + event = cls.Event( deleted_ids=update.messages, peer=types.PeerChannel(update.channel_id) ) diff --git a/telethon/events/messageedited.py b/telethon/events/messageedited.py index 8bb121d3..3694251f 100644 --- a/telethon/events/messageedited.py +++ b/telethon/events/messageedited.py @@ -8,11 +8,11 @@ class MessageEdited(NewMessage): """ Event fired when a message has been edited. """ - @staticmethod - def build(update): + @classmethod + def build(cls, update): if isinstance(update, (types.UpdateEditMessage, types.UpdateEditChannelMessage)): - event = MessageEdited.Event(update.message) + event = cls.Event(update.message) else: return diff --git a/telethon/events/messageread.py b/telethon/events/messageread.py index 91496c40..1788add0 100644 --- a/telethon/events/messageread.py +++ b/telethon/events/messageread.py @@ -18,25 +18,25 @@ class MessageRead(EventBuilder): super().__init__(chats, blacklist_chats) self.inbox = inbox - @staticmethod - def build(update): + @classmethod + def build(cls, update): if isinstance(update, types.UpdateReadHistoryInbox): - event = MessageRead.Event(update.peer, update.max_id, False) + event = cls.Event(update.peer, update.max_id, False) elif isinstance(update, types.UpdateReadHistoryOutbox): - event = MessageRead.Event(update.peer, update.max_id, True) + event = cls.Event(update.peer, update.max_id, True) elif isinstance(update, types.UpdateReadChannelInbox): - event = MessageRead.Event(types.PeerChannel(update.channel_id), + event = cls.Event(types.PeerChannel(update.channel_id), update.max_id, False) elif isinstance(update, types.UpdateReadChannelOutbox): - event = MessageRead.Event(types.PeerChannel(update.channel_id), - update.max_id, True) + event = cls.Event(types.PeerChannel(update.channel_id), + update.max_id, True) elif isinstance(update, types.UpdateReadMessagesContents): - event = MessageRead.Event(message_ids=update.messages, - contents=True) + event = cls.Event(message_ids=update.messages, + contents=True) elif isinstance(update, types.UpdateChannelReadMessagesContents): - event = MessageRead.Event(types.PeerChannel(update.channel_id), - message_ids=update.messages, - contents=True) + event = cls.Event(types.PeerChannel(update.channel_id), + message_ids=update.messages, + contents=True) else: return diff --git a/telethon/events/newmessage.py b/telethon/events/newmessage.py index 02034824..44b688d6 100644 --- a/telethon/events/newmessage.py +++ b/telethon/events/newmessage.py @@ -75,15 +75,15 @@ class NewMessage(EventBuilder): await super().resolve(client) self.from_users = await _into_id_set(client, self.from_users) - @staticmethod - def build(update): + @classmethod + def build(cls, update): if isinstance(update, (types.UpdateNewMessage, types.UpdateNewChannelMessage)): if not isinstance(update.message, types.Message): return # We don't care about MessageService's here - event = NewMessage.Event(update.message) + event = cls.Event(update.message) elif isinstance(update, types.UpdateShortMessage): - event = NewMessage.Event(types.Message( + event = cls.Event(types.Message( out=update.out, mentioned=update.mentioned, media_unread=update.media_unread, @@ -92,9 +92,9 @@ class NewMessage(EventBuilder): # Note that to_id/from_id complement each other in private # messages, depending on whether the message was outgoing. to_id=types.PeerUser( - update.user_id if update.out else EventBuilder.self_id + update.user_id if update.out else cls.self_id ), - from_id=EventBuilder.self_id if update.out else update.user_id, + from_id=cls.self_id if update.out else update.user_id, message=update.message, date=update.date, fwd_from=update.fwd_from, @@ -103,7 +103,7 @@ class NewMessage(EventBuilder): entities=update.entities )) elif isinstance(update, types.UpdateShortChatMessage): - event = NewMessage.Event(types.Message( + event = cls.Event(types.Message( out=update.out, mentioned=update.mentioned, media_unread=update.media_unread, diff --git a/telethon/events/raw.py b/telethon/events/raw.py index befce28f..229fdd53 100644 --- a/telethon/events/raw.py +++ b/telethon/events/raw.py @@ -25,8 +25,8 @@ class Raw(EventBuilder): async def resolve(self, client): pass - @staticmethod - def build(update): + @classmethod + def build(cls, update): return update def filter(self, event): diff --git a/telethon/events/userupdate.py b/telethon/events/userupdate.py index 5a67d40b..925b353b 100644 --- a/telethon/events/userupdate.py +++ b/telethon/events/userupdate.py @@ -9,11 +9,11 @@ class UserUpdate(EventBuilder): """ Represents an user update (gone online, offline, joined Telegram). """ - @staticmethod - def build(update): + @classmethod + def build(cls, update): if isinstance(update, types.UpdateUserStatus): - event = UserUpdate.Event(update.user_id, - status=update.status) + event = cls.Event(update.user_id, + status=update.status) else: return