Skip to content

Commit fe6be64

Browse files
committed
Allow various cv2 components to be used with nqn_common components
1 parent e02bffa commit fe6be64

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

discord/ui/container.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"""
2424
from __future__ import annotations
2525

26+
import asyncio
2627
import copy
2728
import os
2829
from typing import (
@@ -261,6 +262,12 @@ def width(self):
261262
def _is_v2(self) -> bool:
262263
return True
263264

265+
async def serialise(self, ctx) -> Dict[str, Any]:
266+
components = await asyncio.gather(*[
267+
child.serialise(ctx) for child in self._children
268+
])
269+
return self._to_component_dict(components)
270+
264271
def to_components(self) -> List[Dict[str, Any]]:
265272
components = []
266273
for child in sorted(self._children, key=lambda i: i._rendered_row or 0):
@@ -269,7 +276,9 @@ def to_components(self) -> List[Dict[str, Any]]:
269276

270277
def to_component_dict(self) -> Dict[str, Any]:
271278
components = self.to_components()
279+
return self._to_component_dict(components)
272280

281+
def _to_component_dict(self, components: List[Dict[str, Any]]):
273282
colour = None
274283
if self._colour:
275284
colour = self._colour if isinstance(self._colour, int) else self._colour.value
@@ -344,11 +353,9 @@ def add_item(self, item: Item[Any]) -> Self:
344353
Maximum number of children has been exceeded (10).
345354
"""
346355

347-
if len(self._children) >= 10:
348-
raise ValueError('maximum number of children exceeded')
349356

350-
if not isinstance(item, Item):
351-
raise TypeError(f'expected Item not {item.__class__.__name__}')
357+
# if not isinstance(item, Item):
358+
# raise TypeError(f'expected Item not {item.__class__.__name__}')
352359

353360
self._children.append(item)
354361

discord/ui/item.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ def __init__(self):
8888
# and does not break v1 components custom_id property
8989
self.custom_id: str = os.urandom(16).hex()
9090

91+
async def serialise(self, ctx) -> Dict[str, Any]:
92+
return self.to_component_dict()
93+
9194
def to_component_dict(self) -> Dict[str, Any]:
9295
raise NotImplementedError
9396

discord/ui/section.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,14 @@ def from_component(cls, component: SectionComponent) -> Self:
220220
accessory=_component_to_item(component.accessory),
221221
id=component.id,
222222
)
223+
async def serialise(self, ctx) -> Dict[str, Any]:
224+
accessory = await self.accessory.serialise(ctx)
225+
return self._to_component_dict(accessory)
223226

224227
def to_component_dict(self) -> Dict[str, Any]:
228+
return self._to_component_dict(self.accessory.to_component_dict())
229+
230+
def _to_component_dict(self, accessory) -> Dict[str, Any]:
225231
data = {
226232
'type': self.type.value,
227233
'components': [
@@ -231,7 +237,7 @@ def to_component_dict(self) -> Dict[str, Any]:
231237
key=lambda i: i._rendered_row or 0,
232238
)
233239
],
234-
'accessory': self.accessory.to_component_dict(),
240+
'accessory': accessory,
235241
}
236242
if self.id is not None:
237243
data['id'] = self.id

0 commit comments

Comments
 (0)