Skip to content

Commit db8ecd3

Browse files
committed
feat: allow list operations on ContainerComponent
Ideally improves UX. Hasn't been tested much.
1 parent 0d11f77 commit db8ecd3

File tree

1 file changed

+83
-6
lines changed

1 file changed

+83
-6
lines changed

interactions/models/discord/components.py

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
from collections import UserList
12
import contextlib
23
import uuid
34
from abc import abstractmethod
4-
from typing import Any, Dict, Iterator, List, Optional, Sequence, Union, TYPE_CHECKING
5+
from typing import Any, Dict, Iterator, List, Optional, Sequence, Union, TYPE_CHECKING, overload
6+
from typing_extensions import Self
57

68
import attrs
79
import discord_typings
@@ -989,10 +991,12 @@ def to_dict(self) -> dict:
989991
}
990992

991993

992-
class ContainerComponent(BaseComponent):
993-
components: list[
994+
class ContainerComponent(
995+
BaseComponent,
996+
UserList[
994997
ActionRow | SectionComponent | TextDisplayComponent | MediaGalleryComponent | FileComponent | SeparatorComponent
995-
]
998+
],
999+
):
9961000
accent_color: Optional[int] = None
9971001
spoiler: bool = False
9981002

@@ -1007,13 +1011,86 @@ def __init__(
10071011
accent_color: Optional[int] = None,
10081012
spoiler: bool = False,
10091013
):
1010-
self.components = list(components)
1014+
self.data = list(components)
10111015
self.accent_color = accent_color
10121016
self.spoiler = spoiler
10131017
self.type = ComponentType.CONTAINER
10141018

1019+
@property
1020+
def components(
1021+
self,
1022+
) -> list[
1023+
ActionRow | SectionComponent | TextDisplayComponent | MediaGalleryComponent | FileComponent | SeparatorComponent
1024+
]:
1025+
return self.data
1026+
1027+
@components.setter
1028+
def components(
1029+
self,
1030+
value: list[
1031+
ActionRow
1032+
| SectionComponent
1033+
| TextDisplayComponent
1034+
| MediaGalleryComponent
1035+
| FileComponent
1036+
| SeparatorComponent
1037+
],
1038+
) -> None:
1039+
self.data = value
1040+
1041+
@overload
1042+
def __getitem__(
1043+
self, i: int
1044+
) -> (
1045+
ActionRow | SectionComponent | TextDisplayComponent | MediaGalleryComponent | FileComponent | SeparatorComponent
1046+
): ...
1047+
1048+
@overload
1049+
def __getitem__(self, i: slice) -> Self: ...
1050+
1051+
def __getitem__(
1052+
self, i: int | slice
1053+
) -> (
1054+
Self
1055+
| ActionRow
1056+
| SectionComponent
1057+
| TextDisplayComponent
1058+
| MediaGalleryComponent
1059+
| FileComponent
1060+
| SeparatorComponent
1061+
):
1062+
if isinstance(i, slice):
1063+
return self.__class__(*self.data[i], accent_color=self.accent_color, spoiler=self.spoiler)
1064+
return self.data[i]
1065+
1066+
def __add__(self, other: Any) -> Self:
1067+
if isinstance(other, ContainerComponent):
1068+
return self.__class__(*(self.data + other.data), accent_color=self.accent_color, spoiler=self.spoiler)
1069+
if isinstance(other, UserList):
1070+
return self.__class__(*(self.data + other.data), accent_color=self.accent_color, spoiler=self.spoiler)
1071+
if isinstance(other, type(self.data)):
1072+
return self.__class__(*(self.data + other), accent_color=self.accent_color, spoiler=self.spoiler)
1073+
return self.__class__(*(self.data + list(other)), accent_color=self.accent_color, spoiler=self.spoiler)
1074+
1075+
def __radd__(self, other: Any) -> Self:
1076+
if isinstance(other, ContainerComponent):
1077+
return self.__class__(*(self.data + other.data), accent_color=other.accent_color, spoiler=other.spoiler)
1078+
if isinstance(other, UserList):
1079+
return self.__class__(*(other.data + self.data), accent_color=self.accent_color, spoiler=self.spoiler)
1080+
if isinstance(other, type(self.data)):
1081+
return self.__class__(*(other + self.data), accent_color=self.accent_color, spoiler=self.spoiler)
1082+
return self.__class__(*(list(other) + self.data), accent_color=self.accent_color, spoiler=self.spoiler)
1083+
1084+
def __mul__(self, n: int) -> Self:
1085+
return self.__class__(*(self.data * n), accent_color=self.accent_color, spoiler=self.spoiler)
1086+
1087+
__rmul__ = __mul__
1088+
1089+
def copy(self) -> Self:
1090+
return self.__class__(*self.data, accent_color=self.accent_color, spoiler=self.spoiler)
1091+
10151092
@classmethod
1016-
def from_dict(cls, data: dict) -> "ContainerComponent":
1093+
def from_dict(cls, data: dict) -> Self:
10171094
return cls(
10181095
*[BaseComponent.from_dict_factory(component) for component in data["components"]],
10191096
accent_color=data.get("accent_color"),

0 commit comments

Comments
 (0)