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: Polls #2408

Merged
merged 133 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from 131 commits
Commits
Show all changes
133 commits
Select commit Hold shift + click to select a range
a1a9b58
poll framework
NeloBlivion Mar 21, 2024
76faff7
rename
NeloBlivion Mar 21, 2024
9eb85bd
pollanswer and pollresults
NeloBlivion Mar 21, 2024
4ba85b0
poll object
NeloBlivion Mar 21, 2024
dea77cc
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 21, 2024
f27e310
adjust pollanswer structure
NeloBlivion Mar 21, 2024
ca3ba13
implement message routes
NeloBlivion Mar 21, 2024
72ddfd2
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 21, 2024
cbdd5e9
implement message routes and adjustments
NeloBlivion Mar 22, 2024
140056e
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 22, 2024
670948a
events
NeloBlivion Mar 22, 2024
9daa951
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 22, 2024
5ca25f2
events
NeloBlivion Mar 22, 2024
2e47ac2
docfix
NeloBlivion Mar 22, 2024
078e4d1
expiry is now a duration
NeloBlivion Mar 22, 2024
3969b6b
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 22, 2024
dbf53d0
import
NeloBlivion Mar 22, 2024
1c947ab
fix poll from_dict
NeloBlivion Mar 22, 2024
a20c2ee
update message.poll
NeloBlivion Mar 22, 2024
3db1e5f
except
NeloBlivion Mar 22, 2024
55d342b
__all__
NeloBlivion Mar 22, 2024
efef2e0
remove slots
NeloBlivion Mar 22, 2024
794dc73
repr
NeloBlivion Mar 22, 2024
9e31916
emoji?
NeloBlivion Mar 22, 2024
658ac8b
support responses
NeloBlivion Mar 22, 2024
11d85e0
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 22, 2024
937351c
Update discord/enums.py
NeloBlivion Mar 22, 2024
37b1aa8
Update discord/raw_models.py
NeloBlivion Mar 22, 2024
fc2c983
docs
NeloBlivion Mar 22, 2024
a3bb72c
minor fixes
NeloBlivion Mar 22, 2024
1ceef29
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 22, 2024
16604fb
force pollmedia instead of string
NeloBlivion Mar 22, 2024
b662fb0
more fixes
NeloBlivion Mar 22, 2024
5e08b12
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 22, 2024
c08bc5f
emoji and message adjustments
NeloBlivion Mar 22, 2024
8ffa304
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 22, 2024
e952a35
duration is in hours, poll recieves expiry instead of duration from api
NeloBlivion Mar 22, 2024
17ba39a
import datetime
NeloBlivion Mar 22, 2024
337ce12
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 22, 2024
3697bed
rework answer.count and start on iterator
NeloBlivion Mar 23, 2024
527d2f0
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 23, 2024
f8cf868
fully implement VoteIterator
NeloBlivion Mar 23, 2024
cc0c607
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 23, 2024
951c763
fix poll results typing
NeloBlivion Mar 23, 2024
2db6d7f
update answer_id
NeloBlivion Mar 24, 2024
12c5e3f
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 24, 2024
ee6a8e4
is not none
NeloBlivion Mar 24, 2024
10c24e1
fix endpoints
NeloBlivion Mar 24, 2024
acf46c4
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 24, 2024
5370378
fix from_dict layout_type
NeloBlivion Mar 24, 2024
9a8ab97
poll.expire
NeloBlivion Mar 24, 2024
31e8c1f
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 24, 2024
5851c4c
add poll to webhook.send
NeloBlivion Mar 24, 2024
cdde7be
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 24, 2024
40ef294
patch again
NeloBlivion Mar 24, 2024
90b68bf
__all__
NeloBlivion Mar 24, 2024
ca8cdf6
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 24, 2024
3b6867a
minor adjustments
NeloBlivion Mar 25, 2024
d156c20
fix partialemoji
NeloBlivion Mar 27, 2024
3cbda57
change event dispatch
NeloBlivion Mar 27, 2024
5efd743
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 27, 2024
de60514
misc adjustments
NeloBlivion Mar 27, 2024
2294675
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 27, 2024
dfe8109
fix pollresults
NeloBlivion Mar 28, 2024
4c2e555
fix answercounts
NeloBlivion Mar 28, 2024
cb24e26
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 28, 2024
a1f8c86
forgot answer id
NeloBlivion Mar 28, 2024
1fdf9fd
change pollanswercount id
NeloBlivion Mar 28, 2024
e00700a
add convenience function total_votes
NeloBlivion Mar 28, 2024
2c2fa11
change count fallback
NeloBlivion Mar 28, 2024
dcc8922
change except
NeloBlivion Apr 11, 2024
b5caad5
store polls in state cache, and update on events
NeloBlivion Apr 11, 2024
ed84c9a
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 11, 2024
a754653
fix slots
NeloBlivion Apr 11, 2024
77ebcf3
attr
NeloBlivion Apr 11, 2024
d1bf4f9
client.polls
NeloBlivion Apr 11, 2024
3b914c4
alter events
NeloBlivion Apr 11, 2024
4fa4df5
oops
NeloBlivion Apr 11, 2024
5c7666f
fix cached votes
NeloBlivion Apr 11, 2024
3f09c4f
friendlier design, use discord defaults
NeloBlivion Apr 11, 2024
dd3368d
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 11, 2024
21e7887
fix question
NeloBlivion Apr 11, 2024
fe43157
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 11, 2024
80c00b6
remove remove_answer
NeloBlivion Apr 11, 2024
80e5b4c
cache polls on raw message edit
NeloBlivion Apr 11, 2024
7d341a4
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 11, 2024
cc5bfef
typing
NeloBlivion Apr 11, 2024
411c363
add client.get_poll
NeloBlivion Apr 12, 2024
49d111e
typing
NeloBlivion Apr 12, 2024
825951a
poll intents baseline
NeloBlivion Apr 13, 2024
591c960
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 13, 2024
bf7f459
real intent values
NeloBlivion Apr 13, 2024
01b5ae4
edit intent docs
NeloBlivion Apr 13, 2024
8653e1f
docs
NeloBlivion Apr 13, 2024
0a8f2ed
send_polls permission
NeloBlivion Apr 17, 2024
f5a4e4d
final touches
NeloBlivion Apr 17, 2024
fe6f7ab
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 17, 2024
22ab213
null results guard
NeloBlivion Apr 17, 2024
fcab456
type
NeloBlivion Apr 17, 2024
15f9b03
QOL events and has_ended
NeloBlivion Apr 17, 2024
42a36fd
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 17, 2024
729299d
nicer expiry
NeloBlivion Apr 17, 2024
34acea3
intent docs
NeloBlivion Apr 17, 2024
34a1ed4
docs updates
NeloBlivion Apr 17, 2024
cdbdbfa
PartialMessage.poll
NeloBlivion Apr 18, 2024
c28d37d
docs
NeloBlivion Apr 18, 2024
8901c81
duplicate total_votes to Poll
NeloBlivion Apr 18, 2024
abcc050
rename users -> voters
NeloBlivion Apr 18, 2024
27368b1
fix voters
NeloBlivion Apr 20, 2024
cd08105
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 20, 2024
51a491b
typehint and changelog
NeloBlivion Apr 20, 2024
61c8424
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 20, 2024
a4bc36c
Apply grammar suggestions
NeloBlivion Apr 23, 2024
4422726
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 23, 2024
22d125c
Merge branch 'master' into polls
NeloBlivion May 20, 2024
40d860c
Merge branch 'master' into polls
NeloBlivion May 26, 2024
c35373f
Apply suggestions from code review
NeloBlivion May 26, 2024
ba39b2f
review updates
NeloBlivion May 26, 2024
5148a46
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 26, 2024
37a4829
remove count deletion on 0 votes
NeloBlivion May 26, 2024
b3856a3
recheck
NeloBlivion May 26, 2024
20653ae
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 26, 2024
842c71a
revert?
NeloBlivion May 26, 2024
6600023
unrevert
NeloBlivion May 26, 2024
74135c0
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 26, 2024
4861d92
expand example
NeloBlivion May 26, 2024
d360aca
Update discord/poll.py
NeloBlivion May 26, 2024
3b99f2b
Update discord/poll.py
NeloBlivion May 26, 2024
ac1d8fc
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 26, 2024
a132e1e
Merge branch 'master' into polls
NeloBlivion Jun 5, 2024
db3c021
Update docs/api/enums.rst
Lulalaby Jun 14, 2024
c0d1379
Merge branch 'master' into polls
Dorukyum Jun 15, 2024
2cdd7e0
state.py aktualisieren
Dorukyum Jun 15, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ These changes are available on the `master` branch, but have not yet been releas
([#2421](https://github.com/Pycord-Development/pycord/pull/2421))
- Added `member` data to the `raw_reaction_remove` event.
([#2412](https://github.com/Pycord-Development/pycord/pull/2412))
- Added `Poll` and all related features.
([#2408](https://github.com/Pycord-Development/pycord/pull/2408))
- Added `stacklevel` param to `utils.warn_deprecated` and `utils.deprecated`.
([#2450](https://github.com/Pycord-Development/pycord/pull/2450))

Expand Down
1 change: 1 addition & 0 deletions discord/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
from .partial_emoji import *
from .permissions import *
from .player import *
from .poll import *
from .raw_models import *
from .reaction import *
from .role import *
Expand Down
16 changes: 16 additions & 0 deletions discord/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
from .guild import Guild
from .member import Member
from .message import Message, MessageReference, PartialMessage
from .poll import Poll
from .state import ConnectionState
from .threads import Thread
from .types.channel import Channel as ChannelPayload
Expand Down Expand Up @@ -1351,6 +1352,7 @@ async def send(
reference: Message | MessageReference | PartialMessage = ...,
mention_author: bool = ...,
view: View = ...,
poll: Poll = ...,
suppress: bool = ...,
silent: bool = ...,
) -> Message: ...
Expand All @@ -1371,6 +1373,7 @@ async def send(
reference: Message | MessageReference | PartialMessage = ...,
mention_author: bool = ...,
view: View = ...,
poll: Poll = ...,
suppress: bool = ...,
silent: bool = ...,
) -> Message: ...
Expand All @@ -1391,6 +1394,7 @@ async def send(
reference: Message | MessageReference | PartialMessage = ...,
mention_author: bool = ...,
view: View = ...,
poll: Poll = ...,
suppress: bool = ...,
silent: bool = ...,
) -> Message: ...
Expand All @@ -1411,6 +1415,7 @@ async def send(
reference: Message | MessageReference | PartialMessage = ...,
mention_author: bool = ...,
view: View = ...,
poll: Poll = ...,
suppress: bool = ...,
silent: bool = ...,
) -> Message: ...
Expand All @@ -1432,6 +1437,7 @@ async def send(
reference=None,
mention_author=None,
view=None,
poll=None,
suppress=None,
silent=None,
):
Expand Down Expand Up @@ -1515,6 +1521,10 @@ async def send(
Whether to suppress push and desktop notifications for the message.

.. versionadded:: 2.4
poll: :class:`Poll`
The poll to send.

.. versionadded:: 2.6

Returns
-------
Expand Down Expand Up @@ -1594,6 +1604,9 @@ async def send(
else:
components = None

if poll:
poll = poll.to_dict()

if file is not None and files is not None:
raise InvalidArgument("cannot pass both file and files parameter to send()")

Expand All @@ -1616,6 +1629,7 @@ async def send(
stickers=stickers,
components=components,
flags=flags,
poll=poll,
)
finally:
file.close()
Expand Down Expand Up @@ -1643,6 +1657,7 @@ async def send(
stickers=stickers,
components=components,
flags=flags,
poll=poll,
)
finally:
for f in files:
Expand All @@ -1661,6 +1676,7 @@ async def send(
stickers=stickers,
components=components,
flags=flags,
poll=poll,
)

ret = state.create_message(channel=channel, data=data)
Expand Down
24 changes: 24 additions & 0 deletions discord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
from .channel import DMChannel
from .member import Member
from .message import Message
from .poll import Poll
from .voice_client import VoiceProtocol

__all__ = ("Client",)
Expand Down Expand Up @@ -338,6 +339,14 @@ def stickers(self) -> list[GuildSticker]:
"""
return self._connection.stickers

@property
def polls(self) -> list[Poll]:
"""The polls that the connected client has.

.. versionadded:: 2.6
"""
return self._connection.polls

@property
def cached_messages(self) -> Sequence[Message]:
"""Read-only list of messages the connected client has cached.
Expand Down Expand Up @@ -1010,6 +1019,21 @@ def get_sticker(self, id: int, /) -> GuildSticker | None:
"""
return self._connection.get_sticker(id)

def get_poll(self, id: int, /) -> Poll | None:
"""Returns a poll attached to the given message ID.

Parameters
----------
id: :class:`int`
The message ID of the poll to search for.

Returns
-------
Optional[:class:`.Poll`]
The poll or ``None`` if not found.
"""
return self._connection.get_poll(id)

def get_all_channels(self) -> Generator[GuildChannel, None, None]:
"""A generator that retrieves every :class:`.abc.GuildChannel` the client can 'access'.

Expand Down
6 changes: 6 additions & 0 deletions discord/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,12 @@ class EntitlementOwnerType(Enum):
user = 2


class PollLayoutType(Enum):
NeloBlivion marked this conversation as resolved.
Show resolved Hide resolved
"""The poll's layout type."""

default = 1


T = TypeVar("T")


Expand Down
68 changes: 68 additions & 0 deletions discord/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,8 @@ def messages(self):
- :class:`Message`
- :attr:`Client.cached_messages`
- :meth:`Client.get_message`
- :attr:`Client.polls`
- :meth:`Client.get_poll`

Note that due to an implicit relationship this also corresponds to the following events:

Expand Down Expand Up @@ -917,6 +919,8 @@ def guild_messages(self):
- :class:`Message`
- :attr:`Client.cached_messages` (only for guilds)
- :meth:`Client.get_message` (only for guilds)
- :attr:`Client.polls` (only for guilds)
- :meth:`Client.get_poll` (only for guilds)

Note that due to an implicit relationship this also corresponds to the following events:

Expand All @@ -931,6 +935,7 @@ def guild_messages(self):
- :attr:`Message.embeds`
- :attr:`Message.attachments`
- :attr:`Message.components`
- :attr:`Message.poll`

For more information go to the :ref:`message content intent documentation <need_message_content_intent>`.
"""
Expand All @@ -955,6 +960,8 @@ def dm_messages(self):
- :class:`Message`
- :attr:`Client.cached_messages` (only for DMs)
- :meth:`Client.get_message` (only for DMs)
- :attr:`Client.polls` (only for DMs)
- :meth:`Client.get_poll` (only for DMs)

Note that due to an implicit relationship this also corresponds to the following events:

Expand Down Expand Up @@ -1079,6 +1086,7 @@ def message_content(self):
- :attr:`Message.embeds`
- :attr:`Message.attachments`
- :attr:`Message.components`
- :attr:`Message.poll`

These attributes will still be available for messages received from interactions,
the bot's own messages, messages the bot was mentioned in, and DMs.
Expand Down Expand Up @@ -1137,6 +1145,66 @@ def auto_moderation_execution(self):
"""
return 1 << 21

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

See also :attr:`dm_polls` for DMs or :attr:`polls` for both.

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)

This also corresponds to the following attributes and classes in terms of cache:

- :attr:`PollAnswer.count` (only for guild polls)
- :attr:`PollResults.answer_counts` (only for guild polls)
"""
return 1 << 24

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

See also :attr:`guild_polls` for guilds or :attr:`polls` for both.

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)

This also corresponds to the following attributes and classes in terms of cache:

- :attr:`PollAnswer.count` (only for DM polls)
- :attr:`PollResults.answer_counts` (only for DM polls)
"""
return 1 << 25

@alias_flag_value
def polls(self):
""":class:`bool`: Whether poll-related events in guilds and direct messages 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)

This also corresponds to the following attributes and classes in terms of cache:

- :attr:`PollAnswer.count` (both guild and DM polls)
- :attr:`PollResults.answer_counts` (both guild and DM polls)
"""
return (1 << 24) | (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 @@ -71,6 +71,7 @@
message,
monetization,
onboarding,
poll,
role,
scheduled_events,
sticker,
Expand Down Expand Up @@ -471,6 +472,7 @@ def send_message(
stickers: list[sticker.StickerItem] | None = None,
components: list[components.Component] | None = None,
flags: int | None = None,
poll: poll.Poll | None = None,
) -> Response[message.Message]:
r = Route("POST", "/channels/{channel_id}/messages", channel_id=channel_id)
payload = {}
Expand Down Expand Up @@ -508,6 +510,9 @@ def send_message(
if flags:
payload["flags"] = flags

if poll:
payload["poll"] = poll

return self.request(r, json=payload)

def send_typing(self, channel_id: Snowflake) -> Response[None]:
Expand All @@ -531,6 +536,7 @@ def send_multipart_helper(
stickers: list[sticker.StickerItem] | None = None,
components: list[components.Component] | None = None,
flags: int | None = None,
poll: poll.Poll | None = None,
) -> Response[message.Message]:
form = []

Expand All @@ -555,6 +561,8 @@ def send_multipart_helper(
payload["sticker_ids"] = stickers
if flags:
payload["flags"] = flags
if poll:
payload["poll"] = poll

attachments = []
form.append({"name": "payload_json"})
Expand Down Expand Up @@ -594,6 +602,7 @@ def send_files(
stickers: list[sticker.StickerItem] | None = None,
components: list[components.Component] | None = None,
flags: int | None = None,
poll: poll.Poll | None = None,
) -> Response[message.Message]:
r = Route("POST", "/channels/{channel_id}/messages", channel_id=channel_id)
return self.send_multipart_helper(
Expand All @@ -610,6 +619,7 @@ def send_files(
stickers=stickers,
components=components,
flags=flags,
poll=poll,
)

def edit_multipart_helper(
Expand Down Expand Up @@ -3003,6 +3013,43 @@ def edit_onboarding(
reason=reason,
)

# Polls

def expire_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,
)
)

def get_answer_voters(
self,
channel_id: Snowflake,
message_id: Snowflake,
answer_id: int,
limit: int,
after: Snowflake | None = None,
) -> Response[list[user.User]]:
r = Route(
"GET",
"/channels/{channel_id}/polls/{message_id}/answers/{answer_id}",
channel_id=channel_id,
message_id=message_id,
answer_id=answer_id,
)

params: dict[str, Any] = {
"limit": limit,
}
if after:
params["after"] = after
return self.request(r, params=params)

# Misc

def application_info(self) -> Response[appinfo.AppInfo]:
Expand Down
Loading
Loading