Skip to content

Commit

Permalink
Refactor from_components for Select classes
Browse files Browse the repository at this point in the history
Co-authored-by: Danny <1695103+Rapptz@users.noreply.github.com>
  • Loading branch information
Soheab and Rapptz committed Oct 1, 2023
1 parent 5c5ccc4 commit 698363e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
32 changes: 22 additions & 10 deletions discord/ui/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -336,11 +343,16 @@ def is_dispatchable(self) -> bool:

@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]):
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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',
)
Expand Down
21 changes: 2 additions & 19 deletions discord/ui/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import os
from .item import Item, ItemCallbackType
from .dynamic import DynamicItem
from ..enums import ComponentType
from ..components import (
Component,
ActionRow as ActionRowComponent,
Expand Down Expand Up @@ -79,26 +78,10 @@ def _component_to_item(component: Component) -> Item:

return Button.from_component(component)
if isinstance(component, SelectComponent):
if component.type is ComponentType.select:
from .select import Select
from .select import BaseSelect

return Select.from_component(component)
elif component.type is ComponentType.user_select:
from .select import UserSelect
return BaseSelect.from_component(component)

return UserSelect.from_component(component)
elif component.type is ComponentType.mentionable_select:
from .select import MentionableSelect

return MentionableSelect.from_component(component)
elif component.type is ComponentType.channel_select:
from .select import ChannelSelect

return ChannelSelect().from_component(component)
elif component.type is ComponentType.role_select:
from .select import RoleSelect

return RoleSelect.from_component(component)
return Item.from_component(component)


Expand Down

0 comments on commit 698363e

Please sign in to comment.