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

Merged
merged 14 commits into from
Nov 21, 2024
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 ``poll_result`` message type.
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions disnake/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,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
20 changes: 20 additions & 0 deletions disnake/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

from .abc import GuildChannel, MessageableChannel, Snowflake
from .channel import DMChannel
from .embeds import _EmbedFieldProxy
from .guild import GuildMessageable
from .mentions import AllowedMentions
from .role import Role
Expand Down Expand Up @@ -1538,6 +1539,25 @@ 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:
poll_result_embed = self.embeds[0]
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved
get_field: Callable[[str], Optional[_EmbedFieldProxy]] = lambda field_name: utils.get(
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved
poll_result_embed.fields, name=field_name
)
# should never be none
question = get_field("poll_question_text").value # type: ignore
# should never be none
total_votes = get_field("total_votes").value # type: ignore
winning_answer = get_field("victor_answer_text")
winning_answer_votes = get_field("victor_answer_votes")
msg = f"{self.author.global_name}'s poll {question} has closed."
if winning_answer and winning_answer_votes:
msg += (
f" {winning_answer.value} won with {winning_answer_votes.value} "
f"({(100 * int(winning_answer_votes.value)) // int(total_votes)}%)." # type: ignore
)
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 @@ -78,7 +78,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
6 changes: 6 additions & 0 deletions docs/api/messages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,12 @@ MessageType

.. versionadded:: 2.10

.. attribute:: poll_result

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

.. versionadded:: 2.10

Events
------

Expand Down
Loading