Skip to content

Commit d18a9c4

Browse files
authored
basic view support start
1 parent 6e7dde9 commit d18a9c4

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

discord/types/components.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class TextDisplayComponent(BaseComponent):
9999
class SectionComponent(BaseComponent):
100100
type: Literal[9]
101101
components: list[TextDisplayComponent, ButtonComponent]
102+
accessory: NotRequired[Component]
102103

103104

104105
class UnfurledMediaItem(TypedDict):

discord/ui/section.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
from __future__ import annotations
2+
3+
import os
4+
from typing import TYPE_CHECKING
5+
6+
from ..components import Section as SectionComponent
7+
from ..enums import ComponentType
8+
from .item import Item
9+
10+
__all__ = ("InputText",)
11+
12+
if TYPE_CHECKING:
13+
from ..types.components import SectionComponent as SectionComponentPayload
14+
15+
16+
class Section:
17+
"""Represents a UI section.
18+
19+
.. versionadded:: 2.7
20+
21+
Parameters
22+
----------
23+
*items: :class:`Item`
24+
The initial items contained in this section, up to 3. Currently only supports :class:`~discord.ui.TextDisplay` and :class:`~discord.ui.Button`.
25+
accessory: Optional[:class:`Item`]
26+
This section's accessory. This is displayed in the top right of the section. Currently only supports :class:`~discord.ui.TextDisplay` and :class:`~discord.ui.Button`.
27+
"""
28+
29+
def __init__(
30+
self,
31+
*items: Item,
32+
accessory: Item = None
33+
):
34+
super().__init__()
35+
36+
self.items = items
37+
self.accessory = accessory
38+
components = []
39+
40+
self._underlying = SectionComponent._raw_construct(
41+
type=ComponentType.section,
42+
components=components,
43+
accessory=accessory,
44+
)
45+
46+
def add_item(self, item: Item) -> None:
47+
"""Adds an item to the section.
48+
49+
Parameters
50+
----------
51+
item: :class:`Item`
52+
The item to add to the section.
53+
54+
Raises
55+
------
56+
TypeError
57+
An :class:`Item` was not passed.
58+
ValueError
59+
Maximum number of items has been exceeded (10).
60+
"""
61+
62+
if len(self.items) >= 3:
63+
raise ValueError("maximum number of children exceeded")
64+
65+
if not isinstance(item, Item):
66+
raise TypeError(f"expected Item not {item.__class__!r}")
67+
68+
self.items.append(item)
69+
70+
def add_text(self, content: str) -> None:
71+
"""Adds a :class:`TextDisplay` to the section.
72+
73+
Parameters
74+
----------
75+
content: :class:`str`
76+
The content of the TextDisplay
77+
78+
Raises
79+
------
80+
TypeError
81+
An :class:`Item` was not passed.
82+
ValueError
83+
Maximum number of items has been exceeded (3).
84+
"""
85+
86+
if len(self.items) >= 3:
87+
raise ValueError("maximum number of children exceeded")
88+
89+
text = ...
90+
91+
self.items.append(text)
92+
93+
def add_button(self, label: str, ) -> None:
94+
"""finish"""
95+
pass
96+
97+
@property
98+
def type(self) -> ComponentType:
99+
return self._underlying.type
100+
101+
def to_component_dict(self) -> SectionComponentPayload:
102+
return self._underlying.to_dict()

0 commit comments

Comments
 (0)