-
-
Notifications
You must be signed in to change notification settings - Fork 482
feat: Support default_values for Select of type ComponentType.mentionable_select and ComponentType.channel_select #2793
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
Closed
Closed
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7d5f073
Add support for default values in non string selects
doluk b7084d1
fix imports
doluk a34f59c
Add support for default values in non-string select menus
doluk febb64f
Enhance SelectDefaultValue handling and serialization.
doluk edd2e70
Update CHANGELOG.md
doluk 3554673
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2a2818f
Fix Imports type checking
doluk ca4da60
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 33cc690
Refactor if checks for default values
doluk 8524967
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5894eca
Merge branch 'master' into role_select_default
doluk aa7a309
Add an example usage of default values to the examples
doluk 2317367
Update the docs and CHANGELOG.md
doluk e7b8707
Minor reformating and code cleanup
doluk cd3b261
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4afc071
Merge branch 'master' into role_select_default
Paillat-dev 81e54b5
Merge branch 'master' into role_select_default
plun1331 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -27,17 +27,24 @@ | |||||||||
|
||||||||||
from typing import TYPE_CHECKING, Any, ClassVar, TypeVar | ||||||||||
|
||||||||||
from .abc import GuildChannel | ||||||||||
from .emoji import AppEmoji, GuildEmoji | ||||||||||
from .enums import ButtonStyle, ChannelType, ComponentType, InputTextStyle, try_enum | ||||||||||
from .member import Member | ||||||||||
from .partial_emoji import PartialEmoji, _EmojiTag | ||||||||||
from .role import Role | ||||||||||
from .threads import Thread | ||||||||||
from .user import User | ||||||||||
from .utils import MISSING, get_slots | ||||||||||
|
||||||||||
if TYPE_CHECKING: | ||||||||||
from .emoji import AppEmoji, GuildEmoji | ||||||||||
from .types.components import ActionRow as ActionRowPayload | ||||||||||
from .types.components import ButtonComponent as ButtonComponentPayload | ||||||||||
from .types.components import Component as ComponentPayload | ||||||||||
from .types.components import InputText as InputTextComponentPayload | ||||||||||
from .types.components import SelectDefaultValue as SelectDefaultValuePayload | ||||||||||
from .types.components import SelectMenu as SelectMenuPayload | ||||||||||
from .types.components import SelectMenuDefaultValueType | ||||||||||
from .types.components import SelectOption as SelectOptionPayload | ||||||||||
|
||||||||||
__all__ = ( | ||||||||||
|
@@ -46,6 +53,7 @@ | |||||||||
"Button", | ||||||||||
"SelectMenu", | ||||||||||
"SelectOption", | ||||||||||
"SelectDefaultValue", | ||||||||||
"InputText", | ||||||||||
) | ||||||||||
|
||||||||||
|
@@ -328,6 +336,9 @@ class SelectMenu(Component): | |||||||||
except for :attr:`ComponentType.channel_select`. | ||||||||||
disabled: :class:`bool` | ||||||||||
Whether the select is disabled or not. | ||||||||||
default_values: List[:class:`SelectDefaultValue`] | ||||||||||
A list of options that are selected by default for select menus of type user, role, channel and mentionable. | ||||||||||
doluk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
Will be an empty list for component type :attr:`ComponentType.string_select`. | ||||||||||
doluk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
""" | ||||||||||
|
||||||||||
__slots__: tuple[str, ...] = ( | ||||||||||
|
@@ -338,6 +349,7 @@ class SelectMenu(Component): | |||||||||
"options", | ||||||||||
"channel_types", | ||||||||||
"disabled", | ||||||||||
"default_values", | ||||||||||
) | ||||||||||
|
||||||||||
__repr_info__: ClassVar[tuple[str, ...]] = __slots__ | ||||||||||
|
@@ -355,6 +367,24 @@ def __init__(self, data: SelectMenuPayload): | |||||||||
self.channel_types: list[ChannelType] = [ | ||||||||||
try_enum(ChannelType, ct) for ct in data.get("channel_types", []) | ||||||||||
] | ||||||||||
_default_values = [] | ||||||||||
for d in data.get("default_values", []): | ||||||||||
if isinstance(d, SelectDefaultValue): | ||||||||||
_default_values.append(d) | ||||||||||
elif isinstance(d, dict) and "id" in d and "type" in d: | ||||||||||
_default_values.append(SelectDefaultValue(id=d["id"], type=d["type"])) | ||||||||||
elif isinstance(d, (User, Member)): | ||||||||||
_default_values.append(SelectDefaultValue(id=d.id, type="user")) | ||||||||||
elif isinstance(d, Role): | ||||||||||
_default_values.append(SelectDefaultValue(id=d.id, type="role")) | ||||||||||
elif isinstance(d, (GuildChannel, Thread)): | ||||||||||
_default_values.append(SelectDefaultValue(id=d.id, type="channel")) | ||||||||||
else: | ||||||||||
raise TypeError( | ||||||||||
f"expected SelectDefaultValue, User, Member, Role, GuildChannel or Mentionable, not {d.__class__}" | ||||||||||
) | ||||||||||
|
||||||||||
self.default_values: list[SelectDefaultValue] = _default_values | ||||||||||
|
||||||||||
def to_dict(self) -> SelectMenuPayload: | ||||||||||
payload: SelectMenuPayload = { | ||||||||||
|
@@ -371,6 +401,8 @@ def to_dict(self) -> SelectMenuPayload: | |||||||||
payload["channel_types"] = [ct.value for ct in self.channel_types] | ||||||||||
if self.placeholder: | ||||||||||
payload["placeholder"] = self.placeholder | ||||||||||
if self.type is not ComponentType.string_select and self.default_values: | ||||||||||
payload["default_values"] = [dv.to_dict() for dv in self.default_values] | ||||||||||
|
||||||||||
return payload | ||||||||||
|
||||||||||
|
@@ -494,6 +526,53 @@ def to_dict(self) -> SelectOptionPayload: | |||||||||
return payload | ||||||||||
|
||||||||||
|
||||||||||
class SelectDefaultValue: | ||||||||||
"""Represents a :class:`discord.SelectMenu`'s default option for user, role, channel and mentionable select menu types. | ||||||||||
doluk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
These can be created by users. | ||||||||||
|
||||||||||
.. versionadded:: 2.7 | ||||||||||
|
||||||||||
Attributes | ||||||||||
---------- | ||||||||||
id: :class:`int` | ||||||||||
The snowflake id of the default option. | ||||||||||
doluk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
type: :class:`SelectMenuDefaultValueType` | ||||||||||
The type of the default value. This is not displayed to users. | ||||||||||
""" | ||||||||||
|
||||||||||
__slots__: tuple[str, ...] = ( | ||||||||||
"id", | ||||||||||
"type", | ||||||||||
) | ||||||||||
|
||||||||||
def __init__(self, *, id: int, type: SelectMenuDefaultValueType) -> None: | ||||||||||
self.id = id | ||||||||||
self.type = type | ||||||||||
Comment on lines
+625
to
+626
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. type annotations
Suggested change
|
||||||||||
|
||||||||||
def __repr__(self) -> str: | ||||||||||
return "<SelectDefaultValue" f" id={self.id!r} type={self.type!r}>" | ||||||||||
|
||||||||||
def __str__(self) -> str: | ||||||||||
return f"{self.id} {self.type}" | ||||||||||
|
||||||||||
@classmethod | ||||||||||
def from_dict(cls, data: SelectDefaultValuePayload) -> SelectDefaultValue: | ||||||||||
|
||||||||||
return cls( | ||||||||||
id=data["id"], | ||||||||||
type=data["type"], | ||||||||||
) | ||||||||||
|
||||||||||
def to_dict(self) -> SelectDefaultValuePayload: | ||||||||||
payload: SelectDefaultValuePayload = { | ||||||||||
"id": self.id, | ||||||||||
"type": self.type.value, | ||||||||||
} | ||||||||||
|
||||||||||
return payload | ||||||||||
|
||||||||||
|
||||||||||
def _component_factory(data: ComponentPayload) -> Component: | ||||||||||
component_type = data["type"] | ||||||||||
if component_type == 1: | ||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -78,6 +78,7 @@ | |||||||||||||
"InteractionContextType", | ||||||||||||||
"PollLayoutType", | ||||||||||||||
"MessageReferenceType", | ||||||||||||||
"SelectMenuDefaultValueType", | ||||||||||||||
) | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
|
@@ -1078,6 +1079,17 @@ class SubscriptionStatus(Enum): | |||||||||||||
inactive = 2 | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
class SelectMenuDefaultValueType(Enum): | ||||||||||||||
"""The type of a select menu's default value.""" | ||||||||||||||
|
||||||||||||||
Channel = "channel" | ||||||||||||||
Role = "role" | ||||||||||||||
User = "user" | ||||||||||||||
Comment on lines
+1094
to
+1096
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should follow the library style:
Suggested change
|
||||||||||||||
|
||||||||||||||
def __str__(self): | ||||||||||||||
return self.name | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
doluk marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
T = TypeVar("T") | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.