Repository navigation
Expand file tree
/
Copy pathcommand_components.py
More file actions
103 lines (87 loc) · 2.84 KB
/
Copy pathcommand_components.py
File metadata and controls
103 lines (87 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""Reusable Discord Components V2 composition for bot commands."""
from __future__ import annotations
from collections.abc import Iterable, Sequence
from typing import Any
import discord
FIXEMBED_COLOR = 0x5865F2
FIXEMBED_EMOJI_ID = 1525580543503106148
def _render_branded_layout(
view: discord.ui.LayoutView,
*,
title: str,
description: str,
content_blocks: Iterable[str] = (),
controls: Iterable[Sequence[discord.ui.Item[Any]]] = (),
accent_color: int | discord.Color = FIXEMBED_COLOR,
footer: str,
) -> discord.ui.LayoutView:
"""Replace a view with one branded Components V2 container."""
children: list[discord.ui.Item[Any]] = [
discord.ui.TextDisplay(f"## {title}\n{description}".strip())
]
children.extend(
discord.ui.TextDisplay(block)
for block in (str(item).strip() for item in content_blocks)
if block
)
rows = [tuple(row) for row in controls if row]
if rows:
children.append(discord.ui.Separator())
children.extend(discord.ui.ActionRow(*row) for row in rows)
children.extend(
(
discord.ui.Separator(),
discord.ui.TextDisplay(
f"-# <:fixembed:{FIXEMBED_EMOJI_ID}> FixEmbed · {footer}"
),
)
)
view.clear_items()
view.add_item(discord.ui.Container(*children, accent_color=accent_color))
return view
def render_command_layout(
view: discord.ui.LayoutView,
*,
title: str,
description: str,
sections: Iterable[tuple[str, str]] = (),
controls: Iterable[Sequence[discord.ui.Item[Any]]] = (),
accent_color: int | discord.Color = FIXEMBED_COLOR,
footer: str,
) -> discord.ui.LayoutView:
"""Render a structured command card with titled content sections."""
content_blocks = []
for heading, content in sections:
heading = str(heading).strip()
content = str(content).strip()
if heading and content:
content_blocks.append(f"### {heading}\n{content}")
return _render_branded_layout(
view,
title=title,
description=description,
content_blocks=content_blocks,
controls=controls,
accent_color=accent_color,
footer=footer,
)
def render_settings_layout(
view: discord.ui.LayoutView,
*,
title: str,
description: str,
status: str | None = None,
controls: Iterable[Sequence[discord.ui.Item[Any]]] = (),
accent_color: int | discord.Color = FIXEMBED_COLOR,
footer: str = "Settings",
) -> discord.ui.LayoutView:
"""Render a branded settings card with an optional status block."""
return _render_branded_layout(
view,
title=title,
description=description,
content_blocks=(status,) if status else (),
controls=controls,
accent_color=accent_color,
footer=footer,
)