mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-06-06 11:57:17 +00:00
Refactor from_components for Select classes
Co-authored-by: Danny <1695103+Rapptz@users.noreply.github.com>
This commit is contained in:
parent
5c5ccc4e82
commit
698363e76b
@ -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',
|
||||
)
|
||||
|
@ -34,7 +34,6 @@ import time
|
||||
import os
|
||||
from .item import Item, ItemCallbackType
|
||||
from .dynamic import DynamicItem
|
||||
from ..enums import ComponentType
|
||||
from ..components import (
|
||||
Component,
|
||||
ActionRow as ActionRowComponent,
|
||||
@ -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)
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user