Skip to content

Commit

Permalink
Add missing clear methods for InteractionMessageBuilder (#1740)
Browse files Browse the repository at this point in the history
Signed-off-by: davfsa <davfsa@gmail.com>
  • Loading branch information
davfsa authored Nov 2, 2023
1 parent 49de2fe commit ae27ba8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
2 changes: 2 additions & 0 deletions changes/1740.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add missing `clear_x` methods to `InteractionMessageBuilder`
- This brings the functionality more in-line with other message edit APIs
6 changes: 3 additions & 3 deletions hikari/api/special_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,19 +683,19 @@ def attachments(self) -> undefined.UndefinedNoneOr[typing.Sequence[files.Resourc

@property
@abc.abstractmethod
def components(self) -> undefined.UndefinedOr[typing.Sequence[ComponentBuilder]]:
def components(self) -> undefined.UndefinedNoneOr[typing.Sequence[ComponentBuilder]]:
"""Sequence of up to 5 component builders to send in this response."""

@property
@abc.abstractmethod
def embeds(self) -> undefined.UndefinedOr[typing.Sequence[embeds_.Embed]]:
def embeds(self) -> undefined.UndefinedNoneOr[typing.Sequence[embeds_.Embed]]:
"""Sequence of up to 10 of the embeds included in this response."""

# Settable fields

@property
@abc.abstractmethod
def content(self) -> undefined.UndefinedOr[str]:
def content(self) -> undefined.UndefinedNoneOr[str]:
"""Response's message content."""

@property
Expand Down
50 changes: 34 additions & 16 deletions hikari/impl/special_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ class InteractionMessageBuilder(special_endpoints.InteractionMessageBuilder):
)

# Not-required arguments.
_content: undefined.UndefinedOr[str] = attrs.field(alias="content", default=undefined.UNDEFINED)
_content: undefined.UndefinedNoneOr[str] = attrs.field(alias="content", default=undefined.UNDEFINED)

# Key-word only not-required arguments.
_flags: typing.Union[int, messages.MessageFlag, undefined.UndefinedType] = attrs.field(
Expand All @@ -1030,10 +1030,10 @@ class InteractionMessageBuilder(special_endpoints.InteractionMessageBuilder):
_attachments: undefined.UndefinedNoneOr[typing.List[files.Resourceish]] = attrs.field(
alias="attachments", default=undefined.UNDEFINED, kw_only=True
)
_components: undefined.UndefinedOr[typing.List[special_endpoints.ComponentBuilder]] = attrs.field(
_components: undefined.UndefinedNoneOr[typing.List[special_endpoints.ComponentBuilder]] = attrs.field(
alias="components", default=undefined.UNDEFINED, kw_only=True
)
_embeds: undefined.UndefinedOr[typing.List[embeds_.Embed]] = attrs.field(
_embeds: undefined.UndefinedNoneOr[typing.List[embeds_.Embed]] = attrs.field(
alias="embeds", default=undefined.UNDEFINED, kw_only=True
)

Expand All @@ -1042,16 +1042,16 @@ def attachments(self) -> undefined.UndefinedNoneOr[typing.Sequence[files.Resourc
return self._attachments.copy() if self._attachments else self._attachments

@property
def content(self) -> undefined.UndefinedOr[str]:
def content(self) -> undefined.UndefinedNoneOr[str]:
return self._content

@property
def components(self) -> undefined.UndefinedOr[typing.Sequence[special_endpoints.ComponentBuilder]]:
return self._components.copy() if self._components is not undefined.UNDEFINED else undefined.UNDEFINED
def components(self) -> undefined.UndefinedNoneOr[typing.Sequence[special_endpoints.ComponentBuilder]]:
return self._components.copy() if self._components else self._components

@property
def embeds(self) -> undefined.UndefinedOr[typing.Sequence[embeds_.Embed]]:
return self._embeds.copy() if self._embeds is not undefined.UNDEFINED else undefined.UNDEFINED
def embeds(self) -> undefined.UndefinedNoneOr[typing.Sequence[embeds_.Embed]]:
return self._embeds.copy() if self._embeds else self._embeds

@property
def flags(self) -> typing.Union[undefined.UndefinedType, int, messages.MessageFlag]:
Expand Down Expand Up @@ -1081,35 +1081,47 @@ def user_mentions(
) -> undefined.UndefinedOr[typing.Union[snowflakes.SnowflakeishSequence[users.PartialUser], bool]]:
return self._user_mentions

def clear_attachments(self, /) -> Self:
self._attachments = None
return self

def add_attachment(self, attachment: files.Resourceish, /) -> Self:
if not self._attachments:
self._attachments = []

self._attachments.append(attachment)
return self

def clear_attachments(self, /) -> Self:
self._attachments = None
return self

def add_component(self, component: special_endpoints.ComponentBuilder, /) -> Self:
if self._components is undefined.UNDEFINED:
if not self._components:
self._components = []

self._components.append(component)
return self

def clear_components(self, /) -> Self:
self._components = None
return self

def add_embed(self, embed: embeds_.Embed, /) -> Self:
if self._embeds is undefined.UNDEFINED:
if not self._embeds:
self._embeds = []

self._embeds.append(embed)
return self

def clear_embeds(self, /) -> Self:
self._embeds = None
return self

def set_content(self, content: undefined.UndefinedOr[str], /) -> Self:
self._content = str(content) if content is not undefined.UNDEFINED else undefined.UNDEFINED
return self

def clear_content(self, /) -> Self:
self._content = None
return self

def set_flags(self, flags: typing.Union[undefined.UndefinedType, int, messages.MessageFlag], /) -> Self:
self._flags = flags
return self
Expand Down Expand Up @@ -1165,15 +1177,21 @@ def build(
elif self._attachments is None:
data.put("attachments", None)

if self._embeds is not undefined.UNDEFINED:
if self._embeds:
embeds: typing.List[data_binding.JSONObject] = []
for embed, attachments in map(entity_factory.serialize_embed, self._embeds):
final_attachments.extend(attachments)
embeds.append(embed)

data["embeds"] = embeds
elif self._embeds is None:
data.put("embeds", None)

if self._components:
data.put_array("components", self._components, conversion=lambda component: component.build())
elif self._components is None:
data.put("components", None)

data.put_array("components", self._components, conversion=lambda component: component.build())
data.put("flags", self.flags)
data.put("tts", self.is_tts)

Expand Down
5 changes: 1 addition & 4 deletions tests/hikari/impl/test_special_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,10 +927,7 @@ def test_build_for_partial_when_empty_lists(self):
result, attachments = builder.build(mock_entity_factory)

mock_entity_factory.serialize_embed.assert_not_called()
assert result == {
"type": base_interactions.ResponseType.MESSAGE_UPDATE,
"data": {"components": [], "embeds": []},
}
assert result == {"type": base_interactions.ResponseType.MESSAGE_UPDATE, "data": {}}
assert attachments == []

def test_build_handles_attachments(self):
Expand Down

0 comments on commit ae27ba8

Please sign in to comment.