Add support for Polls

Co-authored-by: owocado <24418520+owocado@users.noreply.github.com>
Co-authored-by: Josh <8677174+bijij@users.noreply.github.com>
Co-authored-by: Trevor Flahardy <75498301+trevorflahardy@users.noreply.github.com>
This commit is contained in:
DA344
2024-05-10 12:14:12 +02:00
committed by GitHub
parent a1206dfde8
commit e43bd8692c
19 changed files with 1097 additions and 1 deletions

View File

@ -63,6 +63,7 @@ from .mixins import Hashable
from .sticker import StickerItem, GuildSticker
from .threads import Thread
from .channel import PartialMessageable
from .poll import Poll
if TYPE_CHECKING:
from typing_extensions import Self
@ -1464,6 +1465,7 @@ class PartialMessage(Hashable):
view: View = ...,
suppress_embeds: bool = ...,
silent: bool = ...,
poll: Poll = ...,
) -> Message:
...
@ -1484,6 +1486,7 @@ class PartialMessage(Hashable):
view: View = ...,
suppress_embeds: bool = ...,
silent: bool = ...,
poll: Poll = ...,
) -> Message:
...
@ -1504,6 +1507,7 @@ class PartialMessage(Hashable):
view: View = ...,
suppress_embeds: bool = ...,
silent: bool = ...,
poll: Poll = ...,
) -> Message:
...
@ -1524,6 +1528,7 @@ class PartialMessage(Hashable):
view: View = ...,
suppress_embeds: bool = ...,
silent: bool = ...,
poll: Poll = ...,
) -> Message:
...
@ -1558,6 +1563,30 @@ class PartialMessage(Hashable):
return await self.channel.send(content, reference=self, **kwargs)
async def end_poll(self) -> Message:
"""|coro|
Ends the :class:`Poll` attached to this message.
This can only be done if you are the message author.
If the poll was successfully ended, then it returns the updated :class:`Message`.
Raises
------
~discord.HTTPException
Ending the poll failed.
Returns
-------
:class:`.Message`
The updated message.
"""
data = await self._state.http.end_poll(self.channel.id, self.id)
return Message(state=self._state, channel=self.channel, data=data)
def to_reference(self, *, fail_if_not_exists: bool = True) -> MessageReference:
"""Creates a :class:`~discord.MessageReference` from the current message.
@ -1728,6 +1757,10 @@ class Message(PartialMessage, Hashable):
interaction_metadata: Optional[:class:`.MessageInteractionMetadata`]
The metadata of the interaction that this message is a response to.
.. versionadded:: 2.4
poll: Optional[:class:`Poll`]
The poll attached to this message.
.. versionadded:: 2.4
"""
@ -1764,6 +1797,7 @@ class Message(PartialMessage, Hashable):
'application_id',
'position',
'interaction_metadata',
'poll',
)
if TYPE_CHECKING:
@ -1803,6 +1837,15 @@ class Message(PartialMessage, Hashable):
self.application_id: Optional[int] = utils._get_as_snowflake(data, 'application_id')
self.stickers: List[StickerItem] = [StickerItem(data=d, state=state) for d in data.get('sticker_items', [])]
# This updates the poll so it has the counts, if the message
# was previously cached.
self.poll: Optional[Poll] = state._get_poll(self.id)
if self.poll is None:
try:
self.poll = Poll._from_data(data=data['poll'], message=self, state=state)
except KeyError:
pass
try:
# if the channel doesn't have a guild attribute, we handle that
self.guild = channel.guild