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

Add support for Polls #9759

Merged
merged 173 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 76 commits
Commits
Show all changes
173 commits
Select commit Hold shift + click to select a range
86f9860
Major changes to add poll feature
DA-344 Mar 19, 2024
fd5c37a
Merge branch 'master' of https://github.com/Rapptz/discord.py into polls
DA-344 Mar 19, 2024
f0eca90
Removed unnecessary slotted attributes
DA-344 Mar 19, 2024
7cb5911
Slotted PollAnswer and added poll param to async webhook handle messa…
DA-344 Mar 19, 2024
c660a7e
Added Gobot1234 suggested changes and PollAnswer to __all__
DA-344 Mar 19, 2024
5e58841
Commit owocado review
DA-344 Mar 19, 2024
263b7a1
Updated all the docstrings for send methods and added poll support fo…
DA-344 Mar 22, 2024
523faca
Merge branch 'master' of https://github.com/Rapptz/discord.py into polls
DA-344 Mar 22, 2024
1d9bd58
Added gateway events, updated docs and some more things
DA-344 Mar 22, 2024
b086bae
removed personal things from .gitignore
DA-344 Mar 22, 2024
e670095
Updated add_answer and added final blank line
DA-344 Mar 23, 2024
e6151d6
Update context.py 's send method docstring
DA-344 Mar 23, 2024
0c12a53
Added new overloads for all the send methods, updated poll object
DA-344 Mar 24, 2024
51a2e0f
Fixed a minor error in Poll Expiry handle
DA-344 Mar 24, 2024
1cadca0
Major changes to PollAnswers
DA-344 Mar 24, 2024
9f3ddc9
Polls cannot have message references
DA-344 Mar 26, 2024
85cdffc
Added new PollDuration's and updated results for polls
DA-344 Mar 27, 2024
07b21db
Removed answer param in on_poll_vote_... events docs
DA-344 Mar 27, 2024
0a61840
Updated 'id' param in Poll.get_answer
DA-344 Mar 27, 2024
e895389
Updated poll events docs, added answer parameter to poll events, chan…
DA-344 Mar 27, 2024
9d6ad95
Black polls files
DA-344 Mar 27, 2024
74c8efc
Fixed poll expiry route
DA-344 Mar 27, 2024
3444f80
Updated webhooks & interactions to support polls, updated all `reply`…
DA-344 Mar 27, 2024
6c7c49b
Updated polls docstring
DA-344 Mar 27, 2024
b9f74d8
Updated Webhooks polls docstring in send
DA-344 Mar 28, 2024
09f5437
Updated Raw Poll Event Model (its reusable)
DA-344 Mar 29, 2024
cba11b6
Testing commit to poll docs
DA-344 Apr 1, 2024
9bf1c3d
Added NamedTuple for PollMedia, and some changes to PollAnswer
DA-344 Apr 1, 2024
a5514e1
Added shortcut for Message.poll.end()
DA-344 Apr 2, 2024
0a38090
Bots support any duration polls if hours are in range 1 - 168!
DA-344 Apr 3, 2024
27bc8c7
Added new properties to PollAnswerCount & Poll
DA-344 Apr 3, 2024
89c3501
Added a new class for BaseMethods both answers share
DA-344 Apr 3, 2024
c0ca0fd
ACTUALLY fixing the types (once and for all)
DA-344 Apr 3, 2024
03e3ef0
Changed the users() PollAnswerBase AsyncIterator to also return Membe…
DA-344 Apr 3, 2024
96dfd72
Updated Poll object
DA-344 Apr 5, 2024
4094bf9
Changes to Poll Docs
DA-344 Apr 5, 2024
ca155da
Added PollMedia to docs
DA-344 Apr 8, 2024
824b003
Updated polls
DA-344 Apr 8, 2024
7456255
Added ValueError exception if polls have 0 or >10 answers
DA-344 Apr 8, 2024
cd821a0
Fixed PollMessage error
DA-344 Apr 11, 2024
1e9a1f6
Added newline from imports to __all__
DA-344 Apr 11, 2024
c4f9951
Updated some polls things
DA-344 Apr 11, 2024
d3cc82b
Updated the end_poll method
DA-344 Apr 11, 2024
7637d93
Updated poll.py file
DA-344 Apr 11, 2024
f7462ae
emoji was converted to a utils.cached_property by mistake lol
DA-344 Apr 11, 2024
cca6f9b
Grammar errors
DA-344 Apr 11, 2024
ddefc1f
Added versionadded to raw poll events
DA-344 Apr 11, 2024
8989529
Cannot override __slots__
DA-344 Apr 11, 2024
0419f3d
Fixed PollAnswer emojis
DA-344 Apr 13, 2024
c66bc51
Fixed an error where Message.poll would raise an error
DA-344 Apr 13, 2024
b754635
Added None as default value for poll, then set it later to prevent er…
DA-344 Apr 13, 2024
9193f70
Fixes to polls
DA-344 Apr 13, 2024
bd1e203
Moved duration errors to handle_message_parameters
DA-344 Apr 13, 2024
11f42ca
Fixed an error with the duration calculation when calling _from_data
DA-344 Apr 13, 2024
87e583d
Added overload for poll_vote_add/poll_vote_remove events
DA-344 Apr 13, 2024
d190bfb
Updated return type (forgot this should return tuples for multiple args)
DA-344 Apr 13, 2024
aadaea6
Added poll intents
DA-344 Apr 13, 2024
4ef2222
Moved Poll, PollAnswer and PollMedia to the data classes section, Pol…
DA-344 Apr 13, 2024
2410fed
Made poll a property instead of an attribute
DA-344 Apr 13, 2024
c48a24c
Added raw_poll_vote events to poll intents docstrings
DA-344 Apr 13, 2024
a4126c8
Fixed attributetable lol
DA-344 Apr 13, 2024
f4edabc
Fixed end_poll trying to replace a poll property instead of _poll att…
DA-344 Apr 13, 2024
e687146
Removed unnecessary type: ignore comments, removed PollMessage type a…
DA-344 Apr 13, 2024
aa75030
Fixed Context.send docstring
DA-344 Apr 13, 2024
648ada7
Updated poll intents docstrings
DA-344 Apr 13, 2024
9ea8df2
Black poll.py
DA-344 Apr 13, 2024
618658f
Removed PollEmoji as it is a PartialEmoji
DA-344 Apr 14, 2024
0dfb675
Update Poll._message & Poll._state when sending a message
DA-344 Apr 14, 2024
3258c32
Updated poll param in handle_parameters and updated after & limit in …
DA-344 Apr 14, 2024
8bf15cf
Added Poll._update to update the state, message, counts etc. attribut…
DA-344 Apr 14, 2024
fab8524
Call Poll._update when message is sent
DA-344 Apr 14, 2024
9860f35
.
DA-344 Apr 14, 2024
5253697
Updated poll._counts, PollAnswer.get_count docstring
DA-344 Apr 14, 2024
f877b54
Fix Messageable.send docstring
DA-344 Apr 15, 2024
fda6135
Prevent Pyright error
DA-344 Apr 15, 2024
a96a6e2
Added raw_poll_vote wait_for overloads
DA-344 Apr 15, 2024
1253761
Making `send` update the poll state
DA-344 Apr 16, 2024
c3de7c9
Makin `send` update the poll state (as well)
DA-344 Apr 16, 2024
8c6ed29
Removed unneccessary typed dicts and made answer_id an int instead of…
DA-344 Apr 16, 2024
e5b6468
Updated answer_id in PollVoteActionType and guild_id from Optional to…
DA-344 Apr 16, 2024
7170a04
Updated imports for the types changes
DA-344 Apr 16, 2024
0541527
Fix typo in PollAnswerBase.users docstring
DA-344 Apr 16, 2024
c246e8e
Moved end_poll from Message to PartialMessage
DA-344 Apr 17, 2024
f50456c
Merge branch 'polls' of https://github.com/DA-344/d.py into polls
DA-344 Apr 17, 2024
b091943
Reworked PollAnswerBase.users
DA-344 Apr 17, 2024
c621992
Fixed imports & change poll from NotRequired[Optional[...]] to NotReq…
DA-344 Apr 17, 2024
9583e84
Removed unneccessary warning in Poll docstring
DA-344 Apr 17, 2024
de1947a
Fixed check type annotation in wait_for poll related overloads
DA-344 Apr 17, 2024
b3f753a
Prevented circular import
DA-344 Apr 17, 2024
a9bcf4a
Added send_polls permission
DA-344 Apr 18, 2024
3db12c1
Made Intents.poll an alias_flag_value instead of a flag_value
DA-344 Apr 18, 2024
8b74a1a
use dict[key] instead of dict.get(key)
DA-344 Apr 18, 2024
20f33a8
Added newline
DA-344 Apr 18, 2024
1f1edeb
Added commas
DA-344 Apr 18, 2024
028899b
Added a comma
DA-344 Apr 18, 2024
8a1ebf9
Added a space before #type: ignore 's
DA-344 Apr 18, 2024
84be92c
Added a comma
DA-344 Apr 18, 2024
2f9b9c4
Added commas
DA-344 Apr 18, 2024
635f69c
Removed #type: ignore \'s
DA-344 Apr 18, 2024
d93954f
Merge branch 'polls' of https://github.com/DA-344/d.py into polls
DA-344 Apr 18, 2024
f118958
Resolve merge conflicts + update from master
DA-344 Apr 18, 2024
c4d9cc5
Added warning if cached message does not have a poll
DA-344 Apr 18, 2024
127a212
Oops 😬
DA-344 Apr 18, 2024
a4a9e0e
Remove length of recieved data from limit
DA-344 Apr 20, 2024
8ff203d
Add comma
DA-344 Apr 20, 2024
7396cb8
Poll changes
DA-344 Apr 20, 2024
ce37704
Merge branch 'polls' of https://github.com/DA-344/d.py into polls
DA-344 Apr 20, 2024
def3d76
Add comma (again)
DA-344 Apr 20, 2024
cc8d9d4
Another comma :v
DA-344 Apr 20, 2024
6f64ab8
And another comma >:v
DA-344 Apr 20, 2024
3f0c5dd
Another comma, again🙂👍
DA-344 Apr 20, 2024
63a3fd6
" " before "#type: ignore"
DA-344 Apr 20, 2024
1bd623c
,
DA-344 Apr 20, 2024
2bae4a0
Simplify polls
DA-344 Apr 20, 2024
fac840e
Added PollMedia to docs
DA-344 Apr 21, 2024
5f4ba13
Merge branch 'polls' of https://github.com/DA-344/d.py into polls
DA-344 Apr 21, 2024
f2c4cdb
Added Poll.copy
DA-344 Apr 21, 2024
09a4c59
Changes PollAnswerBase.voters docstring
DA-344 Apr 21, 2024
a24f1b0
warning -> note
DA-344 Apr 21, 2024
f17546f
poll: Poll = MISSING -> poll: Optional[Poll] = None
DA-344 Apr 21, 2024
8a49c88
not in (MISSING, None)
DA-344 Apr 21, 2024
ef3b685
Updated warning message
DA-344 Apr 21, 2024
2df6f0d
Added _add_vote_count to add a vote count if possible
DA-344 Apr 21, 2024
c81261f
Black
DA-344 Apr 21, 2024
cc01fff
Black
DA-344 Apr 21, 2024
082a1b9
Updated Messageables overloads
DA-344 Apr 21, 2024
065abab
Removed unnecessary overload
DA-344 Apr 21, 2024
bc41755
Merge branch 'master' of https://github.com/Rapptz/discord.py into polls
DA-344 Apr 22, 2024
e904668
Added base property 'poll'
DA-344 Apr 22, 2024
b74d2a5
BREAKING CHANGE | Edited poll events
DA-344 Apr 22, 2024
0093e66
Updated docs to show the latest commits
DA-344 Apr 22, 2024
ded24b0
Updated wait_for overload
DA-344 Apr 22, 2024
022e511
TypeHinted payload var in PollMedia.to_dict
DA-344 Apr 22, 2024
26f39be
Removed _add_vote_coun
DA-344 Apr 22, 2024
b83f7e0
poll events now update the cached message poll vote count
DA-344 Apr 23, 2024
f34f400
Merged all functionalities into PollAnswer
DA-344 Apr 23, 2024
b9a8da3
Added Message._handle_poll_vote to update inner poll vote counts
DA-344 Apr 23, 2024
4ead3f2
Removed PollAnswerCount
DA-344 Apr 23, 2024
4ac3dcf
rm #type: ignore
DA-344 Apr 23, 2024
807a72f
Updated self._poll attr to set the cached message's poll, if any
DA-344 Apr 24, 2024
004cefc
Updated poll to handle self_voted and PollAnswer repr to show its med…
DA-344 Apr 24, 2024
be53f23
Updated poll vote parser to update poll vote counts manually
DA-344 Apr 24, 2024
6a5e306
is not None
DA-344 Apr 25, 2024
e881dc7
reworked emoji dict in PollMedia.to_dict
DA-344 Apr 25, 2024
7038423
Return full answer payload instead of media
DA-344 Apr 25, 2024
c68ae70
Update poll._to_dict() with the poll answer changes
DA-344 Apr 25, 2024
7e427bc
Update Poll._to_dict
DA-344 Apr 25, 2024
ee1250f
Updated with @bijij suggestions
DA-344 Apr 25, 2024
98eda87
Updated Poll._from_data
DA-344 Apr 25, 2024
7940d43
Updated property name & doc and is_finalized
DA-344 Apr 25, 2024
63b65d4
Fixed check type annotation in Client.wait_for
DA-344 Apr 25, 2024
78d8c70
Merge branch 'master' of https://github.com/Rapptz/discord.py into polls
DA-344 May 5, 2024
d964851
remove poll checks
DA-344 May 5, 2024
492751e
Updated send docstring
DA-344 May 5, 2024
c941cad
update message.py with Rapptz suggestions
DA-344 May 5, 2024
7a174e3
Added create_polls permission alias
DA-344 May 5, 2024
ca8ca38
applied Rapptz suggestions
DA-344 May 5, 2024
91134e8
updated PollVoteActionEvent docstring
DA-344 May 5, 2024
e4d41bf
s
DA-344 May 5, 2024
41151c2
..versionadded:: -> .. versionadded::
DA-344 May 5, 2024
dfb124a
Black
DA-344 May 5, 2024
82bb213
change polls pos
DA-344 May 5, 2024
deb19f3
correct pos if sorted alphabetically
DA-344 May 5, 2024
6b0374a
merge branch 'master' from https://github.com/Rapptz/discord.py
DA-344 May 6, 2024
d952eae
Add alias for send_polls in permission overwrites
DA-344 May 6, 2024
8b2515e
PollMedia.__init__
DA-344 May 8, 2024
cbfcd15
Merge branch 'master' of https://github.com/Rapptz/discord.py into polls
DA-344 May 8, 2024
cdadbb1
Message._poll -> Message.poll (removed poll property)
DA-344 May 8, 2024
e120721
Updated Poll
DA-344 May 8, 2024
2a6c4ea
Updated PollMedia docstring
DA-344 May 8, 2024
0138dca
Added poll attr in Message docstring
DA-344 May 8, 2024
8fbce83
Fixed self type annotation
DA-344 May 9, 2024
8adaf0f
Remove Poll.emoji (no need for it)
DA-344 May 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions discord/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
from .components import *
from .threads import *
from .automod import *
from .poll import *


class VersionInfo(NamedTuple):
Expand Down
19 changes: 19 additions & 0 deletions discord/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
VoiceChannel,
StageChannel,
)
from .poll import Poll
from .threads import Thread
from .ui.view import View
from .types.channel import (
Expand Down Expand Up @@ -1333,6 +1334,15 @@ class Messageable:
async def _get_channel(self) -> MessageableChannel:
raise NotImplementedError

@overload
async def send(
self,
*,
poll: Poll,
reference: Union[Message, MessageReference, PartialMessage] = ...,
) -> Message:
...

@overload
async def send(
self,
Expand Down Expand Up @@ -1431,6 +1441,7 @@ async def send(
view: Optional[View] = None,
suppress_embeds: bool = False,
silent: bool = False,
poll: Poll = MISSING,
) -> Message:
"""|coro|

Expand Down Expand Up @@ -1516,6 +1527,10 @@ async def send(
in the UI, but will not actually send a notification.

.. versionadded:: 2.2
poll: :class:`~discord.Poll`
The attached poll for this message.
DA-344 marked this conversation as resolved.
Show resolved Hide resolved

.. versionadded:: 2.4

Raises
--------
Expand Down Expand Up @@ -1582,13 +1597,17 @@ async def send(
stickers=sticker_ids,
view=view,
flags=flags,
poll=poll
DA-344 marked this conversation as resolved.
Show resolved Hide resolved
) as params:
data = await state.http.send_message(channel.id, params=params)

ret = state.create_message(channel=channel, data=data)
if view and not view.is_finished():
state.store_view(view, ret.id)

if poll is not MISSING:
poll._update(ret)

if delete_after is not None:
await ret.delete(delay=delete_after)
return ret
Expand Down
24 changes: 24 additions & 0 deletions discord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
RawThreadMembersUpdate,
RawThreadUpdateEvent,
RawTypingEvent,
RawPollVoteActionEvent,
)
from .reaction import Reaction
from .role import Role
Expand All @@ -116,6 +117,7 @@
from .ui.item import Item
from .voice_client import VoiceProtocol
from .audit_logs import AuditLogEntry
from .poll import PollAnswer


# fmt: off
Expand Down Expand Up @@ -1834,6 +1836,28 @@ async def wait_for(
) -> Tuple[Context[Any], CommandError]:
...

@overload
async def wait_for(
self: Union[Bot, AutoShardedBot],
event: Literal['poll_vote_add', 'poll_vote_remove'],
/,
*,
check: Optional[Callable[[Context[Any]], bool]] = None,
timeout: Optional[float] = None,
) -> Tuple[Union[User, Member], Message, PollAnswer]:
...

@overload
async def wait_for(
self: Union[Bot, AutoShardedBot],
event: Literal['raw_poll_vote_add', 'raw_poll_vote_remove'],
/,
*,
check: Optional[Callable[[Context[Any]], bool]] = None,
timeout: Optional[float] = None,
) -> RawPollVoteActionEvent:
...

@overload
async def wait_for(
self,
Expand Down
5 changes: 5 additions & 0 deletions discord/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
'SKUType',
'EntitlementType',
'EntitlementOwnerType',
'PollLayoutType',
)


Expand Down Expand Up @@ -809,6 +810,10 @@ class EntitlementOwnerType(Enum):
user = 2


class PollLayoutType(Enum):
default = 1


def create_unknown_value(cls: Type[E], val: Any) -> E:
value_cls = cls._enum_value_cls_ # type: ignore # This is narrowed below
name = f'unknown_{val}'
Expand Down
30 changes: 28 additions & 2 deletions discord/ext/commands/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from discord.message import MessageReference, PartialMessage
from discord.ui import View
from discord.types.interactions import ApplicationCommandInteractionData
from discord.poll import Poll

from .cog import Cog
from .core import Command
Expand Down Expand Up @@ -623,6 +624,14 @@ async def send_help(self, *args: Any) -> Any:
except CommandError as e:
await cmd.on_help_command_error(self, e)

@overload
async def reply(
self,
*,
poll: Poll
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
poll: Poll
poll: Poll,

) -> Message:
...

@overload
async def reply(
self,
Expand Down Expand Up @@ -808,6 +817,15 @@ async def defer(self, *, ephemeral: bool = False) -> None:
if self.interaction:
await self.interaction.response.defer(ephemeral=ephemeral)

@overload
async def send(
self,
*,
poll: Poll,
reference: Union[Message, MessageReference, PartialMessage] = ...,
) -> Message:
...

@overload
async def send(
self,
Expand Down Expand Up @@ -911,6 +929,7 @@ async def send(
suppress_embeds: bool = False,
ephemeral: bool = False,
silent: bool = False,
poll: Poll = MISSING
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
poll: Poll = MISSING
poll: Poll = MISSING,

) -> Message:
"""|coro|

Expand Down Expand Up @@ -1000,6 +1019,11 @@ async def send(

.. versionadded:: 2.2

poll: :class:`~discord.Poll`
The attached poll for this message.
DA-344 marked this conversation as resolved.
Show resolved Hide resolved

.. versionadded:: 2.4

Raises
--------
~discord.HTTPException
Expand Down Expand Up @@ -1037,6 +1061,7 @@ async def send(
view=view,
suppress_embeds=suppress_embeds,
silent=silent,
poll=poll
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
poll=poll
poll=poll,

) # type: ignore # The overloads don't support Optional but the implementation does

# Convert the kwargs from None to MISSING to appease the remaining implementations
Expand All @@ -1052,6 +1077,7 @@ async def send(
'suppress_embeds': suppress_embeds,
'ephemeral': ephemeral,
'silent': silent,
'poll': poll
DA-344 marked this conversation as resolved.
Show resolved Hide resolved
}

if self.interaction.response.is_done():
Expand All @@ -1061,5 +1087,5 @@ async def send(
msg = await self.interaction.original_response()

if delete_after is not None:
await msg.delete(delay=delete_after)
return msg
await msg.delete(delay=delete_after) # type: ignore
return msg # type: ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await msg.delete(delay=delete_after) # type: ignore
return msg # type: ignore
await msg.delete(delay=delete_after) # type: ignore
return msg # type: ignore

51 changes: 51 additions & 0 deletions discord/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,57 @@ def auto_moderation_execution(self):
"""
return 1 << 21

@flag_value
def polls(self):
""":class:`bool`: Whether guild and direct messages poll related events are enabled.

This is a shortcut to set or get both :attr:`guild_polls` and :attr:`dm_polls`.

This corresponds to the following events:

- :func:`on_poll_vote_add` (both guilds and DMs)
- :func:`on_poll_vote_remove` (both guilds and DMs)
- :func:`on_raw_poll_vote_add` (both guilds and DMs)
- :func:`on_raw_poll_vote_remove` (both guilds and DMs)

.. versionadded:: 2.4
"""
return (1 << 24) | (1 << 25)

@flag_value
def guild_polls(self):
""":class:`bool`: Whether guild poll related events are enabled.

See also :attr:`dm_polls` and :attr:`polls`.

This corresponds to the following events:

- :func:`on_poll_vote_add` (only for guilds)
- :func:`on_poll_vote_remove` (only for guilds)
- :func:`on_raw_poll_vote_add` (only for guilds)
- :func:`on_raw_poll_vote_remove` (only for guilds)

.. versionadded:: 2.4
"""
return 1 << 24

@flag_value
def dm_polls(self):
""":class:`bool`: Whether direct messages poll related events are enabled.

See also :attr:`guild_polls` and :attr:`polls`.

This corresponds to the following events:

- :func:`on_poll_vote_add` (only for DMs)
- :func:`on_poll_vote_remove` (only for DMs)
- :func:`on_raw_poll_vote_add` (only for DMs)
- :func:`on_raw_poll_vote_remove` (only for DMs)

.. versionadded:: 2.4
"""
return 1 << 25


@fill_with_flags()
class MemberCacheFlags(BaseFlags):
Expand Down
47 changes: 47 additions & 0 deletions discord/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
from .embeds import Embed
from .message import Attachment
from .flags import MessageFlags
from .poll import Poll

from .types import (
appinfo,
Expand All @@ -91,6 +92,7 @@
sticker,
welcome_screen,
sku,
poll
DA-344 marked this conversation as resolved.
Show resolved Hide resolved
)
from .types.snowflake import Snowflake, SnowflakeList

Expand Down Expand Up @@ -154,6 +156,7 @@ def handle_message_parameters(
thread_name: str = MISSING,
channel_payload: Dict[str, Any] = MISSING,
applied_tags: Optional[SnowflakeList] = MISSING,
poll: Poll = MISSING
DA-344 marked this conversation as resolved.
Show resolved Hide resolved
) -> MultipartParameters:
if files is not MISSING and file is not MISSING:
raise TypeError('Cannot mix file and files keyword arguments.')
Expand Down Expand Up @@ -256,6 +259,13 @@ def handle_message_parameters(
}
payload.update(channel_payload)

if poll is not MISSING:
if len(poll) == 0 or len(poll) > 10:
raise ValueError('Poll must contain between 1 and 10 answers')
if poll._hours_duration < 1 or poll._hours_duration > 168:
raise ValueError('Polls duration must be between 1 hour and 7 days')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure there's much benefit to having the library validate this.

Suggested change
if len(poll) == 0 or len(poll) > 10:
raise ValueError('Poll must contain between 1 and 10 answers')
if poll._hours_duration < 1 or poll._hours_duration > 168:
raise ValueError('Polls duration must be between 1 hour and 7 days')

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to reiterate this, I'm still not sure we should be doing this.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, the library delegates this stuff to the server side since it's the source of truth. It only validates when the trade-off for user experience is better (e.g. discord.ui). This is not true for polls to me.

payload['poll'] = poll._to_dict() # type: ignore
DA-344 marked this conversation as resolved.
Show resolved Hide resolved

multipart = []
if files:
multipart.append({'name': 'payload_json', 'value': utils._to_json(payload)})
Expand Down Expand Up @@ -2502,6 +2512,43 @@ def edit_application_info(self, *, reason: Optional[str], payload: Any) -> Respo

payload = {k: v for k, v in payload.items() if k in valid_keys}
return self.request(Route('PATCH', '/applications/@me'), json=payload, reason=reason)

def get_poll_answer_voters(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def get_poll_answer_voters(
def get_poll_answer_voters(

self,
channel_id: Snowflake,
message_id: Snowflake,
answer_id: Snowflake,
after: Optional[Snowflake] = None,
limit: Optional[int] = None,
) -> Response[poll.PollAnswerVoters]:
params = {}

if after:
params['after'] = int(after)

if limit:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
params['after'] = int(after)
if limit:
params['after'] = int(after)
if limit:

DA-344 marked this conversation as resolved.
Show resolved Hide resolved
params['limit'] = limit

return self.request(
Route(
'GET',
'/channels/{channel_id}/polls/{message_id}/answers/{answer_id}',
channel_id=channel_id,
message_id=message_id,
answer_id=answer_id,
),
params=params
)

def end_poll(self, channel_id: Snowflake, message_id: Snowflake) -> Response[message.Message]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
params=params
)
def end_poll(self, channel_id: Snowflake, message_id: Snowflake) -> Response[message.Message]:
params=params,
)
def end_poll(self, channel_id: Snowflake, message_id: Snowflake) -> Response[message.Message]:

return self.request(
Route(
'POST',
'/channels/{channel_id}/polls/{message_id}/expire',
channel_id=channel_id,
message_id=message_id
DA-344 marked this conversation as resolved.
Show resolved Hide resolved
)
)

async def get_gateway(self, *, encoding: str = 'json', zlib: bool = True) -> str:
try:
Expand Down
3 changes: 3 additions & 0 deletions discord/interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
from .channel import VoiceChannel, StageChannel, TextChannel, ForumChannel, CategoryChannel, DMChannel, GroupChannel
from .threads import Thread
from .app_commands.commands import Command, ContextMenu
from .poll import Poll

InteractionChannel = Union[
VoiceChannel,
Expand Down Expand Up @@ -727,6 +728,7 @@ async def send_message(
suppress_embeds: bool = False,
silent: bool = False,
delete_after: Optional[float] = None,
poll: Poll = MISSING,
) -> None:
"""|coro|

Expand Down Expand Up @@ -807,6 +809,7 @@ async def send_message(
allowed_mentions=allowed_mentions,
flags=flags,
view=view,
poll=poll
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
poll=poll
poll=poll,

)

http = parent._state.http
Expand Down
Loading