mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-07-13 21:35:30 +00:00
Allow passing Emoji in components
This commit is contained in:
parent
be5f4ae4ab
commit
47f2d04940
@ -37,6 +37,7 @@ if TYPE_CHECKING:
|
|||||||
SelectOption as SelectOptionPayload,
|
SelectOption as SelectOptionPayload,
|
||||||
ActionRow as ActionRowPayload,
|
ActionRow as ActionRowPayload,
|
||||||
)
|
)
|
||||||
|
from .emoji import Emoji
|
||||||
|
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
@ -269,7 +270,7 @@ class SelectOption:
|
|||||||
description: Optional[:class:`str`]
|
description: Optional[:class:`str`]
|
||||||
An additional description of the option, if any.
|
An additional description of the option, if any.
|
||||||
Can only be up to 50 characters.
|
Can only be up to 50 characters.
|
||||||
emoji: Optional[:class:`PartialEmoji`]
|
emoji: Optional[Union[:class:`str`, :class:`Emoji`, :class:`PartialEmoji`]]
|
||||||
The emoji of the option, if available.
|
The emoji of the option, if available.
|
||||||
default: :class:`bool`
|
default: :class:`bool`
|
||||||
Whether this option is selected by default.
|
Whether this option is selected by default.
|
||||||
@ -289,15 +290,20 @@ class SelectOption:
|
|||||||
label: str,
|
label: str,
|
||||||
value: str = MISSING,
|
value: str = MISSING,
|
||||||
description: Optional[str] = None,
|
description: Optional[str] = None,
|
||||||
emoji: Optional[Union[str, PartialEmoji]] = None,
|
emoji: Optional[Union[str, Emoji, PartialEmoji]] = None,
|
||||||
default: bool = False,
|
default: bool = False,
|
||||||
) -> None:
|
) -> None:
|
||||||
self.label = label
|
self.label = label
|
||||||
self.value = label if value is MISSING else value
|
self.value = label if value is MISSING else value
|
||||||
self.description = description
|
self.description = description
|
||||||
|
|
||||||
if isinstance(emoji, str):
|
if emoji is not None:
|
||||||
emoji = PartialEmoji.from_str(emoji)
|
if isinstance(emoji, str):
|
||||||
|
emoji = PartialEmoji.from_str(emoji)
|
||||||
|
elif isinstance(emoji, _EmojiTag):
|
||||||
|
emoji = emoji._to_partial()
|
||||||
|
else:
|
||||||
|
raise TypeError(f'expected emoji to be str, Emoji, or PartialEmoji not {emoji.__class__}')
|
||||||
|
|
||||||
self.emoji = emoji
|
self.emoji = emoji
|
||||||
self.default = default
|
self.default = default
|
||||||
|
@ -27,7 +27,7 @@ from typing import Any, Iterator, List, Optional, TYPE_CHECKING, Tuple
|
|||||||
|
|
||||||
from .asset import Asset, AssetMixin
|
from .asset import Asset, AssetMixin
|
||||||
from .utils import SnowflakeList, snowflake_time, MISSING
|
from .utils import SnowflakeList, snowflake_time, MISSING
|
||||||
from .partial_emoji import _EmojiTag
|
from .partial_emoji import _EmojiTag, PartialEmoji
|
||||||
from .user import User
|
from .user import User
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
@ -122,6 +122,9 @@ class Emoji(_EmojiTag, AssetMixin):
|
|||||||
user = emoji.get('user')
|
user = emoji.get('user')
|
||||||
self.user: Optional[User] = User(state=self._state, data=user) if user else None
|
self.user: Optional[User] = User(state=self._state, data=user) if user else None
|
||||||
|
|
||||||
|
def _to_partial(self) -> PartialEmoji:
|
||||||
|
return PartialEmoji(name=self.name, animated=self.animated, id=self.id)
|
||||||
|
|
||||||
def __iter__(self) -> Iterator[Tuple[str, Any]]:
|
def __iter__(self) -> Iterator[Tuple[str, Any]]:
|
||||||
for attr in self.__slots__:
|
for attr in self.__slots__:
|
||||||
if attr[0] != '_':
|
if attr[0] != '_':
|
||||||
|
@ -45,6 +45,9 @@ class _EmojiTag:
|
|||||||
|
|
||||||
id: int
|
id: int
|
||||||
|
|
||||||
|
def _to_partial(self) -> PartialEmoji:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
PE = TypeVar('PE', bound='PartialEmoji')
|
PE = TypeVar('PE', bound='PartialEmoji')
|
||||||
|
|
||||||
@ -151,6 +154,9 @@ class PartialEmoji(_EmojiTag, AssetMixin):
|
|||||||
o['animated'] = self.animated
|
o['animated'] = self.animated
|
||||||
return o
|
return o
|
||||||
|
|
||||||
|
def _to_partial(self) -> PartialEmoji:
|
||||||
|
return self
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def with_state(
|
def with_state(
|
||||||
cls: Type[PE], state: ConnectionState, *, name: str, animated: bool = False, id: Optional[int] = None
|
cls: Type[PE], state: ConnectionState, *, name: str, animated: bool = False, id: Optional[int] = None
|
||||||
|
@ -31,7 +31,7 @@ import os
|
|||||||
|
|
||||||
from .item import Item, ItemCallbackType
|
from .item import Item, ItemCallbackType
|
||||||
from ..enums import ButtonStyle, ComponentType
|
from ..enums import ButtonStyle, ComponentType
|
||||||
from ..partial_emoji import PartialEmoji
|
from ..partial_emoji import PartialEmoji, _EmojiTag
|
||||||
from ..components import Button as ButtonComponent
|
from ..components import Button as ButtonComponent
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
@ -41,6 +41,7 @@ __all__ = (
|
|||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .view import View
|
from .view import View
|
||||||
|
from ..emoji import Emoji
|
||||||
|
|
||||||
B = TypeVar('B', bound='Button')
|
B = TypeVar('B', bound='Button')
|
||||||
V = TypeVar('V', bound='View', covariant=True)
|
V = TypeVar('V', bound='View', covariant=True)
|
||||||
@ -64,7 +65,7 @@ class Button(Item[V]):
|
|||||||
Whether the button is disabled or not.
|
Whether the button is disabled or not.
|
||||||
label: Optional[:class:`str`]
|
label: Optional[:class:`str`]
|
||||||
The label of the button, if any.
|
The label of the button, if any.
|
||||||
emoji: Optional[:class:`PartialEmoji`]
|
emoji: Optional[Union[:class:`PartialEmoji`, :class:`Emoji`, :class:`str`]]
|
||||||
The emoji of the button, if available.
|
The emoji of the button, if available.
|
||||||
row: Optional[:class:`int`]
|
row: Optional[:class:`int`]
|
||||||
The relative row this button belongs to. A Discord component can only have 5
|
The relative row this button belongs to. A Discord component can only have 5
|
||||||
@ -91,7 +92,7 @@ class Button(Item[V]):
|
|||||||
disabled: bool = False,
|
disabled: bool = False,
|
||||||
custom_id: Optional[str] = None,
|
custom_id: Optional[str] = None,
|
||||||
url: Optional[str] = None,
|
url: Optional[str] = None,
|
||||||
emoji: Optional[Union[str, PartialEmoji]] = None,
|
emoji: Optional[Union[str, Emoji, PartialEmoji]] = None,
|
||||||
row: Optional[int] = None,
|
row: Optional[int] = None,
|
||||||
):
|
):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
@ -105,8 +106,13 @@ class Button(Item[V]):
|
|||||||
if url is not None:
|
if url is not None:
|
||||||
style = ButtonStyle.link
|
style = ButtonStyle.link
|
||||||
|
|
||||||
if isinstance(emoji, str):
|
if emoji is not None:
|
||||||
emoji = PartialEmoji.from_str(emoji)
|
if isinstance(emoji, str):
|
||||||
|
emoji = PartialEmoji.from_str(emoji)
|
||||||
|
elif isinstance(emoji, _EmojiTag):
|
||||||
|
emoji = emoji._to_partial()
|
||||||
|
else:
|
||||||
|
raise TypeError(f'expected emoji to be str, Emoji, or PartialEmoji not {emoji.__class__}')
|
||||||
|
|
||||||
self._underlying = ButtonComponent._raw_construct(
|
self._underlying = ButtonComponent._raw_construct(
|
||||||
type=ComponentType.button,
|
type=ComponentType.button,
|
||||||
@ -178,12 +184,14 @@ class Button(Item[V]):
|
|||||||
return self._underlying.emoji
|
return self._underlying.emoji
|
||||||
|
|
||||||
@emoji.setter
|
@emoji.setter
|
||||||
def emoji(self, value: Optional[Union[str, PartialEmoji]]): # type: ignore
|
def emoji(self, value: Optional[Union[str, Emoji, PartialEmoji]]): # type: ignore
|
||||||
if value is not None:
|
if value is not None:
|
||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
self._underlying.emoji = PartialEmoji.from_str(value)
|
self._underlying.emoji = PartialEmoji.from_str(value)
|
||||||
|
elif isinstance(value, _EmojiTag):
|
||||||
|
self._underlying.emoji = value._to_partial()
|
||||||
else:
|
else:
|
||||||
self._underlying.emoji = value
|
raise TypeError(f'expected str, Emoji, or PartialEmoji, received {value.__class__} instead')
|
||||||
else:
|
else:
|
||||||
self._underlying.emoji = None
|
self._underlying.emoji = None
|
||||||
|
|
||||||
@ -219,7 +227,7 @@ def button(
|
|||||||
custom_id: Optional[str] = None,
|
custom_id: Optional[str] = None,
|
||||||
disabled: bool = False,
|
disabled: bool = False,
|
||||||
style: ButtonStyle = ButtonStyle.secondary,
|
style: ButtonStyle = ButtonStyle.secondary,
|
||||||
emoji: Optional[Union[str, PartialEmoji]] = None,
|
emoji: Optional[Union[str, Emoji, PartialEmoji]] = None,
|
||||||
row: Optional[int] = None,
|
row: Optional[int] = None,
|
||||||
) -> Callable[[ItemCallbackType], ItemCallbackType]:
|
) -> Callable[[ItemCallbackType], ItemCallbackType]:
|
||||||
"""A decorator that attaches a button to a component.
|
"""A decorator that attaches a button to a component.
|
||||||
@ -247,8 +255,9 @@ def button(
|
|||||||
The style of the button. Defaults to :attr:`ButtonStyle.grey`.
|
The style of the button. Defaults to :attr:`ButtonStyle.grey`.
|
||||||
disabled: :class:`bool`
|
disabled: :class:`bool`
|
||||||
Whether the button is disabled or not. Defaults to ``False``.
|
Whether the button is disabled or not. Defaults to ``False``.
|
||||||
emoji: Optional[Union[:class:`str`, :class:`PartialEmoji`]]
|
emoji: Optional[Union[:class:`str`, :class:`Emoji`, :class:`PartialEmoji`]]
|
||||||
The emoji of the button. This can be in string form or a :class:`PartialEmoji`.
|
The emoji of the button. This can be in string form or a :class:`PartialEmoji`
|
||||||
|
or a full :class:`Emoji`.
|
||||||
row: Optional[:class:`int`]
|
row: Optional[:class:`int`]
|
||||||
The relative row this button belongs to. A Discord component can only have 5
|
The relative row this button belongs to. A Discord component can only have 5
|
||||||
rows. By default, items are arranged automatically into those 5 rows. If you'd
|
rows. By default, items are arranged automatically into those 5 rows. If you'd
|
||||||
|
@ -30,6 +30,7 @@ import os
|
|||||||
from .item import Item, ItemCallbackType
|
from .item import Item, ItemCallbackType
|
||||||
from ..enums import ComponentType
|
from ..enums import ComponentType
|
||||||
from ..partial_emoji import PartialEmoji
|
from ..partial_emoji import PartialEmoji
|
||||||
|
from ..emoji import Emoji
|
||||||
from ..interactions import Interaction
|
from ..interactions import Interaction
|
||||||
from ..utils import MISSING
|
from ..utils import MISSING
|
||||||
from ..components import (
|
from ..components import (
|
||||||
@ -176,7 +177,7 @@ class Select(Item[V]):
|
|||||||
label: str,
|
label: str,
|
||||||
value: str = MISSING,
|
value: str = MISSING,
|
||||||
description: Optional[str] = None,
|
description: Optional[str] = None,
|
||||||
emoji: Optional[Union[str, PartialEmoji]] = None,
|
emoji: Optional[Union[str, Emoji, PartialEmoji]] = None,
|
||||||
default: bool = False,
|
default: bool = False,
|
||||||
):
|
):
|
||||||
"""Adds an option to the select menu.
|
"""Adds an option to the select menu.
|
||||||
@ -195,9 +196,9 @@ class Select(Item[V]):
|
|||||||
description: Optional[:class:`str`]
|
description: Optional[:class:`str`]
|
||||||
An additional description of the option, if any.
|
An additional description of the option, if any.
|
||||||
Can only be up to 50 characters.
|
Can only be up to 50 characters.
|
||||||
emoji: Optional[Union[:class:`str`, :class:`PartialEmoji`]]
|
emoji: Optional[Union[:class:`str`, :class:`Emoji`, :class:`PartialEmoji`]]
|
||||||
The emoji of the option, if available. This can either be a string representing
|
The emoji of the option, if available. This can either be a string representing
|
||||||
the custom or unicode emoji or an instance of :class:`PartialEmoji`.
|
the custom or unicode emoji or an instance of :class:`PartialEmoji` or :class:`Emoji`.
|
||||||
default: :class:`bool`
|
default: :class:`bool`
|
||||||
Whether this option is selected by default.
|
Whether this option is selected by default.
|
||||||
|
|
||||||
@ -207,9 +208,6 @@ class Select(Item[V]):
|
|||||||
The number of options exceeds 25.
|
The number of options exceeds 25.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if isinstance(emoji, str):
|
|
||||||
emoji = PartialEmoji.from_str(emoji)
|
|
||||||
|
|
||||||
option = SelectOption(
|
option = SelectOption(
|
||||||
label=label,
|
label=label,
|
||||||
value=value,
|
value=value,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user