Skip to content

Commit 2b981d3

Browse files
authored
attempt modal conversion
1 parent 1da73f6 commit 2b981d3

File tree

8 files changed

+418
-378
lines changed

8 files changed

+418
-378
lines changed

discord/components.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def __init__(self, data: InputTextComponentPayload):
266266
self.id: int | None = data.get("id")
267267
self.style: InputTextStyle = try_enum(InputTextStyle, data["style"])
268268
self.custom_id = data["custom_id"]
269-
self.label: str = data.get("label", None)
269+
self.label: str | None = data.get("label", None)
270270
self.placeholder: str | None = data.get("placeholder", None)
271271
self.min_length: int | None = data.get("min_length", None)
272272
self.max_length: int | None = data.get("max_length", None)
@@ -278,7 +278,6 @@ def to_dict(self) -> InputTextComponentPayload:
278278
"type": 4,
279279
"id": self.id,
280280
"style": self.style.value,
281-
"label": self.label,
282281
}
283282
if self.custom_id:
284283
payload["custom_id"] = self.custom_id
@@ -298,6 +297,9 @@ def to_dict(self) -> InputTextComponentPayload:
298297
if self.value:
299298
payload["value"] = self.value
300299

300+
if self.label:
301+
payload["label"] = self.label
302+
301303
return payload # type: ignore
302304

303305

@@ -1305,7 +1307,7 @@ class Label(Component):
13051307
``component`` may only be:
13061308
13071309
- :class:`InputText`
1308-
- :class:`SelectMenu` (string)
1310+
- :class:`SelectMenu`
13091311
13101312
This inherits from :class:`Component`.
13111313

discord/ui/action_row.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def __init__(
6868
):
6969
super().__init__()
7070

71-
self.items: list[Item] = []
71+
self.children: list[Item] = []
7272

7373
self._underlying = ActionRowComponent._raw_construct(
7474
type=ComponentType.action_row,
@@ -114,7 +114,7 @@ def add_item(self, item: Item) -> Self:
114114
item._view = self.view
115115
item.parent = self
116116

117-
self.items.append(item)
117+
self.children.append(item)
118118
self._add_component_from_item(item)
119119
return self
120120

@@ -130,13 +130,13 @@ def remove_item(self, item: Item | str | int) -> Self:
130130
if isinstance(item, (str, int)):
131131
item = self.get_item(item)
132132
try:
133-
self.items.remove(item)
133+
self.children.remove(item)
134134
except ValueError:
135135
pass
136136
return self
137137

138138
def get_item(self, id: str | int) -> Item | None:
139-
"""Get an item from this action row. Roughly equivalent to `utils.get(row.items, ...)`.
139+
"""Get an item from this action row. Roughly equivalent to `utils.get(row.children, ...)`.
140140
If an ``int`` is provided, the item will be retrieved by ``id``, otherwise by ``custom_id``.
141141
142142
Parameters
@@ -152,7 +152,7 @@ def get_item(self, id: str | int) -> Item | None:
152152
if not id:
153153
return None
154154
attr = "id" if isinstance(id, int) else "custom_id"
155-
child = find(lambda i: getattr(i, attr, None) == id, self.items)
155+
child = find(lambda i: getattr(i, attr, None) == id, self.children)
156156
return child
157157

158158
def add_button(
@@ -268,29 +268,25 @@ def add_select(
268268
@Item.view.setter
269269
def view(self, value):
270270
self._view = value
271-
for item in self.items:
271+
for item in self.children:
272272
item.parent = self
273273
item._view = value
274274

275275
@property
276276
def type(self) -> ComponentType:
277277
return self._underlying.type
278278

279-
@property
280-
def width(self) -> int:
281-
return 5
282-
283279
def is_dispatchable(self) -> bool:
284-
return any(item.is_dispatchable() for item in self.items)
280+
return any(item.is_dispatchable() for item in self.children)
285281

286282
def is_persistent(self) -> bool:
287-
return all(item.is_persistent() for item in self.items)
283+
return all(item.is_persistent() for item in self.children)
288284

289285
def refresh_component(self, component: ActionRowComponent) -> None:
290286
self._underlying = component
291287
i = 0
292288
for y in component.components:
293-
x = self.items[i]
289+
x = self.children[i]
294290
x.refresh_component(y)
295291
i += 1
296292

@@ -301,7 +297,7 @@ def disable_all_items(self, *, exclusions: list[Item] | None = None) -> Self:
301297
Parameters
302298
----------
303299
exclusions: Optional[List[:class:`Item`]]
304-
A list of items in `self.items` to not disable.
300+
A list of items in `self.children` to not disable.
305301
"""
306302
for item in self.walk_items():
307303
if exclusions is None or item not in exclusions:
@@ -315,7 +311,7 @@ def enable_all_items(self, *, exclusions: list[Item] | None = None) -> Self:
315311
Parameters
316312
----------
317313
exclusions: Optional[List[:class:`Item`]]
318-
A list of items in `self.items` to not enable.
314+
A list of items in `self.children` to not enable.
319315
"""
320316
for item in self.walk_items():
321317
if hasattr(item, "disabled") and (
@@ -325,10 +321,10 @@ def enable_all_items(self, *, exclusions: list[Item] | None = None) -> Self:
325321
return self
326322

327323
def walk_items(self) -> Iterator[Item]:
328-
yield from self.items
324+
yield from self.children
329325

330326
def to_component_dict(self) -> ActionRowPayload:
331-
self._set_components(self.items)
327+
self._set_components(self.children)
332328
return self._underlying.to_dict()
333329

334330
@classmethod

discord/ui/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def __init__(
8686
*items: Item,
8787
timeout: float | None = 180.0,
8888
):
89-
self.timeout = timeout
89+
self.timeout: float | None = timeout
9090
self.children: list[Item] = []
9191
for item in items:
9292
self.add_item(item)

discord/ui/input_text.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ class InputText:
2727
label: :class:`str`
2828
The label for the input text field.
2929
Must be 45 characters or fewer.
30-
description: Optional[:class:`str`]
31-
The description for the input text field.
32-
Must be 100 characters or fewer.
33-
34-
.. versionadded:: 2.7
3530
placeholder: Optional[:class:`str`]
3631
The placeholder text that is shown if nothing is selected, if any.
3732
Must be 100 characters or fewer.
@@ -64,29 +59,25 @@ class InputText:
6459
"max_length",
6560
"custom_id",
6661
"id",
67-
"description",
6862
)
6963

7064
def __init__(
7165
self,
7266
*,
7367
style: InputTextStyle = InputTextStyle.short,
7468
custom_id: str | None = None,
75-
label: str,
69+
label: str | None = None,
7670
placeholder: str | None = None,
7771
min_length: int | None = None,
7872
max_length: int | None = None,
7973
required: bool | None = True,
8074
value: str | None = None,
8175
row: int | None = None,
8276
id: int | None = None,
83-
description: str | None = None,
8477
):
8578
super().__init__()
86-
if len(str(label)) > 45:
79+
if label and len(str(label)) > 45:
8780
raise ValueError("label must be 45 characters or fewer")
88-
if description and len(description) > 100:
89-
raise ValueError("description must be 100 characters or fewer")
9081
if min_length and (min_length < 0 or min_length > 4000):
9182
raise ValueError("min_length must be between 0 and 4000")
9283
if max_length and (max_length < 0 or max_length > 4000):
@@ -100,7 +91,6 @@ def __init__(
10091
f"expected custom_id to be str, not {custom_id.__class__.__name__}"
10192
)
10293
custom_id = os.urandom(16).hex() if custom_id is None else custom_id
103-
self.description: str | None = description
10494

10595
self._underlying = InputTextComponent._raw_construct(
10696
type=ComponentType.input_text,
@@ -251,8 +241,5 @@ def refresh_state(self, data) -> None:
251241
def refresh_from_modal(self, interaction: Interaction, data: dict) -> None:
252242
return self.refresh_state(data)
253243

254-
def uses_label(self) -> bool:
255-
return self.description is not None
256-
257244

258245
TextInput = InputText

discord/ui/item.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ def view(self) -> V | None:
196196
The parent view of this item, or ``None`` if the item is not attached to any view.
197197
"""
198198
return self._view
199+
200+
@view.setter
201+
def view(self, value) -> None:
202+
self._view = value
203+
199204

200205
async def callback(self, interaction: Interaction):
201206
"""|coro|

0 commit comments

Comments
 (0)