disunity 0.2.0__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {disunity-0.2.0/src/disunity.egg-info → disunity-0.3.0}/PKG-INFO +51 -2
- {disunity-0.2.0 → disunity-0.3.0}/README.md +50 -1
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity/__init__.py +1 -1
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity/errors.py +4 -0
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity/models/__init__.py +32 -0
- disunity-0.3.0/src/disunity/models/_v2.py +145 -0
- disunity-0.3.0/src/disunity/models/components.py +1048 -0
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity/models/context.py +145 -17
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity/models/interaction.py +7 -0
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity/models/message.py +50 -3
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity/utils.py +2 -0
- {disunity-0.2.0 → disunity-0.3.0/src/disunity.egg-info}/PKG-INFO +51 -2
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity.egg-info/SOURCES.txt +3 -0
- disunity-0.3.0/tests/test_components_v2.py +619 -0
- disunity-0.3.0/tests/test_send_v2.py +651 -0
- disunity-0.2.0/src/disunity/models/components.py +0 -254
- {disunity-0.2.0 → disunity-0.3.0}/LICENSE +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/pyproject.toml +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/setup.cfg +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity/cache.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity/embed.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity/identifiers.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity/models/attachment.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity/models/channel.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity/models/member.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity/models/resolved_attachment.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity/models/role.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity/models/user.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity/package.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity/server.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity.egg-info/dependency_links.txt +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity.egg-info/requires.txt +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/src/disunity.egg-info/top_level.txt +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/tests/test_autocomplete.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/tests/test_commands.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/tests/test_components.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/tests/test_custom_id.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/tests/test_error_message.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/tests/test_followup.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/tests/test_hooks.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/tests/test_http.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/tests/test_models.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/tests/test_original.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/tests/test_resolved.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/tests/test_resolved_models.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/tests/test_subcommands.py +0 -0
- {disunity-0.2.0 → disunity-0.3.0}/tests/test_transport.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: disunity
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: Python framework for Discord interactions using a web server
|
|
5
5
|
Author: Tadeo Murillo
|
|
6
6
|
License: MIT License
|
|
@@ -49,7 +49,7 @@ Dynamic: license-file
|
|
|
49
49
|
|
|
50
50
|
# disunity
|
|
51
51
|
|
|
52
|
-
Python framework for Discord interactions using a web server (Quart). Version 0.
|
|
52
|
+
Python framework for Discord interactions using a web server (Quart). Version 0.3.0, Python >= 3.10.
|
|
53
53
|
|
|
54
54
|
# Installation
|
|
55
55
|
|
|
@@ -115,6 +115,33 @@ def setup(app):
|
|
|
115
115
|
app.register_package(FirstPackage(app))
|
|
116
116
|
```
|
|
117
117
|
|
|
118
|
+
# Components V2
|
|
119
|
+
|
|
120
|
+
Discord's Components V2 lets a message be built from layout components (text, sections, images, files, separators, containers) instead of `content` and `embeds`. Passing a layout component to `components` enables it automatically (the `IS_COMPONENTS_V2` flag is set for you); such a message can't have `content` or `embeds`:
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
from disunity.models import (
|
|
124
|
+
ActionRow, Button, ButtonStyles, Container, Section, Separator, TextDisplay, Thumbnail,
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
@package.Package.command("poll")
|
|
128
|
+
async def poll(self, ctx):
|
|
129
|
+
return await ctx.callback(components=[
|
|
130
|
+
Container([
|
|
131
|
+
Section(["## Favourite color?", "Vote below."],
|
|
132
|
+
Thumbnail("https://example.com/palette.png", description="Color palette")),
|
|
133
|
+
Separator(),
|
|
134
|
+
TextDisplay("Votes close in 10 minutes."),
|
|
135
|
+
ActionRow([
|
|
136
|
+
Button("vote-blue", "Blue", ButtonStyles.PRIMARY),
|
|
137
|
+
Button("vote-red", "Red", ButtonStyles.DANGER),
|
|
138
|
+
]),
|
|
139
|
+
], accent_color=0x5865F2),
|
|
140
|
+
])
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
See [docs/components.md](docs/components.md) and [docs/gotchas.md](docs/gotchas.md) (#13, including what was verified on Discord and what is still unverified).
|
|
144
|
+
|
|
118
145
|
# Documentation
|
|
119
146
|
|
|
120
147
|
Agent-oriented reference docs live in [docs/](docs/index.md).
|
|
@@ -147,6 +174,28 @@ One-time setup:
|
|
|
147
174
|
|
|
148
175
|
# Changelog
|
|
149
176
|
|
|
177
|
+
## 0.3.0
|
|
178
|
+
|
|
179
|
+
Nothing breaking: legacy messages, components and modals produce exactly the same payloads as in 0.2.0. Components V2 was tested live on Discord (2026-11-29) except for what `docs/gotchas.md` #13 lists as unverified.
|
|
180
|
+
|
|
181
|
+
### Added
|
|
182
|
+
- Components V2 layout components (`disunity.models`): `TextDisplay`, `Section`, `Thumbnail`, `MediaGallery` / `MediaGalleryItem`, `File`, `Separator` / `SeparatorSpacing`, `Container`, all with an optional `id`. Media accepts a URL or an `Attachment` (sent as `attachment://<filename>`); `File` only supports `attachment://`.
|
|
183
|
+
- Native selects `UserSelect`, `RoleSelect`, `MentionableSelect`, `ChannelSelect` (with `channel_types`) and `SelectDefaultValue`; `ComponentTypes` constants.
|
|
184
|
+
- `Label` (modals only) wrapping a `UserTextInput`, `SelectMenu` or any of the new selects; `Modal` now accepts `Label` and `TextDisplay`.
|
|
185
|
+
- `components_v2` parameter on `ctx.callback`, `ctx.followup`, `ctx.edit_original`, `Message.edit_as_webhook` and `Message.edit_as_bot`. The V2 flag is set automatically when a layout component is passed, and kept on messages that already have it.
|
|
186
|
+
- `Message.flags` and `Message.is_components_v2`.
|
|
187
|
+
- `ctx.modal_select_values(custom_id)` to read selects submitted inside a modal `Label`.
|
|
188
|
+
- `utils.MessageFlags.SUPPRESS_EMBEDS` (4) and `IS_COMPONENTS_V2` (`1 << 15`).
|
|
189
|
+
- `errors.ComponentsV2Error` (a `DisunityError` and a `ValueError`) for invalid layouts and for mixing V2 with `content` / `embeds`.
|
|
190
|
+
- Optional new arguments on existing classes: `SelectMenuOption` / `MenuOption` `default`; `SelectMenu` `min_values`, `max_values`, `required`; `UserTextInput` `label` may be `None` when wrapped in a `Label`.
|
|
191
|
+
|
|
192
|
+
### Changed
|
|
193
|
+
- A message may hold at most 40 components in total, nested ones included (raises `ComponentsV2Error`; media gallery items don't count), and a `Container` takes 1 to 40 children.
|
|
194
|
+
- `ctx.edit_original` clears `content` and `embeds` (sends null / `[]`) while it doesn't know the original is V2, so it can turn a legacy original into a V2 message; it now starts from the V2 flag of the component's own message.
|
|
195
|
+
- V2 messages can't have `content` or `embeds`, can't be deferred with type 5 (defer, then `edit_original`), and `ctx.callback` can't upload files (use `followup` / `edit_original` for `attachment://` references).
|
|
196
|
+
- `Modal` with a `Label` or `TextDisplay` validates 1 to 5 components, a title of at most 45 characters and 4000 characters of text in total; modals with only `UserTextInput`s are validated as before.
|
|
197
|
+
- Docs: Components V2 section in `docs/components.md`, `docs/gotchas.md` #13, and updated `context`, `models`, `errors`, `utils`, `index`.
|
|
198
|
+
|
|
150
199
|
## 0.2.0
|
|
151
200
|
|
|
152
201
|
### Fixed
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# disunity
|
|
2
2
|
|
|
3
|
-
Python framework for Discord interactions using a web server (Quart). Version 0.
|
|
3
|
+
Python framework for Discord interactions using a web server (Quart). Version 0.3.0, Python >= 3.10.
|
|
4
4
|
|
|
5
5
|
# Installation
|
|
6
6
|
|
|
@@ -66,6 +66,33 @@ def setup(app):
|
|
|
66
66
|
app.register_package(FirstPackage(app))
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
+
# Components V2
|
|
70
|
+
|
|
71
|
+
Discord's Components V2 lets a message be built from layout components (text, sections, images, files, separators, containers) instead of `content` and `embeds`. Passing a layout component to `components` enables it automatically (the `IS_COMPONENTS_V2` flag is set for you); such a message can't have `content` or `embeds`:
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from disunity.models import (
|
|
75
|
+
ActionRow, Button, ButtonStyles, Container, Section, Separator, TextDisplay, Thumbnail,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
@package.Package.command("poll")
|
|
79
|
+
async def poll(self, ctx):
|
|
80
|
+
return await ctx.callback(components=[
|
|
81
|
+
Container([
|
|
82
|
+
Section(["## Favourite color?", "Vote below."],
|
|
83
|
+
Thumbnail("https://example.com/palette.png", description="Color palette")),
|
|
84
|
+
Separator(),
|
|
85
|
+
TextDisplay("Votes close in 10 minutes."),
|
|
86
|
+
ActionRow([
|
|
87
|
+
Button("vote-blue", "Blue", ButtonStyles.PRIMARY),
|
|
88
|
+
Button("vote-red", "Red", ButtonStyles.DANGER),
|
|
89
|
+
]),
|
|
90
|
+
], accent_color=0x5865F2),
|
|
91
|
+
])
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
See [docs/components.md](docs/components.md) and [docs/gotchas.md](docs/gotchas.md) (#13, including what was verified on Discord and what is still unverified).
|
|
95
|
+
|
|
69
96
|
# Documentation
|
|
70
97
|
|
|
71
98
|
Agent-oriented reference docs live in [docs/](docs/index.md).
|
|
@@ -98,6 +125,28 @@ One-time setup:
|
|
|
98
125
|
|
|
99
126
|
# Changelog
|
|
100
127
|
|
|
128
|
+
## 0.3.0
|
|
129
|
+
|
|
130
|
+
Nothing breaking: legacy messages, components and modals produce exactly the same payloads as in 0.2.0. Components V2 was tested live on Discord (2026-11-29) except for what `docs/gotchas.md` #13 lists as unverified.
|
|
131
|
+
|
|
132
|
+
### Added
|
|
133
|
+
- Components V2 layout components (`disunity.models`): `TextDisplay`, `Section`, `Thumbnail`, `MediaGallery` / `MediaGalleryItem`, `File`, `Separator` / `SeparatorSpacing`, `Container`, all with an optional `id`. Media accepts a URL or an `Attachment` (sent as `attachment://<filename>`); `File` only supports `attachment://`.
|
|
134
|
+
- Native selects `UserSelect`, `RoleSelect`, `MentionableSelect`, `ChannelSelect` (with `channel_types`) and `SelectDefaultValue`; `ComponentTypes` constants.
|
|
135
|
+
- `Label` (modals only) wrapping a `UserTextInput`, `SelectMenu` or any of the new selects; `Modal` now accepts `Label` and `TextDisplay`.
|
|
136
|
+
- `components_v2` parameter on `ctx.callback`, `ctx.followup`, `ctx.edit_original`, `Message.edit_as_webhook` and `Message.edit_as_bot`. The V2 flag is set automatically when a layout component is passed, and kept on messages that already have it.
|
|
137
|
+
- `Message.flags` and `Message.is_components_v2`.
|
|
138
|
+
- `ctx.modal_select_values(custom_id)` to read selects submitted inside a modal `Label`.
|
|
139
|
+
- `utils.MessageFlags.SUPPRESS_EMBEDS` (4) and `IS_COMPONENTS_V2` (`1 << 15`).
|
|
140
|
+
- `errors.ComponentsV2Error` (a `DisunityError` and a `ValueError`) for invalid layouts and for mixing V2 with `content` / `embeds`.
|
|
141
|
+
- Optional new arguments on existing classes: `SelectMenuOption` / `MenuOption` `default`; `SelectMenu` `min_values`, `max_values`, `required`; `UserTextInput` `label` may be `None` when wrapped in a `Label`.
|
|
142
|
+
|
|
143
|
+
### Changed
|
|
144
|
+
- A message may hold at most 40 components in total, nested ones included (raises `ComponentsV2Error`; media gallery items don't count), and a `Container` takes 1 to 40 children.
|
|
145
|
+
- `ctx.edit_original` clears `content` and `embeds` (sends null / `[]`) while it doesn't know the original is V2, so it can turn a legacy original into a V2 message; it now starts from the V2 flag of the component's own message.
|
|
146
|
+
- V2 messages can't have `content` or `embeds`, can't be deferred with type 5 (defer, then `edit_original`), and `ctx.callback` can't upload files (use `followup` / `edit_original` for `attachment://` references).
|
|
147
|
+
- `Modal` with a `Label` or `TextDisplay` validates 1 to 5 components, a title of at most 45 characters and 4000 characters of text in total; modals with only `UserTextInput`s are validated as before.
|
|
148
|
+
- Docs: Components V2 section in `docs/components.md`, `docs/gotchas.md` #13, and updated `context`, `models`, `errors`, `utils`, `index`.
|
|
149
|
+
|
|
101
150
|
## 0.2.0
|
|
102
151
|
|
|
103
152
|
### Fixed
|
|
@@ -7,6 +7,10 @@ class InvalidMethodUse(DisunityError):
|
|
|
7
7
|
super().__init__(reason)
|
|
8
8
|
|
|
9
9
|
|
|
10
|
+
class ComponentsV2Error(DisunityError, ValueError):
|
|
11
|
+
"""Invalid Components V2 layout or an invalid mix of V2 and legacy message fields."""
|
|
12
|
+
|
|
13
|
+
|
|
10
14
|
class CommandNotFound(DisunityError):
|
|
11
15
|
def __init__(self, command):
|
|
12
16
|
super().__init__(f"Command with name {command} could not be found")
|
|
@@ -4,11 +4,27 @@ from .components import (
|
|
|
4
4
|
ActionRow,
|
|
5
5
|
Button,
|
|
6
6
|
ButtonStyles,
|
|
7
|
+
ChannelSelect,
|
|
8
|
+
ComponentTypes,
|
|
9
|
+
Container,
|
|
10
|
+
File,
|
|
11
|
+
Label,
|
|
12
|
+
MediaGallery,
|
|
13
|
+
MediaGalleryItem,
|
|
14
|
+
MentionableSelect,
|
|
7
15
|
MenuOption,
|
|
8
16
|
Modal,
|
|
17
|
+
RoleSelect,
|
|
18
|
+
Section,
|
|
19
|
+
SelectDefaultValue,
|
|
9
20
|
SelectMenu,
|
|
10
21
|
SelectMenuOption,
|
|
22
|
+
Separator,
|
|
23
|
+
SeparatorSpacing,
|
|
24
|
+
TextDisplay,
|
|
11
25
|
TextInputStyles,
|
|
26
|
+
Thumbnail,
|
|
27
|
+
UserSelect,
|
|
12
28
|
UserTextInput,
|
|
13
29
|
)
|
|
14
30
|
from .context import Context
|
|
@@ -24,16 +40,32 @@ __all__ = [
|
|
|
24
40
|
"Button",
|
|
25
41
|
"ButtonStyles",
|
|
26
42
|
"Channel",
|
|
43
|
+
"ChannelSelect",
|
|
44
|
+
"ComponentTypes",
|
|
45
|
+
"Container",
|
|
27
46
|
"Context",
|
|
47
|
+
"File",
|
|
48
|
+
"Label",
|
|
49
|
+
"MediaGallery",
|
|
50
|
+
"MediaGalleryItem",
|
|
28
51
|
"Member",
|
|
52
|
+
"MentionableSelect",
|
|
29
53
|
"MenuOption",
|
|
30
54
|
"Message",
|
|
31
55
|
"Modal",
|
|
32
56
|
"ResolvedAttachment",
|
|
33
57
|
"Role",
|
|
58
|
+
"RoleSelect",
|
|
59
|
+
"Section",
|
|
60
|
+
"SelectDefaultValue",
|
|
34
61
|
"SelectMenu",
|
|
35
62
|
"SelectMenuOption",
|
|
63
|
+
"Separator",
|
|
64
|
+
"SeparatorSpacing",
|
|
65
|
+
"TextDisplay",
|
|
36
66
|
"TextInputStyles",
|
|
67
|
+
"Thumbnail",
|
|
37
68
|
"User",
|
|
69
|
+
"UserSelect",
|
|
38
70
|
"UserTextInput",
|
|
39
71
|
]
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"""Helpers shared by the message sending methods for Components V2."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .. import errors
|
|
6
|
+
from ..utils import MessageFlags
|
|
7
|
+
from .components import (
|
|
8
|
+
ATTACHMENT_PREFIX,
|
|
9
|
+
ActionRow,
|
|
10
|
+
Container,
|
|
11
|
+
File,
|
|
12
|
+
Label,
|
|
13
|
+
MediaGallery,
|
|
14
|
+
MediaGalleryItem,
|
|
15
|
+
Section,
|
|
16
|
+
SelectDefaultValue,
|
|
17
|
+
Separator,
|
|
18
|
+
TextDisplay,
|
|
19
|
+
Thumbnail,
|
|
20
|
+
_check_text_total,
|
|
21
|
+
iter_components,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
#: Top-level components that are only available with Components V2.
|
|
25
|
+
V2_LAYOUT = (Section, TextDisplay, MediaGallery, File, Separator, Container)
|
|
26
|
+
#: New classes that are never valid at the top level of a message.
|
|
27
|
+
_NEVER_TOP_LEVEL = (Thumbnail, Label, MediaGalleryItem, SelectDefaultValue)
|
|
28
|
+
|
|
29
|
+
#: Total components in a message, nested ones included.
|
|
30
|
+
MAX_TOTAL_COMPONENTS = 40
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def normalize(components) -> list:
|
|
34
|
+
"""None -> [], a single object -> [obj], an iterable -> list."""
|
|
35
|
+
if components is None:
|
|
36
|
+
return []
|
|
37
|
+
if not isinstance(components, (str, bytes)) and hasattr(components, "__iter__"):
|
|
38
|
+
return list(components)
|
|
39
|
+
return [components]
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def has_layout(components) -> bool:
|
|
43
|
+
"""Does the top level contain a Components V2 layout component?"""
|
|
44
|
+
return any(isinstance(c, V2_LAYOUT) for c in normalize(components))
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def v2_flags(*, ephemeral: bool = False, keep: int = 0) -> int:
|
|
48
|
+
"""Message flags of a Components V2 message; ``keep`` carries over flags to preserve."""
|
|
49
|
+
flags = MessageFlags.IS_COMPONENTS_V2 | keep
|
|
50
|
+
if ephemeral:
|
|
51
|
+
flags |= MessageFlags.EPHEMERAL
|
|
52
|
+
return int(flags)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def clear_legacy_fields(body: dict) -> None:
|
|
56
|
+
"""
|
|
57
|
+
Clear the content and embeds of a legacy message that is converted to Components V2.
|
|
58
|
+
|
|
59
|
+
Discord rejects setting the flag while they are present (verified live; content
|
|
60
|
+
null + embeds [] is accepted, also on an empty deferred response).
|
|
61
|
+
"""
|
|
62
|
+
body["content"] = None
|
|
63
|
+
body["embeds"] = []
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def require_components(built: list, where: str, *, converting: bool = False) -> None:
|
|
67
|
+
"""A Components V2 message can't be empty; ``built`` is the serialised component list."""
|
|
68
|
+
if not built:
|
|
69
|
+
detail = " when converting a message to Components V2" if converting else ""
|
|
70
|
+
raise errors.ComponentsV2Error(
|
|
71
|
+
f"{where}: a Components V2 message needs at least one component{detail}"
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def check_v2_fields(content, embeds, where: str) -> None:
|
|
76
|
+
"""Content and embeds can't be combined with Components V2."""
|
|
77
|
+
if content is not None and content != "":
|
|
78
|
+
raise errors.ComponentsV2Error(
|
|
79
|
+
f"{where}: content can't be used with Components V2; put text in a TextDisplay"
|
|
80
|
+
)
|
|
81
|
+
if embeds is not None and not (isinstance(embeds, (list, tuple)) and len(embeds) == 0):
|
|
82
|
+
raise errors.ComponentsV2Error(
|
|
83
|
+
f"{where}: embeds can't be used with Components V2; use a Container or MediaGallery"
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def build_message_components(
|
|
88
|
+
components, *, force_v2: bool, where: str
|
|
89
|
+
) -> tuple[list[dict], bool]:
|
|
90
|
+
"""
|
|
91
|
+
Serialise the components of a message.
|
|
92
|
+
|
|
93
|
+
Returns ``(component dicts, is_v2)``. Without V2 the result is exactly what
|
|
94
|
+
the legacy code produced: only ActionRows are kept, anything else is dropped.
|
|
95
|
+
"""
|
|
96
|
+
items = normalize(components)
|
|
97
|
+
|
|
98
|
+
for item in items:
|
|
99
|
+
if isinstance(item, _NEVER_TOP_LEVEL):
|
|
100
|
+
name = type(item).__name__
|
|
101
|
+
if isinstance(item, Thumbnail):
|
|
102
|
+
rule = "can only be used as a Section accessory"
|
|
103
|
+
elif isinstance(item, Label):
|
|
104
|
+
rule = "can only be used in a modal"
|
|
105
|
+
else:
|
|
106
|
+
rule = "can't be used as a message component"
|
|
107
|
+
raise errors.ComponentsV2Error(f"{where}: {name} {rule}")
|
|
108
|
+
|
|
109
|
+
is_v2 = force_v2 or any(isinstance(item, V2_LAYOUT) for item in items)
|
|
110
|
+
if not is_v2:
|
|
111
|
+
return [item.to_dict() for item in items if isinstance(item, ActionRow)], False
|
|
112
|
+
|
|
113
|
+
for item in items:
|
|
114
|
+
if not isinstance(item, (ActionRow, *V2_LAYOUT)):
|
|
115
|
+
raise errors.ComponentsV2Error(
|
|
116
|
+
f"{where}: {type(item).__name__} can't be a top-level component of a "
|
|
117
|
+
"Components V2 message"
|
|
118
|
+
)
|
|
119
|
+
walked = list(iter_components(items))
|
|
120
|
+
_check_text_total(where, walked)
|
|
121
|
+
|
|
122
|
+
# Verified live: nested components count ("Total number of components cannot
|
|
123
|
+
# exceed 40"). MediaGalleryItems are not components and don't count.
|
|
124
|
+
total = sum(1 for c in walked if not isinstance(c, MediaGalleryItem))
|
|
125
|
+
if total > MAX_TOTAL_COMPONENTS:
|
|
126
|
+
raise errors.ComponentsV2Error(
|
|
127
|
+
f"{where}: a message accepts up to {MAX_TOTAL_COMPONENTS} components including "
|
|
128
|
+
f"nested ones (rows, buttons, section accessories, ...), got {total}"
|
|
129
|
+
)
|
|
130
|
+
return [item.to_dict() for item in items], True
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def attachment_refs(components) -> set[str]:
|
|
134
|
+
"""Filenames referenced through ``attachment://`` by Thumbnail, MediaGalleryItem and File."""
|
|
135
|
+
refs: set[str] = set()
|
|
136
|
+
for comp in iter_components(normalize(components)):
|
|
137
|
+
if isinstance(comp, (Thumbnail, MediaGalleryItem)):
|
|
138
|
+
url = comp.media["url"]
|
|
139
|
+
elif isinstance(comp, File):
|
|
140
|
+
url = comp.file["url"]
|
|
141
|
+
else:
|
|
142
|
+
continue
|
|
143
|
+
if url.startswith(ATTACHMENT_PREFIX):
|
|
144
|
+
refs.add(url[len(ATTACHMENT_PREFIX) :])
|
|
145
|
+
return refs
|