mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-01-27 01:34:29 +03:00
Use classmethod for all Event.build
This commit is contained in:
parent
2d7c8908eb
commit
7f78d7ed2f
|
@ -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
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user