hikari icon indicating copy to clipboard operation
hikari copied to clipboard

Implement Polls

Open PythonTryHard opened this issue 1 year ago • 3 comments

Summary

Implementing built-in Discord polls.

Checklist

  • [ ] I have run nox and all the pipelines have passed.
  • [ ] I have made unittests according to the code I have added/modified/deleted.

Related issues

Resolves #1886

PythonTryHard avatar May 19 '24 12:05 PythonTryHard

Okay, I can't seem to push anything to GH, so I will just send it here.

hikari/impl/entity_factory.py - This actually allows for the payload to be serialised properly, by converting the question from a poll media object, to a payload (plus, same with questions)

    def _serialize_poll_partial_emoji(self, emoji: typing.Optional[emoji_models.Emoji]) -> data_binding.JSONObject:
        if isinstance(emoji, emoji_models.UnicodeEmoji):
            return {"name": emoji.name}
        elif isinstance(emoji, emoji_models.CustomEmoji):
            return {"name": emoji.name, "id": emoji.name}
        return {}

    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(poll_media.emoji)
        if answer_emoji:
            serialised_poll_media["emoji"] = answer_emoji

        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": self._serialize_poll_media(poll.question),
            "answers": answers,
            "expiry": poll.duration,
            "allow_multiple_options": poll.allow_multiselect,
            "layout_type": poll.layout_type,
        }

mplatypus avatar May 27 '24 11:05 mplatypus

CI is green. Let's end this.

PythonTryHard avatar Aug 19 '24 06:08 PythonTryHard

@davfsa One final thing is left, waiting for your input

PythonTryHard avatar Aug 19 '24 06:08 PythonTryHard

Closing this in favor of #2219

davfsa avatar Mar 18 '25 20:03 davfsa