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

feat: add new poll system message type #1213

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions changelog/1212.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add new :attr:`~MessageType.poll_result` message type.
1 change: 1 addition & 0 deletions disnake/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ class MessageType(Enum):
guild_incident_alert_mode_disabled = 37
guild_incident_report_raid = 38
guild_incident_report_false_alarm = 39
poll_result = 46


class PartyType(Enum):
Expand Down
29 changes: 29 additions & 0 deletions disnake/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,35 @@ def system_content(self) -> Optional[str]:
if self.type is MessageType.guild_incident_report_false_alarm:
return f"{self.author.name} resolved an Activity Alert."

if self.type is MessageType.poll_result:
if not self.embeds:
return

poll_result_embed = self.embeds[0]
poll_embed_fields: Dict[str, str] = {}
if not poll_result_embed._fields:
return

for field in poll_result_embed._fields:
poll_embed_fields[field["name"]] = field["value"]

# should never be none
question = poll_embed_fields["poll_question_text"]
# should never be none
total_votes = poll_embed_fields["total_votes"]
winning_answer = poll_embed_fields.get("victor_answer_text")
winning_answer_votes = poll_embed_fields.get("victor_answer_votes")
msg = f"{self.author.display_name}'s poll {question} has closed."

if winning_answer and winning_answer_votes:
msg += (
f"\n\n{winning_answer}"
f"\nWinning answer • {(100 * int(winning_answer_votes)) // int(total_votes)}%"
)
else:
msg += f" With {total_votes} total votes."
return msg

# in the event of an unknown or unsupported message type, we return nothing
return None

Expand Down
2 changes: 1 addition & 1 deletion disnake/types/embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class EmbedAuthor(TypedDict):
proxy_icon_url: NotRequired[str]


EmbedType = Literal["rich", "image", "video", "gifv", "article", "link"]
EmbedType = Literal["rich", "image", "video", "gifv", "article", "link", "poll_result"]


class Embed(TypedDict, total=False):
Expand Down
2 changes: 1 addition & 1 deletion disnake/types/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class RoleSubscriptionData(TypedDict):


# fmt: off
MessageType = Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 36, 37, 38, 39]
MessageType = Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 36, 37, 38, 39, 46]
# fmt: on


Expand Down
7 changes: 7 additions & 0 deletions docs/api/messages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,12 @@ MessageType

.. versionadded:: 2.10

.. attribute:: poll_result

The system message denoting that a poll expired, announcing the most voted answer.

.. versionadded:: 2.10

PollLayoutType
~~~~~~~~~~~~~~

Expand All @@ -414,6 +420,7 @@ PollLayoutType

The default poll layout type.


Events
------

Expand Down
Loading