pyrofork: Refactor Poll

Signed-off-by: wulan17 <wulan17@nusantararom.org>
This commit is contained in:
wulan17 2024-11-29 13:19:57 +07:00
parent d7018d8ed1
commit c9bbeec525
No known key found for this signature in database
GPG key ID: 318CD6CD3A6AC0A5
2 changed files with 25 additions and 16 deletions

View file

@ -158,14 +158,22 @@ class Poll(Object, Update):
text=answer.text.text, text=answer.text.text,
voter_count=voter_count, voter_count=voter_count,
data=answer.option, data=answer.option,
entities=option_entities, entities=option_entities
client=client
) )
) )
q_entities = [types.MessageEntity._parse(client, entity, {}) for entity in poll.question.entities] if poll.question.entities else [] q_entities = [types.MessageEntity._parse(client, entity, {}) for entity in poll.question.entities] if poll.question.entities else []
question_entities = types.List(filter(lambda x: x is not None, q_entities)) question_entities = types.List(filter(lambda x: x is not None, q_entities))
recent_voters = []
for user in poll_results.recent_voters:
try:
voter = types.User._parse(client, users.get(user.user_id, None))
except Exception:
pass
else:
recent_voters.append(voter)
return Poll( return Poll(
id=str(poll.id), id=str(poll.id),
question=poll.question.text, question=poll.question.text,
@ -185,10 +193,7 @@ class Poll(Object, Update):
] if poll_results.solution_entities else None, ] if poll_results.solution_entities else None,
open_period=poll.close_period, open_period=poll.close_period,
close_date=utils.timestamp_to_datetime(poll.close_date), close_date=utils.timestamp_to_datetime(poll.close_date),
recent_voters=[ recent_voters=recent_voters if len(recent_voters) > 0 else None,
await client.get_users(user.user_id)
for user in poll_results.recent_voters
] if poll_results.recent_voters else None,
client=client client=client
) )
@ -217,11 +222,20 @@ class Poll(Object, Update):
types.PollOption( types.PollOption(
text="", text="",
voter_count=result.voters, voter_count=result.voters,
data=result.option, data=result.option
client=client
) )
) )
recent_voters = []
if update.results.recent_voters:
for user in update.results.recent_voters:
try:
voter = types.User._parse(client, users.get(user.user_id, None))
except Exception:
pass
else:
recent_voters.append(voter)
return Poll( return Poll(
id=str(update.poll_id), id=str(update.poll_id),
question="", question="",
@ -230,10 +244,7 @@ class Poll(Object, Update):
is_closed=False, is_closed=False,
chosen_option_id=chosen_option_id, chosen_option_id=chosen_option_id,
correct_option_id=correct_option_id, correct_option_id=correct_option_id,
recent_voters=[ recent_voters=recent_voters if len(recent_voters) > 0 else None,
types.User._parse(client, users.get(user.user_id, None))
for user in update.results.recent_voters
] if update.results.recent_voters else None,
client=client client=client
) )

View file

@ -41,15 +41,13 @@ class PollOption(Object):
""" """
def __init__( def __init__(
self, self: "pyrogram.Client",
*,
client: "pyrogram.Client" = None,
text: str, text: str,
voter_count: int = 0, voter_count: int = 0,
data: bytes = None, data: bytes = None,
entities: Optional[List["pyrogram.types.MessageEntity"]] = None, entities: Optional[List["pyrogram.types.MessageEntity"]] = None,
): ):
super().__init__(client) super().__init__(self)
self.text = text self.text = text
self.voter_count = voter_count self.voter_count = voter_count