Remove client check in custom Message

This commit is contained in:
Lonami Exo 2022-02-07 09:36:31 +01:00
parent d87b68a756
commit a6f53baaba

View File

@ -27,6 +27,11 @@ def _fwd(field, doc):
return property(fget, fset, None, doc) return property(fget, fset, None, doc)
class _UninitClient:
def __getattribute__(self, attr):
raise ValueError('this Message instance does not come from a chat and cannot be used')
# TODO Figure out a way to have the code generator error on missing fields # TODO Figure out a way to have the code generator error on missing fields
# Maybe parsing the init function alone if that's possible. # Maybe parsing the init function alone if that's possible.
class Message(ChatGetter, SenderGetter): class Message(ChatGetter, SenderGetter):
@ -44,6 +49,9 @@ class Message(ChatGetter, SenderGetter):
file with a certain performer, duration and thumbnail. However, most file with a certain performer, duration and thumbnail. However, most
properties and methods won't work (since messages you create have not yet properties and methods won't work (since messages you create have not yet
been sent). been sent).
Manually-created instances of this message cannot be responded to, edited,
and so on, because the message needs to first be sent for those to make sense.
""" """
# region Forwarded properties # region Forwarded properties
@ -409,6 +417,7 @@ class Message(ChatGetter, SenderGetter):
buttons=buttons, buttons=buttons,
ttl=ttl, ttl=ttl,
) )
self._client = _UninitClient()
@classmethod @classmethod
def _new(cls, client, message, entities, input_chat): def _new(cls, client, message, entities, input_chat):
@ -515,10 +524,9 @@ class Message(ChatGetter, SenderGetter):
def client(self): def client(self):
""" """
Returns the `TelegramClient <telethon.client.telegramclient.TelegramClient>` Returns the `TelegramClient <telethon.client.telegramclient.TelegramClient>`
that *patched* this message. This will only be present if you which returned this message from a friendly method. It won't be there if you
**use the friendly methods**, it won't be there if you invoke invoke raw API methods manually (because those return the original :tl:`Message`,
raw API methods manually, in which case you should only access not this class).
members, not properties.
""" """
return self._client return self._client
@ -840,7 +848,7 @@ class Message(ChatGetter, SenderGetter):
""" """
# If the client wasn't set we can't emulate the behaviour correctly, # If the client wasn't set we can't emulate the behaviour correctly,
# so as a best-effort simply return the chat peer. # so as a best-effort simply return the chat peer.
if self._client and not self.out and self.is_private: if not self.out and self.is_private:
return _tl.PeerUser(self._client._session_state.user_id) return _tl.PeerUser(self._client._session_state.user_id)
return self.peer_id return self.peer_id
@ -895,7 +903,7 @@ class Message(ChatGetter, SenderGetter):
The result will be cached after its first use. The result will be cached after its first use.
""" """
if self._reply_message is None and self._client: if self._reply_message is None:
if not self.reply_to: if not self.reply_to:
return None return None
@ -923,7 +931,6 @@ class Message(ChatGetter, SenderGetter):
`telethon.client.messages.MessageMethods.send_message` `telethon.client.messages.MessageMethods.send_message`
with ``entity`` already set. with ``entity`` already set.
""" """
if self._client:
return await self._client.send_message( return await self._client.send_message(
await self.get_input_chat(), *args, **kwargs) await self.get_input_chat(), *args, **kwargs)
@ -933,7 +940,6 @@ class Message(ChatGetter, SenderGetter):
`telethon.client.messages.MessageMethods.send_message` `telethon.client.messages.MessageMethods.send_message`
with both ``entity`` and ``reply_to`` already set. with both ``entity`` and ``reply_to`` already set.
""" """
if self._client:
kwargs['reply_to'] = self.id kwargs['reply_to'] = self.id
return await self._client.send_message( return await self._client.send_message(
await self.get_input_chat(), *args, **kwargs) await self.get_input_chat(), *args, **kwargs)
@ -948,7 +954,6 @@ class Message(ChatGetter, SenderGetter):
this `forward_to` method. Use a this `forward_to` method. Use a
`telethon.client.telegramclient.TelegramClient` instance directly. `telethon.client.telegramclient.TelegramClient` instance directly.
""" """
if self._client:
kwargs['messages'] = self.id kwargs['messages'] = self.id
kwargs['from_peer'] = await self.get_input_chat() kwargs['from_peer'] = await self.get_input_chat()
return await self._client.forward_messages(*args, **kwargs) return await self._client.forward_messages(*args, **kwargs)
@ -994,7 +999,6 @@ class Message(ChatGetter, SenderGetter):
this `delete` method. Use a this `delete` method. Use a
`telethon.client.telegramclient.TelegramClient` instance directly. `telethon.client.telegramclient.TelegramClient` instance directly.
""" """
if self._client:
return await self._client.delete_messages( return await self._client.delete_messages(
await self.get_input_chat(), [self.id], await self.get_input_chat(), [self.id],
*args, **kwargs *args, **kwargs
@ -1006,7 +1010,6 @@ class Message(ChatGetter, SenderGetter):
for `telethon.client.downloads.DownloadMethods.download_media` for `telethon.client.downloads.DownloadMethods.download_media`
with the ``message`` already set. with the ``message`` already set.
""" """
if self._client:
# Passing the entire message is important, in case it has to be # Passing the entire message is important, in case it has to be
# refetched for a fresh file reference. # refetched for a fresh file reference.
return await self._client.download_media(self, *args, **kwargs) return await self._client.download_media(self, *args, **kwargs)
@ -1117,9 +1120,6 @@ class Message(ChatGetter, SenderGetter):
# Click on a button requesting a phone # Click on a button requesting a phone
await message.click(0, share_phone=True) await message.click(0, share_phone=True)
""" """
if not self._client:
return
if data: if data:
chat = await self.get_input_chat() chat = await self.get_input_chat()
if not chat: if not chat:
@ -1209,7 +1209,6 @@ class Message(ChatGetter, SenderGetter):
<telethon.client.messages.MessageMethods.mark_read>` <telethon.client.messages.MessageMethods.mark_read>`
with both ``entity`` and ``message`` already set. with both ``entity`` and ``message`` already set.
""" """
if self._client:
await self._client.mark_read( await self._client.mark_read(
await self.get_input_chat(), max_id=self.id) await self.get_input_chat(), max_id=self.id)
@ -1222,7 +1221,6 @@ class Message(ChatGetter, SenderGetter):
# TODO Constantly checking if client is a bit annoying, # TODO Constantly checking if client is a bit annoying,
# maybe just make it illegal to call messages from raw API? # maybe just make it illegal to call messages from raw API?
# That or figure out a way to always set it directly. # That or figure out a way to always set it directly.
if self._client:
return await self._client.pin_message( return await self._client.pin_message(
await self.get_input_chat(), self.id, notify=notify, pm_oneside=pm_oneside) await self.get_input_chat(), self.id, notify=notify, pm_oneside=pm_oneside)
@ -1232,7 +1230,6 @@ class Message(ChatGetter, SenderGetter):
`telethon.client.messages.MessageMethods.unpin_message` `telethon.client.messages.MessageMethods.unpin_message`
with both ``entity`` and ``message`` already set. with both ``entity`` and ``message`` already set.
""" """
if self._client:
return await self._client.unpin_message( return await self._client.unpin_message(
await self.get_input_chat(), self.id) await self.get_input_chat(), self.id)
@ -1257,9 +1254,6 @@ class Message(ChatGetter, SenderGetter):
Re-fetches this message to reload the sender and chat entities, Re-fetches this message to reload the sender and chat entities,
along with their input versions. along with their input versions.
""" """
if not self._client:
return
try: try:
chat = await self.get_input_chat() if self.is_channel else None chat = await self.get_input_chat() if self.is_channel else None
msg = await self._client.get_messages(chat, ids=self.id) msg = await self._client.get_messages(chat, ids=self.id)
@ -1284,7 +1278,7 @@ class Message(ChatGetter, SenderGetter):
""" """
Helper methods to set the buttons given the input sender and chat. Helper methods to set the buttons given the input sender and chat.
""" """
if self._client and isinstance(self.reply_markup, ( if isinstance(self.reply_markup, (
_tl.ReplyInlineMarkup, _tl.ReplyKeyboardMarkup)): _tl.ReplyInlineMarkup, _tl.ReplyKeyboardMarkup)):
self._buttons = [[ self._buttons = [[
MessageButton(self._client, button, chat, bot, self.id) MessageButton(self._client, button, chat, bot, self.id)
@ -1300,7 +1294,7 @@ class Message(ChatGetter, SenderGetter):
to know what bot we want to start. Raises ``ValueError`` if the bot to know what bot we want to start. Raises ``ValueError`` if the bot
cannot be found but is needed. Returns `None` if it's not needed. cannot be found but is needed. Returns `None` if it's not needed.
""" """
if self._client and not isinstance(self.reply_markup, ( if not isinstance(self.reply_markup, (
_tl.ReplyInlineMarkup, _tl.ReplyKeyboardMarkup)): _tl.ReplyInlineMarkup, _tl.ReplyKeyboardMarkup)):
return None return None