Revisit codebase to add missing async/await

This commit is contained in:
Lonami Exo
2018-06-14 17:09:20 +02:00
parent 1247d050ab
commit 908dfa148b
9 changed files with 73 additions and 111 deletions

View File

@@ -164,15 +164,16 @@ class ChatAction(EventBuilder):
self.action_message = custom.Message(
client, self.action_message, self._entities, None)
def respond(self, *args, **kwargs):
async def respond(self, *args, **kwargs):
"""
Responds to the chat action message (not as a reply). Shorthand for
`telethon.telegram_client.TelegramClient.send_message` with
``entity`` already set.
"""
return self._client.send_message(self.input_chat, *args, **kwargs)
return await self._client.send_message(
await self.input_chat, *args, **kwargs)
def reply(self, *args, **kwargs):
async def reply(self, *args, **kwargs):
"""
Replies to the chat action message (as a reply). Shorthand for
`telethon.telegram_client.TelegramClient.send_message` with
@@ -181,12 +182,13 @@ class ChatAction(EventBuilder):
Has the same effect as `respond` if there is no message.
"""
if not self.action_message:
return self.respond(*args, **kwargs)
return await self.respond(*args, **kwargs)
kwargs['reply_to'] = self.action_message.id
return self._client.send_message(self.input_chat, *args, **kwargs)
return await self._client.send_message(
await self.input_chat, *args, **kwargs)
def delete(self, *args, **kwargs):
async def delete(self, *args, **kwargs):
"""
Deletes the chat action message. You're responsible for checking
whether you have the permission to do so, or to except the error
@@ -196,13 +198,14 @@ class ChatAction(EventBuilder):
Does nothing if no message action triggered this event.
"""
if self.action_message:
return self._client.delete_messages(self.input_chat,
[self.action_message],
*args, **kwargs)
if not self.action_message:
return
return await self._client.delete_messages(
await self.input_chat, [self.action_message], *args, **kwargs)
@property
def pinned_message(self):
async def pinned_message(self):
"""
If ``new_pin`` is ``True``, this returns the
`telethon.tl.custom.message.Message` object that was pinned.
@@ -210,8 +213,8 @@ class ChatAction(EventBuilder):
if self._pinned_message == 0:
return None
if isinstance(self._pinned_message, int) and self.input_chat:
r = self._client(functions.channels.GetMessagesRequest(
if isinstance(self._pinned_message, int) and await self.input_chat:
r = await self._client(functions.channels.GetMessagesRequest(
self._input_chat, [self._pinned_message]
))
try:
@@ -227,50 +230,48 @@ class ChatAction(EventBuilder):
return self._pinned_message
@property
def added_by(self):
async def added_by(self):
"""
The user who added ``users``, if applicable (``None`` otherwise).
"""
if self._added_by and not isinstance(self._added_by, types.User):
self._added_by =\
self._entities.get(utils.get_peer_id(self._added_by))
if not self._added_by:
self._added_by = self._client.get_entity(self._added_by)
aby = self._entities.get(utils.get_peer_id(self._added_by))
if not aby:
aby = await self._client.get_entity(self._added_by)
self._added_by = aby
return self._added_by
@property
def kicked_by(self):
async def kicked_by(self):
"""
The user who kicked ``users``, if applicable (``None`` otherwise).
"""
if self._kicked_by and not isinstance(self._kicked_by, types.User):
self._kicked_by =\
self._entities.get(utils.get_peer_id(self._kicked_by))
if not self._kicked_by:
self._kicked_by = self._client.get_entity(self._kicked_by)
kby = self._entities.get(utils.get_peer_id(self._kicked_by))
if kby:
kby = await self._client.get_entity(self._kicked_by)
self._kicked_by = kby
return self._kicked_by
@property
def user(self):
async def user(self):
"""
The first user that takes part in this action (e.g. joined).
Might be ``None`` if the information can't be retrieved or
there is no user taking part.
"""
if self.users:
if await self.users:
return self._users[0]
@property
def input_user(self):
async def input_user(self):
"""
Input version of the ``self.user`` property.
"""
if self.input_users:
if await self.input_users:
return self._input_users[0]
@property
@@ -282,7 +283,7 @@ class ChatAction(EventBuilder):
return utils.get_peer_id(self._user_peers[0])
@property
def users(self):
async def users(self):
"""
A list of users that take part in this action (e.g. joined).
@@ -302,7 +303,7 @@ class ChatAction(EventBuilder):
missing.append(peer)
try:
missing = self._client.get_entity(missing)
missing = await self._client.get_entity(missing)
except (TypeError, ValueError):
missing = []
@@ -311,7 +312,7 @@ class ChatAction(EventBuilder):
return self._users
@property
def input_users(self):
async def input_users(self):
"""
Input version of the ``self.users`` property.
"""
@@ -319,9 +320,9 @@ class ChatAction(EventBuilder):
self._input_users = []
for peer in self._user_peers:
try:
self._input_users.append(self._client.get_input_entity(
peer
))
self._input_users.append(
await self._client.get_input_entity(peer)
)
except (TypeError, ValueError):
pass
return self._input_users