Refactor from_components for Select classes

Co-authored-by: Danny <1695103+Rapptz@users.noreply.github.com>
This commit is contained in:
Soheab_
2023-10-01 03:21:29 +02:00
committed by GitHub
parent 5c5ccc4e82
commit 698363e76b
2 changed files with 24 additions and 29 deletions

View File

@ -216,6 +216,13 @@ class BaseSelect(Item[V]):
'max_values',
'disabled',
)
__component_attributes__: Tuple[str, ...] = (
'custom_id',
'placeholder',
'min_values',
'max_values',
'disabled',
)
def __init__(
self,
@ -336,11 +343,16 @@ class BaseSelect(Item[V]):
@classmethod
def from_component(cls, component: SelectMenu) -> Self:
return cls(
**{k: getattr(component, k) for k in cls.__item_repr_attributes__},
custom_id=component.custom_id,
row=None,
)
type_to_cls: Dict[ComponentType, Type[BaseSelect[Any]]] = {
ComponentType.string_select: Select,
ComponentType.user_select: UserSelect,
ComponentType.role_select: RoleSelect,
ComponentType.channel_select: ChannelSelect,
ComponentType.mentionable_select: MentionableSelect,
}
constructor = type_to_cls.get(component.type, Select)
kwrgs = {key: getattr(component, key) for key in constructor.__component_attributes__}
return constructor(**kwrgs)
class Select(BaseSelect[V]):
@ -374,7 +386,7 @@ class Select(BaseSelect[V]):
ordering. The row number must be between 0 and 4 (i.e. zero indexed).
"""
__item_repr_attributes__ = BaseSelect.__item_repr_attributes__ + ('options',)
__component_attributes__ = BaseSelect.__component_attributes__ + ('options',)
def __init__(
self,
@ -525,7 +537,7 @@ class UserSelect(BaseSelect[V]):
ordering. The row number must be between 0 and 4 (i.e. zero indexed).
"""
__item_repr_attributes__ = BaseSelect.__item_repr_attributes__ + ('default_values',)
__component_attributes__ = BaseSelect.__component_attributes__ + ('default_values',)
def __init__(
self,
@ -614,7 +626,7 @@ class RoleSelect(BaseSelect[V]):
ordering. The row number must be between 0 and 4 (i.e. zero indexed).
"""
__item_repr_attributes__ = BaseSelect.__item_repr_attributes__ + ('default_values',)
__component_attributes__ = BaseSelect.__component_attributes__ + ('default_values',)
def __init__(
self,
@ -699,7 +711,7 @@ class MentionableSelect(BaseSelect[V]):
ordering. The row number must be between 0 and 4 (i.e. zero indexed).
"""
__item_repr_attributes__ = BaseSelect.__item_repr_attributes__ + ('default_values',)
__component_attributes__ = BaseSelect.__component_attributes__ + ('default_values',)
def __init__(
self,
@ -790,7 +802,7 @@ class ChannelSelect(BaseSelect[V]):
ordering. The row number must be between 0 and 4 (i.e. zero indexed).
"""
__item_repr_attributes__ = BaseSelect.__item_repr_attributes__ + (
__component_attributes__ = BaseSelect.__component_attributes__ + (
'channel_types',
'default_values',
)