diff --git a/hikari/impl/entity_factory.py b/hikari/impl/entity_factory.py index 5b545bdeb..1ddb1e0c3 100644 --- a/hikari/impl/entity_factory.py +++ b/hikari/impl/entity_factory.py @@ -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, diff --git a/hikari/polls.py b/hikari/polls.py index b7c6f3a7f..018b6369f 100644 --- a/hikari/polls.py +++ b/hikari/polls.py @@ -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.""" @@ -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. @@ -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