Skip to content

Commit

Permalink
Add reaction type to raw events and users iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
Puncher1 committed May 18, 2024
1 parent b5ada0a commit f77ba71
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 4 deletions.
5 changes: 5 additions & 0 deletions discord/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,11 @@ class InviteType(Enum):
friend = 2


class ReactionType(Enum):
normal = 0
burst = 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
5 changes: 5 additions & 0 deletions discord/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,7 @@ def get_reaction_users(
emoji: str,
limit: int,
after: Optional[Snowflake] = None,
type: Optional[message.ReactionType] = None,
) -> Response[List[user.User]]:
r = Route(
'GET',
Expand All @@ -955,6 +956,10 @@ def get_reaction_users(
}
if after:
params['after'] = after

if type is not None:
params['type'] = type

return self.request(r, params=params)

def clear_reactions(self, channel_id: Snowflake, message_id: Snowflake) -> Response[None]:
Expand Down
8 changes: 7 additions & 1 deletion discord/raw_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import datetime
from typing import TYPE_CHECKING, Literal, Optional, Set, List, Tuple, Union

from .enums import ChannelType, try_enum
from .enums import ChannelType, try_enum, ReactionType
from .utils import _get_as_snowflake
from .app_commands import AppCommandPermissions
from .colour import Colour
Expand Down Expand Up @@ -221,6 +221,10 @@ class RawReactionActionEvent(_RawReprMixin):
and if ``event_type`` is ``REACTION_ADD``.
.. versionadded:: 2.0
type: :class:`ReactionType`
The type of the reaction.
.. versionadded:: 2.4
"""

__slots__ = (
Expand All @@ -234,6 +238,7 @@ class RawReactionActionEvent(_RawReprMixin):
'message_author_id',
'burst',
'burst_colours',
'type',
)

def __init__(self, data: ReactionActionEvent, emoji: PartialEmoji, event_type: ReactionActionType) -> None:
Expand All @@ -246,6 +251,7 @@ def __init__(self, data: ReactionActionEvent, emoji: PartialEmoji, event_type: R
self.message_author_id: Optional[int] = _get_as_snowflake(data, 'message_author_id')
self.burst: bool = data.get('burst', False)
self.burst_colours: List[Colour] = [Colour.from_str(c) for c in data.get('burst_colours', [])]
self.type: ReactionType = try_enum(ReactionType, data['type'])

try:
self.guild_id: Optional[int] = int(data['guild_id'])
Expand Down
17 changes: 15 additions & 2 deletions discord/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

from .user import User
from .object import Object
from .enums import ReactionType

# fmt: off
__all__ = (
Expand Down Expand Up @@ -185,7 +186,7 @@ async def clear(self) -> None:
await self.message.clear_reaction(self.emoji)

async def users(
self, *, limit: Optional[int] = None, after: Optional[Snowflake] = None
self, *, limit: Optional[int] = None, after: Optional[Snowflake] = None, type: Optional[ReactionType] = None
) -> AsyncIterator[Union[Member, User]]:
"""Returns an :term:`asynchronous iterator` representing the users that have reacted to the message.
Expand Down Expand Up @@ -220,6 +221,11 @@ async def users(
reacted to the message.
after: Optional[:class:`abc.Snowflake`]
For pagination, reactions are sorted by member.
type: Optional[:class:`ReactionType`]
The type of reaction to return users from.
If not provided, Discord only returns users of reactions with type ``normal``.
.. versionadded:: 2.4
Raises
--------
Expand Down Expand Up @@ -251,7 +257,14 @@ async def users(
state = message._state
after_id = after.id if after else None

data = await state.http.get_reaction_users(message.channel.id, message.id, emoji, retrieve, after=after_id)
data = await state.http.get_reaction_users(
message.channel.id,
message.id,
emoji,
retrieve,
after=after_id,
type=type.value if type is not None else None,
)

if data:
limit -= len(data)
Expand Down
4 changes: 3 additions & 1 deletion discord/types/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from .emoji import Emoji, PartialEmoji
from .member import MemberWithUser
from .snowflake import Snowflake
from .message import Message
from .message import Message, ReactionType
from .sticker import GuildSticker
from .appinfo import GatewayAppInfo, PartialAppInfo
from .guild import Guild, UnavailableGuild
Expand Down Expand Up @@ -104,6 +104,7 @@ class MessageReactionAddEvent(TypedDict):
message_author_id: NotRequired[Snowflake]
burst: bool
burst_colors: NotRequired[List[str]]
type: ReactionType


class MessageReactionRemoveEvent(TypedDict):
Expand All @@ -113,6 +114,7 @@ class MessageReactionRemoveEvent(TypedDict):
emoji: PartialEmoji
guild_id: NotRequired[Snowflake]
burst: bool
type: ReactionType


class MessageReactionRemoveAllEvent(TypedDict):
Expand Down
3 changes: 3 additions & 0 deletions discord/types/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class ReactionCountDetails(TypedDict):
normal: int


ReactionType = Literal[0, 1]


class Reaction(TypedDict):
count: int
me: bool
Expand Down
15 changes: 15 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3643,6 +3643,21 @@ of :class:`enum.Enum`.
The invite is a friend invite.


.. class:: ReactionType

Represents the type of a reaction.

.. versionadded:: 2.4

.. attribute:: normal

A normal reaction.

.. attribute:: burst

A burst reaction, also known as a "super reaction".


.. _discord-api-audit-logs:

Audit Log Data
Expand Down

0 comments on commit f77ba71

Please sign in to comment.