Skip to content

Reaction

class ReactionUsers (AsyncIterator)

An async iterator for searching through a channel's history.

Attributes:

Name Type Description
reaction 'Reaction'

The reaction to search through

limit

The maximum number of users to return (set to 0 for no limit)

after 'Snowflake_Type'

get users after this message ID

Source code in naff/models/discord/reaction.py
class ReactionUsers(AsyncIterator):
    """
    An async iterator for searching through a channel's history.

    Attributes:
        reaction: The reaction to search through
        limit: The maximum number of users to return (set to 0 for no limit)
        after: get users after this message ID

    """

    def __init__(self, reaction: "Reaction", limit: int = 50, after: Optional["Snowflake_Type"] = None) -> None:
        self.reaction: "Reaction" = reaction
        self.after: "Snowflake_Type" = after
        self._more = True
        super().__init__(limit)

    async def fetch(self) -> List["User"]:
        """
        Gets all the users who reacted to the message. Requests user data from discord API if not cached.

        Returns:
            A list of users who reacted to the message.

        """
        if self._more:
            expected = self.get_limit

            if self.after and not self.last:
                self.last = namedtuple("temp", "id")
                self.last.id = self.after

            users = await self.reaction._client.http.get_reactions(
                self.reaction._channel_id,
                self.reaction._message_id,
                self.reaction.emoji.req_format,
                limit=expected,
                after=self.last.id or MISSING,
            )
            if not users:
                raise QueueEmpty
            self._more = len(users) == expected
            return [self.reaction._client.cache.place_user_data(u) for u in users]
        else:
            raise QueueEmpty

inherited property readonly get_limit: int

Get how the maximum number of items that should be retrieved.

async method fetch(self)

Gets all the users who reacted to the message. Requests user data from discord API if not cached.

Returns:

Type Description
List[User]

A list of users who reacted to the message.

Source code in naff/models/discord/reaction.py
async def fetch(self) -> List["User"]:
    """
    Gets all the users who reacted to the message. Requests user data from discord API if not cached.

    Returns:
        A list of users who reacted to the message.

    """
    if self._more:
        expected = self.get_limit

        if self.after and not self.last:
            self.last = namedtuple("temp", "id")
            self.last.id = self.after

        users = await self.reaction._client.http.get_reactions(
            self.reaction._channel_id,
            self.reaction._message_id,
            self.reaction.emoji.req_format,
            limit=expected,
            after=self.last.id or MISSING,
        )
        if not users:
            raise QueueEmpty
        self._more = len(users) == expected
        return [self.reaction._client.cache.place_user_data(u) for u in users]
    else:
        raise QueueEmpty

inherited property readonly total_retrieved: int

Get the total number of objects this iterator has retrieved.

async inherited method add_object(self, obj)

Add an object to iterator's queue.

Source code in naff/models/discord/reaction.py
async def add_object(self, obj) -> None:
    """Add an object to iterator's queue."""
    return await self._queue.put(obj)

async inherited method flatten(self)

Flatten this iterator into a list of objects.

Source code in naff/models/discord/reaction.py
async def flatten(self) -> List:
    """Flatten this iterator into a list of objects."""
    return [elem async for elem in self]

async inherited method search(self, target_id)

Search the iterator for an object with the given ID.

Source code in naff/models/discord/reaction.py
async def search(self, target_id: "snowflake.Snowflake_Type") -> bool:
    """Search the iterator for an object with the given ID."""
    target_id = snowflake.to_snowflake(target_id)

    if target_id in [o.id for o in self._retrieved_objects]:
        return True

    async for o in self:
        if o.id == target_id:
            return True
    return False

attrs class Reaction (ClientObject)

Attr attributes:

Name Type Description
count int

times this emoji has been used to react

me bool

whether the current user reacted using this emoji

emoji PartialEmoji

emoji information

Source code in naff/models/discord/reaction.py
@define()
class Reaction(ClientObject):
    count: int = field()
    """times this emoji has been used to react"""
    me: bool = field(default=False)
    """whether the current user reacted using this emoji"""
    emoji: "PartialEmoji" = field(converter=PartialEmoji.from_dict)
    """emoji information"""

    _channel_id: "Snowflake_Type" = field(converter=to_snowflake)
    _message_id: "Snowflake_Type" = field(converter=to_snowflake)

    def users(self, limit: int = 0, after: "Snowflake_Type" = None) -> ReactionUsers:
        """Users who reacted using this emoji."""
        return ReactionUsers(self, limit, after)

    @property
    def message(self) -> "Message":
        """The message this reaction is on."""
        return self._client.cache.get_message(self._channel_id, self._message_id)

    @property
    def channel(self) -> "TYPE_ALL_CHANNEL":
        """The channel this reaction is on."""
        return self._client.cache.get_channel(self._channel_id)

    async def remove(self) -> None:
        """Remove all this emoji's reactions from the message."""
        await self._client.http.clear_reaction(self._channel_id, self._message_id, self.emoji.req_format)

inherited method update_from_dict(self, data)

Updates object attribute(s) with new json data received from discord api.

Parameters:

Name Type Description Default
data

The json data received from discord api.

required

Returns:

Type Description
~T

The updated object class instance.

Source code in naff/models/discord/reaction.py
def update_from_dict(self, data) -> T:
    data = self._process_dict(data, self._client)
    for key, value in self._filter_kwargs(data, self._get_keys()).items():
        # todo improve
        setattr(self, key, value)

    return self

method users(self, limit, after)

Users who reacted using this emoji.

Source code in naff/models/discord/reaction.py
def users(self, limit: int = 0, after: "Snowflake_Type" = None) -> ReactionUsers:
    """Users who reacted using this emoji."""
    return ReactionUsers(self, limit, after)

property readonly message: Message

The message this reaction is on.

property readonly channel: TYPE_ALL_CHANNEL

The channel this reaction is on.

async method remove(self)

Remove all this emoji's reactions from the message.

Source code in naff/models/discord/reaction.py
async def remove(self) -> None:
    """Remove all this emoji's reactions from the message."""
    await self._client.http.clear_reaction(self._channel_id, self._message_id, self.emoji.req_format)

inherited method to_dict(self)

Exports object into dictionary representation, ready to be sent to discord api.

Returns:

Type Description
Dict[str, Any]

The exported dictionary.

Source code in naff/models/discord/reaction.py
def to_dict(self) -> Dict[str, Any]:
    """
    Exports object into dictionary representation, ready to be sent to discord api.

    Returns:
        The exported dictionary.

    """
    self._check_object()
    return serializer.to_dict(self)