Skip to content

Commit 7a26484

Browse files
authored
fix: various fixes for embeds (#841)
* feat: add convert_type * fix: use convert_type to allow passing in objects for fields * fix: add fields for EmbedImageStruct * fix: properly typehint embed-related objects
1 parent f7ec708 commit 7a26484

File tree

4 files changed

+45
-22
lines changed

4 files changed

+45
-22
lines changed

interactions/api/models/attrs_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ def inner_convert_dict(dict):
157157
return inner_convert_dict
158158

159159

160+
def convert_type(type_: type):
161+
"""A helper function to convert an input to a specified type."""
162+
163+
def inner_convert_object(value):
164+
return value if isinstance(value, type_) else type_(value)
165+
166+
return inner_convert_object
167+
168+
160169
define_defaults = dict(kw_only=True, eq=False, init=False, on_setattr=attrs.setters.NO_OP)
161170

162171

@@ -175,6 +184,8 @@ def field(
175184
**kwargs,
176185
):
177186
if converter is not None:
187+
if isinstance(converter, type):
188+
converter = convert_type(converter)
178189
converter = attrs.converters.optional(converter)
179190

180191
metadata = kwargs.get("metadata", {})

interactions/api/models/attrs_utils.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from functools import wraps
2-
from typing import Any, Callable, Dict, List, Optional, Tuple, TypeVar, Union
2+
from typing import Any, Callable, Dict, List, Optional, Tuple, TypeVar, Union, Type
33

44
import attrs
55

@@ -55,6 +55,9 @@ def convert_list(converter: Callable[[_T], _P]) -> Callable[[List[_T]], List[_P]
5555
def convert_int(converter: Callable[[int], _T]) -> Callable[[Any], _T]:
5656
"""A helper function to pass an int to the converter, e.x. for Enums"""
5757

58+
def convert_type(object: Type[_T]) -> Callable[[Any], _T]:
59+
"""A helper function to convert an input to a specified type."""
60+
5861
def convert_dict(
5962
key_converter: Optional[Callable[[Any], _T]] = None,
6063
value_converter: Optional[Callable[[Any], _P]] = None,

interactions/api/models/message.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ class EmbedImageStruct(DictSerializerMixin):
279279
:ivar Optional[int] width?: Width of the object.
280280
"""
281281

282+
url: str = field()
283+
proxy_url: Optional[str] = field(default=None)
284+
height: Optional[int] = field(default=None)
285+
width: Optional[int] = field(default=None)
286+
282287
def __setattr__(self, key, value) -> None:
283288
super().__setattr__(key, value)
284289
if key != "_json" and (key not in self._json or value != self._json.get(key)):

interactions/api/models/message.pyi

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -96,51 +96,55 @@ class Emoji(ClientSerializerMixin):
9696

9797
@define()
9898
class EmbedImageStruct(DictSerializerMixin):
99+
url: str
100+
proxy_url: Optional[str] = None
101+
height: Optional[int] = None
102+
width: Optional[int] = None
99103
def __setattr__(self, key, value) -> None: ...
100104

101105
@define()
102106
class EmbedProvider(DictSerializerMixin):
103-
name: Optional[str]
104-
url: Optional[str]
107+
name: Optional[str] = None
108+
url: Optional[str] = None
105109
def __setattr__(self, key, value) -> None: ...
106110

107111
@define()
108112
class EmbedAuthor(DictSerializerMixin):
109113
name: str
110-
url: Optional[str]
111-
icon_url: Optional[str]
112-
proxy_icon_url: Optional[str]
114+
url: Optional[str] = None
115+
icon_url: Optional[str] = None
116+
proxy_icon_url: Optional[str] = None
113117
def __setattr__(self, key, value) -> None: ...
114118

115119
@define()
116120
class EmbedFooter(DictSerializerMixin):
117121
text: str
118-
icon_url: Optional[str]
119-
proxy_icon_url: Optional[str]
122+
icon_url: Optional[str] = None
123+
proxy_icon_url: Optional[str] = None
120124
def __setattr__(self, key, value) -> None: ...
121125

122126
@define()
123127
class EmbedField(DictSerializerMixin):
124128
name: str
125-
inline: Optional[bool]
129+
inline: Optional[bool] = None
126130
value: str
127131
def __setattr__(self, key, value) -> None: ...
128132

129133
@define()
130134
class Embed(DictSerializerMixin):
131-
title: Optional[str]
132-
type: Optional[str]
133-
description: Optional[str]
134-
url: Optional[str]
135-
timestamp: Optional[datetime]
136-
color: Optional[int]
137-
footer: Optional[EmbedFooter]
138-
image: Optional[EmbedImageStruct]
139-
thumbnail: Optional[EmbedImageStruct]
140-
video: Optional[EmbedImageStruct]
141-
provider: Optional[EmbedProvider]
142-
author: Optional[EmbedAuthor]
143-
fields: Optional[List[EmbedField]]
135+
title: Optional[str] = None
136+
type: Optional[str] = None
137+
description: Optional[str] = None
138+
url: Optional[str] = None
139+
timestamp: Optional[datetime] = None
140+
color: Optional[int] = None
141+
footer: Optional[EmbedFooter] = None
142+
image: Optional[EmbedImageStruct] = None
143+
thumbnail: Optional[EmbedImageStruct] = None
144+
video: Optional[EmbedImageStruct] = None
145+
provider: Optional[EmbedProvider] = None
146+
author: Optional[EmbedAuthor] = None
147+
fields: Optional[List[EmbedField]] = None
144148
def __setattr__(self, key, value) -> None: ...
145149
def add_field(self, name: str, value: str, inline: Optional[bool] = ...) -> None: ...
146150
def clear_fields(self) -> None: ...

0 commit comments

Comments
 (0)