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

Make SelectOption.emoji a property #1550

Merged
Merged
Changes from all commits
Commits
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
33 changes: 21 additions & 12 deletions discord/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,6 @@ class SelectOption:
description: Optional[:class:`str`]
An additional description of the option, if any.
Can only be up to 100 characters.
emoji: Optional[Union[:class:`str`, :class:`Emoji`, :class:`PartialEmoji`]]
The emoji of the option, if available.
default: :class:`bool`
Whether this option is selected by default.
"""
Expand All @@ -384,7 +382,7 @@ class SelectOption:
"label",
"value",
"description",
"emoji",
"_emoji",
"default",
)

Expand All @@ -399,22 +397,16 @@ def __init__(
) -> None:
if len(label) > 100:
raise ValueError("label must be 100 characters or fewer")

if value is not MISSING and len(value) > 100:
raise ValueError("value must be 100 characters or fewer")

if description is not None and len(description) > 100:
raise ValueError("description must be 100 characters or fewer")

self.label = label
self.value = label if value is MISSING else value
self.description = description

if emoji is not None:
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.default = default

Expand All @@ -430,6 +422,23 @@ def __str__(self) -> str:
return f"{base}\n{self.description}"
return base

@property
def emoji(self) -> Optional[Union[str, Emoji, PartialEmoji]]:
"""Optional[Union[:class:`str`, :class:`Emoji`, :class:`PartialEmoji`]]: The emoji of the option, if available."""
return self._emoji

@emoji.setter
def emoji(self, value) -> None:
if value is not None:
if isinstance(value, str):
value = PartialEmoji.from_str(value)
elif isinstance(value, _EmojiTag):
value = value._to_partial()
else:
raise TypeError(f"expected emoji to be str, Emoji, or PartialEmoji not {value.__class__}")

self._emoji = value

@classmethod
def from_dict(cls, data: SelectOptionPayload) -> SelectOption:
try:
Expand Down