Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/polls #1

Merged
merged 2 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions hikari/impl/entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3800,20 +3800,24 @@ def _serialize_poll_partial_emoji(self, emoji: typing.Optional[emoji_models.Emoj
return {"name": emoji.name, "id": emoji.name}
return {}

def serialize_poll(self, poll: poll_models.PollCreate) -> data_binding.JSONObject:
answers = []
for answer_id, answer in poll.answers.items():
# FIXME: Typing is **very** dodgy here. Revise this before shipping.
poll_media: typing.MutableMapping[str, typing.Any] = {"text": answer.poll_media.text}
def _serialize_poll_media(self, poll_media: poll_models.PollMedia) -> data_binding.JSONObject:
# FIXME: Typing is **very** dodgy here. Revise this before shipping.

serialised_poll_media: typing.MutableMapping[str, typing.Any] = {"text": poll_media.text}

answer_emoji = self._serialize_poll_partial_emoji(answer.poll_media.emoji)
if answer_emoji:
poll_media["emoji"] = answer_emoji
answer_emoji = self._serialize_poll_partial_emoji(poll_media.emoji)
if answer_emoji:
serialised_poll_media["emoji"] = answer_emoji

answers.append({"answer_id": answer_id, "poll_media": poll_media})
return serialised_poll_media

def serialize_poll(self, poll: poll_models.PollCreate) -> data_binding.JSONObject:
answers: typing.MutableSequence[typing.Any] = []
for answer_id, answer in poll.answers.items():
answers.append({"answer_id": answer_id, "poll_media": self._serialize_poll_media(answer.poll_media)})

return {
"question": poll.question.text,
"question": self._serialize_poll_media(poll.question),
"answers": answers,
"expiry": poll.duration,
"allow_multiple_options": poll.allow_multiselect,
Expand Down
14 changes: 4 additions & 10 deletions hikari/polls.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,6 @@ def __init__(self, question: str, allow_multiselect: bool, layout_type: typing.U
# due to hashmap overhead.
self._answers: typing.MutableMapping[int, PollAnswer] = {} # TODO: Do we need to set to None?

# Counter to keep track of the answer IDs
#
# Discord (at the time of writing this) starts at 1, I'm opting to follow
# the behaviour.
self._counter = 1

@property
def question(self) -> PollMedia:
"""Returns the question of the poll."""
Expand Down Expand Up @@ -201,7 +195,7 @@ def duration(self) -> int:
def duration(self, value: int) -> None:
self._duration = value

def add_answer(self, text: str, emoji: typing.Optional[Emoji]) -> PartialPoll:
def add_answer(self, answer_id: int, text: str, emoji: typing.Optional[Emoji]) -> PartialPoll:
"""
Add an answer to the poll.

Expand All @@ -217,13 +211,13 @@ def add_answer(self, text: str, emoji: typing.Optional[Emoji]) -> PartialPoll:
PartialPoll
This poll. Allows for call chaining.
"""
answer_id = self._counter
self._counter += 1

new_answer = PollAnswer(
answer_id=answer_id, poll_media=PollMedia(text=text, emoji=_ensure_optional_emoji(emoji))
)
self._answers[answer_id] = new_answer

# FIXME: Not sure if this is ideal, but this will override an item, if it has the same answer id.
self._answers.update({answer_id: new_answer})

return self

Expand Down