Use classmethod for all Event.build

This commit is contained in:
Lonami Exo 2018-07-19 01:47:32 +02:00
parent 2d7c8908eb
commit 7f78d7ed2f
10 changed files with 79 additions and 79 deletions

View File

@ -40,11 +40,11 @@ class CallbackQuery(EventBuilder):
else: else:
raise TypeError('Invalid data type given') raise TypeError('Invalid data type given')
@staticmethod @classmethod
def build(update): def build(cls, update):
if isinstance(update, (types.UpdateBotCallbackQuery, if isinstance(update, (types.UpdateBotCallbackQuery,
types.UpdateInlineBotCallbackQuery)): types.UpdateInlineBotCallbackQuery)):
event = CallbackQuery.Event(update) event = cls.Event(update)
else: else:
return return

View File

@ -8,24 +8,24 @@ class ChatAction(EventBuilder):
""" """
Represents an action in a chat (such as user joined, left, or new pin). Represents an action in a chat (such as user joined, left, or new pin).
""" """
@staticmethod @classmethod
def build(update): def build(cls, update):
if isinstance(update, types.UpdateChannelPinnedMessage) and update.id == 0: if isinstance(update, types.UpdateChannelPinnedMessage) and update.id == 0:
# Telegram does not always send # Telegram does not always send
# UpdateChannelPinnedMessage for new pins # UpdateChannelPinnedMessage for new pins
# but always for unpin, with update.id = 0 # but always for unpin, with update.id = 0
event = ChatAction.Event(types.PeerChannel(update.channel_id), event = cls.Event(types.PeerChannel(update.channel_id),
unpin=True) unpin=True)
elif isinstance(update, types.UpdateChatParticipantAdd): elif isinstance(update, types.UpdateChatParticipantAdd):
event = ChatAction.Event(types.PeerChat(update.chat_id), event = cls.Event(types.PeerChat(update.chat_id),
added_by=update.inviter_id or True, added_by=update.inviter_id or True,
users=update.user_id) users=update.user_id)
elif isinstance(update, types.UpdateChatParticipantDelete): elif isinstance(update, types.UpdateChatParticipantDelete):
event = ChatAction.Event(types.PeerChat(update.chat_id), event = cls.Event(types.PeerChat(update.chat_id),
kicked_by=True, kicked_by=True,
users=update.user_id) users=update.user_id)
elif (isinstance(update, ( elif (isinstance(update, (
types.UpdateNewMessage, types.UpdateNewChannelMessage)) types.UpdateNewMessage, types.UpdateNewChannelMessage))
@ -33,46 +33,46 @@ class ChatAction(EventBuilder):
msg = update.message msg = update.message
action = update.message.action action = update.message.action
if isinstance(action, types.MessageActionChatJoinedByLink): if isinstance(action, types.MessageActionChatJoinedByLink):
event = ChatAction.Event(msg, event = cls.Event(msg,
added_by=True, added_by=True,
users=msg.from_id) users=msg.from_id)
elif isinstance(action, types.MessageActionChatAddUser): elif isinstance(action, types.MessageActionChatAddUser):
# If an user adds itself, it means they joined # If an user adds itself, it means they joined
added_by = ([msg.from_id] == action.users) or msg.from_id added_by = ([msg.from_id] == action.users) or msg.from_id
event = ChatAction.Event(msg, event = cls.Event(msg,
added_by=added_by, added_by=added_by,
users=action.users) users=action.users)
elif isinstance(action, types.MessageActionChatDeleteUser): elif isinstance(action, types.MessageActionChatDeleteUser):
event = ChatAction.Event(msg, event = cls.Event(msg,
kicked_by=msg.from_id or True, kicked_by=msg.from_id or True,
users=action.user_id) users=action.user_id)
elif isinstance(action, types.MessageActionChatCreate): elif isinstance(action, types.MessageActionChatCreate):
event = ChatAction.Event(msg, event = cls.Event(msg,
users=action.users, users=action.users,
created=True, created=True,
new_title=action.title) new_title=action.title)
elif isinstance(action, types.MessageActionChannelCreate): elif isinstance(action, types.MessageActionChannelCreate):
event = ChatAction.Event(msg, event = cls.Event(msg,
created=True, created=True,
users=msg.from_id, users=msg.from_id,
new_title=action.title) new_title=action.title)
elif isinstance(action, types.MessageActionChatEditTitle): elif isinstance(action, types.MessageActionChatEditTitle):
event = ChatAction.Event(msg, event = cls.Event(msg,
users=msg.from_id, users=msg.from_id,
new_title=action.title) new_title=action.title)
elif isinstance(action, types.MessageActionChatEditPhoto): elif isinstance(action, types.MessageActionChatEditPhoto):
event = ChatAction.Event(msg, event = cls.Event(msg,
users=msg.from_id, users=msg.from_id,
new_photo=action.photo) new_photo=action.photo)
elif isinstance(action, types.MessageActionChatDeletePhoto): elif isinstance(action, types.MessageActionChatDeletePhoto):
event = ChatAction.Event(msg, event = cls.Event(msg,
users=msg.from_id, users=msg.from_id,
new_photo=True) new_photo=True)
elif isinstance(action, types.MessageActionPinMessage): elif isinstance(action, types.MessageActionPinMessage):
# Telegram always sends this service message for new pins # Telegram always sends this service message for new pins
event = ChatAction.Event(msg, event = cls.Event(msg,
users=msg.from_id, users=msg.from_id,
new_pin=msg.reply_to_msg_id) new_pin=msg.reply_to_msg_id)
else: else:
return return
else: else:

View File

@ -59,9 +59,9 @@ class EventBuilder(abc.ABC):
self.blacklist_chats = blacklist_chats self.blacklist_chats = blacklist_chats
self._self_id = None self._self_id = None
@staticmethod @classmethod
@abc.abstractmethod @abc.abstractmethod
def build(update): def build(cls, update):
"""Builds an event for the given update if possible, or returns None""" """Builds an event for the given update if possible, or returns None"""
async def resolve(self, client): async def resolve(self, client):

View File

@ -43,10 +43,10 @@ class InlineQuery(EventBuilder):
else: else:
raise TypeError('Invalid pattern type given') raise TypeError('Invalid pattern type given')
@staticmethod @classmethod
def build(update): def build(cls, update):
if isinstance(update, types.UpdateBotInlineQuery): if isinstance(update, types.UpdateBotInlineQuery):
event = InlineQuery.Event(update) event = cls.Event(update)
else: else:
return return

View File

@ -7,15 +7,15 @@ class MessageDeleted(EventBuilder):
""" """
Event fired when one or more messages are deleted. Event fired when one or more messages are deleted.
""" """
@staticmethod @classmethod
def build(update): def build(cls, update):
if isinstance(update, types.UpdateDeleteMessages): if isinstance(update, types.UpdateDeleteMessages):
event = MessageDeleted.Event( event = cls.Event(
deleted_ids=update.messages, deleted_ids=update.messages,
peer=None peer=None
) )
elif isinstance(update, types.UpdateDeleteChannelMessages): elif isinstance(update, types.UpdateDeleteChannelMessages):
event = MessageDeleted.Event( event = cls.Event(
deleted_ids=update.messages, deleted_ids=update.messages,
peer=types.PeerChannel(update.channel_id) peer=types.PeerChannel(update.channel_id)
) )

View File

@ -8,11 +8,11 @@ class MessageEdited(NewMessage):
""" """
Event fired when a message has been edited. Event fired when a message has been edited.
""" """
@staticmethod @classmethod
def build(update): def build(cls, update):
if isinstance(update, (types.UpdateEditMessage, if isinstance(update, (types.UpdateEditMessage,
types.UpdateEditChannelMessage)): types.UpdateEditChannelMessage)):
event = MessageEdited.Event(update.message) event = cls.Event(update.message)
else: else:
return return

View File

@ -18,25 +18,25 @@ class MessageRead(EventBuilder):
super().__init__(chats, blacklist_chats) super().__init__(chats, blacklist_chats)
self.inbox = inbox self.inbox = inbox
@staticmethod @classmethod
def build(update): def build(cls, update):
if isinstance(update, types.UpdateReadHistoryInbox): 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): 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): 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) update.max_id, False)
elif isinstance(update, types.UpdateReadChannelOutbox): elif isinstance(update, types.UpdateReadChannelOutbox):
event = MessageRead.Event(types.PeerChannel(update.channel_id), event = cls.Event(types.PeerChannel(update.channel_id),
update.max_id, True) update.max_id, True)
elif isinstance(update, types.UpdateReadMessagesContents): elif isinstance(update, types.UpdateReadMessagesContents):
event = MessageRead.Event(message_ids=update.messages, event = cls.Event(message_ids=update.messages,
contents=True) contents=True)
elif isinstance(update, types.UpdateChannelReadMessagesContents): elif isinstance(update, types.UpdateChannelReadMessagesContents):
event = MessageRead.Event(types.PeerChannel(update.channel_id), event = cls.Event(types.PeerChannel(update.channel_id),
message_ids=update.messages, message_ids=update.messages,
contents=True) contents=True)
else: else:
return return

View File

@ -75,15 +75,15 @@ class NewMessage(EventBuilder):
await super().resolve(client) await super().resolve(client)
self.from_users = await _into_id_set(client, self.from_users) self.from_users = await _into_id_set(client, self.from_users)
@staticmethod @classmethod
def build(update): def build(cls, update):
if isinstance(update, if isinstance(update,
(types.UpdateNewMessage, types.UpdateNewChannelMessage)): (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
if not isinstance(update.message, types.Message): if not isinstance(update.message, types.Message):
return # We don't care about MessageService's here return # We don't care about MessageService's here
event = NewMessage.Event(update.message) event = cls.Event(update.message)
elif isinstance(update, types.UpdateShortMessage): elif isinstance(update, types.UpdateShortMessage):
event = NewMessage.Event(types.Message( event = cls.Event(types.Message(
out=update.out, out=update.out,
mentioned=update.mentioned, mentioned=update.mentioned,
media_unread=update.media_unread, media_unread=update.media_unread,
@ -92,9 +92,9 @@ class NewMessage(EventBuilder):
# Note that to_id/from_id complement each other in private # Note that to_id/from_id complement each other in private
# messages, depending on whether the message was outgoing. # messages, depending on whether the message was outgoing.
to_id=types.PeerUser( 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, message=update.message,
date=update.date, date=update.date,
fwd_from=update.fwd_from, fwd_from=update.fwd_from,
@ -103,7 +103,7 @@ class NewMessage(EventBuilder):
entities=update.entities entities=update.entities
)) ))
elif isinstance(update, types.UpdateShortChatMessage): elif isinstance(update, types.UpdateShortChatMessage):
event = NewMessage.Event(types.Message( event = cls.Event(types.Message(
out=update.out, out=update.out,
mentioned=update.mentioned, mentioned=update.mentioned,
media_unread=update.media_unread, media_unread=update.media_unread,

View File

@ -25,8 +25,8 @@ class Raw(EventBuilder):
async def resolve(self, client): async def resolve(self, client):
pass pass
@staticmethod @classmethod
def build(update): def build(cls, update):
return update return update
def filter(self, event): def filter(self, event):

View File

@ -9,11 +9,11 @@ class UserUpdate(EventBuilder):
""" """
Represents an user update (gone online, offline, joined Telegram). Represents an user update (gone online, offline, joined Telegram).
""" """
@staticmethod @classmethod
def build(update): def build(cls, update):
if isinstance(update, types.UpdateUserStatus): if isinstance(update, types.UpdateUserStatus):
event = UserUpdate.Event(update.user_id, event = cls.Event(update.user_id,
status=update.status) status=update.status)
else: else:
return return