Skip to content

Commit 347c245

Browse files
authored
refactor: extract component building into seperate function (#526)
* refactor: extract component building into seperate function * fix!: fix a recursion error
1 parent d266350 commit 347c245

File tree

5 files changed

+127
-597
lines changed

5 files changed

+127
-597
lines changed

interactions/api/models/channel.py

Lines changed: 2 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ async def send(
228228
"""
229229
if not self._client:
230230
raise AttributeError("HTTPClient not found!")
231-
from ...models.component import ActionRow, Button, SelectMenu
231+
from ...models.component import _build_components
232232
from .message import Message
233233

234234
_content: str = "" if content is MISSING else content
@@ -243,106 +243,10 @@ async def send(
243243
else:
244244
_embeds = [embeds._json]
245245

246-
# TODO: Break this obfuscation pattern down to a "builder" method.
247246
if not components or components is MISSING:
248247
_components = []
249248
else:
250-
_components: List[dict] = [{"type": 1, "components": []}]
251-
if isinstance(components, list) and all(
252-
isinstance(action_row, ActionRow) for action_row in components
253-
):
254-
_components = [
255-
{
256-
"type": 1,
257-
"components": [
258-
(
259-
component._json
260-
if component._json.get("custom_id") or component._json.get("url")
261-
else []
262-
)
263-
for component in action_row.components
264-
],
265-
}
266-
for action_row in components
267-
]
268-
elif isinstance(components, list) and all(
269-
isinstance(component, (Button, SelectMenu)) for component in components
270-
):
271-
for component in components:
272-
if isinstance(component, SelectMenu):
273-
component._json["options"] = [
274-
options._json if not isinstance(options, dict) else options
275-
for options in component._json["options"]
276-
]
277-
_components = [
278-
{
279-
"type": 1,
280-
"components": [
281-
(
282-
component._json
283-
if component._json.get("custom_id") or component._json.get("url")
284-
else []
285-
)
286-
for component in components
287-
],
288-
}
289-
]
290-
elif isinstance(components, list) and all(
291-
isinstance(action_row, (list, ActionRow)) for action_row in components
292-
):
293-
_components = []
294-
for action_row in components:
295-
for component in (
296-
action_row if isinstance(action_row, list) else action_row.components
297-
):
298-
if isinstance(component, SelectMenu):
299-
component._json["options"] = [
300-
option._json if not isinstance(option, dict) else option
301-
for option in component.options
302-
]
303-
_components.append(
304-
{
305-
"type": 1,
306-
"components": [
307-
(
308-
component._json
309-
if component._json.get("custom_id")
310-
or component._json.get("url")
311-
else []
312-
)
313-
for component in (
314-
action_row
315-
if isinstance(action_row, list)
316-
else action_row.components
317-
)
318-
],
319-
}
320-
)
321-
elif isinstance(components, ActionRow):
322-
_components[0]["components"] = [
323-
(
324-
component._json
325-
if component._json.get("custom_id") or component._json.get("url")
326-
else []
327-
)
328-
for component in components.components
329-
]
330-
elif isinstance(components, Button):
331-
_components[0]["components"] = (
332-
[components._json]
333-
if components._json.get("custom_id") or components._json.get("url")
334-
else []
335-
)
336-
elif isinstance(components, SelectMenu):
337-
components._json["options"] = [
338-
options._json if not isinstance(options, dict) else options
339-
for options in components._json["options"]
340-
]
341-
_components[0]["components"] = (
342-
[components._json]
343-
if components._json.get("custom_id") or components._json.get("url")
344-
else []
345-
)
249+
_components = _build_components(components=components)
346250

347251
# TODO: post-v4: Add attachments into Message obj.
348252
payload = Message(

interactions/api/models/member.py

Lines changed: 2 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ async def send(
246246
"""
247247
if not self._client:
248248
raise AttributeError("HTTPClient not found!")
249-
from ...models.component import ActionRow, Button, SelectMenu
249+
from ...models.component import _build_components
250250
from .message import Message
251251

252252
_content: str = "" if content is MISSING else content
@@ -262,104 +262,8 @@ async def send(
262262

263263
if not components or components is MISSING:
264264
_components = []
265-
# TODO: Break this obfuscation pattern down to a "builder" method.
266265
else:
267-
_components: List[dict] = [{"type": 1, "components": []}]
268-
if isinstance(components, list) and all(
269-
isinstance(action_row, ActionRow) for action_row in components
270-
):
271-
_components = [
272-
{
273-
"type": 1,
274-
"components": [
275-
(
276-
component._json
277-
if component._json.get("custom_id") or component._json.get("url")
278-
else []
279-
)
280-
for component in action_row.components
281-
],
282-
}
283-
for action_row in components
284-
]
285-
elif isinstance(components, list) and all(
286-
isinstance(component, (Button, SelectMenu)) for component in components
287-
):
288-
for component in components:
289-
if isinstance(component, SelectMenu):
290-
component._json["options"] = [
291-
options._json if not isinstance(options, dict) else options
292-
for options in component._json["options"]
293-
]
294-
_components = [
295-
{
296-
"type": 1,
297-
"components": [
298-
(
299-
component._json
300-
if component._json.get("custom_id") or component._json.get("url")
301-
else []
302-
)
303-
for component in components
304-
],
305-
}
306-
]
307-
elif isinstance(components, list) and all(
308-
isinstance(action_row, (list, ActionRow)) for action_row in components
309-
):
310-
_components = []
311-
for action_row in components:
312-
for component in (
313-
action_row if isinstance(action_row, list) else action_row.components
314-
):
315-
if isinstance(component, SelectMenu):
316-
component._json["options"] = [
317-
option._json if not isinstance(option, dict) else option
318-
for option in component.options
319-
]
320-
_components.append(
321-
{
322-
"type": 1,
323-
"components": [
324-
(
325-
component._json
326-
if component._json.get("custom_id")
327-
or component._json.get("url")
328-
else []
329-
)
330-
for component in (
331-
action_row
332-
if isinstance(action_row, list)
333-
else action_row.components
334-
)
335-
],
336-
}
337-
)
338-
elif isinstance(components, ActionRow):
339-
_components[0]["components"] = [
340-
(
341-
component._json
342-
if component._json.get("custom_id") or component._json.get("url")
343-
else []
344-
)
345-
for component in components.components
346-
]
347-
elif isinstance(components, Button):
348-
_components[0]["components"] = (
349-
[components._json]
350-
if components._json.get("custom_id") or components._json.get("url")
351-
else []
352-
)
353-
elif isinstance(components, SelectMenu):
354-
components._json["options"] = [
355-
options._json if not isinstance(options, dict) else options
356-
for options in components._json["options"]
357-
]
358-
_components[0]["components"] = (
359-
[components._json]
360-
if components._json.get("custom_id") or components._json.get("url")
361-
else []
362-
)
266+
_components = _build_components(components=components)
363267

364268
# TODO: post-v4: Add attachments into Message obj.
365269
payload = Message(

0 commit comments

Comments
 (0)