hikari
hikari copied to clipboard
Implement Polls
Summary
Implementing built-in Discord polls.
Checklist
- [ ] I have run
noxand all the pipelines have passed. - [ ] I have made unittests according to the code I have added/modified/deleted.
Related issues
Resolves #1886
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,
}
CI is green. Let's end this.
@davfsa One final thing is left, waiting for your input
Closing this in favor of #2219