Skip to content

Commit

Permalink
Implement voice messages (hikari-py#1609)
Browse files Browse the repository at this point in the history
  • Loading branch information
davfsa authored and yakMM committed Oct 19, 2023
1 parent 7b0159e commit 99704a1
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions changes/1609.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implement voice messages
2 changes: 2 additions & 0 deletions hikari/impl/entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2946,6 +2946,8 @@ def _deserialize_message_attachment(self, payload: data_binding.JSONObject) -> m
height=payload.get("height"),
width=payload.get("width"),
is_ephemeral=payload.get("ephemeral", False),
duration=payload.get("duration_secs"),
waveform=payload.get("waveform"),
)

def _deserialize_message_reaction(self, payload: data_binding.JSONObject) -> message_models.Reaction:
Expand Down
9 changes: 9 additions & 0 deletions hikari/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ class MessageFlag(enums.Flag):
SUPPRESS_NOTIFICATIONS = 1 << 12
"""This message will not trigger push and desktop notifications."""

IS_VOICE_MESSAGE = 1 << 13
"""This message is a voice message."""


@typing.final
class MessageActivityType(int, enums.Enum):
Expand Down Expand Up @@ -235,6 +238,12 @@ class Attachment(snowflakes.Unique, files.WebResource):
time (but will exist as long as their relevant message exists).
"""

duration: typing.Optional[float] = attrs.field(hash=False, eq=False, repr=False)
"""The duration (in seconds) of the voice message."""

waveform: typing.Optional[str] = attrs.field(hash=False, eq=False, repr=False)
"""A base64 encoded representation of the sampled waveform for the voice message."""

def __str__(self) -> str:
return self.filename

Expand Down
9 changes: 9 additions & 0 deletions hikari/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@ class Permissions(enums.Flag):
MODERATE_MEMBERS = 1 << 40
"""Allows for timing out members."""

VIEW_CREATOR_MONETIZATION_ANALYTICS = 1 << 41
"""Allows for viewing role subscription insights"""

USE_SOUNDBOARD = 1 << 42
"""Allows the use of soundboard in a voice chat."""

SEND_VOICE_MESSAGES = 1 << 46
"""Allows sending voice messages."""

@classmethod
def all_permissions(cls) -> Permissions:
"""Get an instance of `Permissions` with all the known permissions.
Expand Down
8 changes: 8 additions & 0 deletions tests/hikari/impl/test_entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -5537,6 +5537,8 @@ def attachment_payload(self):
"width": 1844,
"height": 2638,
"ephemeral": True,
"duration_secs": 1000.123,
"waveform": "some encoded string",
}

@pytest.fixture()
Expand Down Expand Up @@ -5604,6 +5606,8 @@ def test__deserialize_message_attachment(self, entity_factory_impl, attachment_p
assert attachment.width == 1844
assert attachment.height == 2638
assert attachment.is_ephemeral is True
assert attachment.duration == 1000.123
assert attachment.waveform == "some encoded string"
assert isinstance(attachment, message_models.Attachment)

def test__deserialize_message_attachment_with_null_fields(self, entity_factory_impl, attachment_payload):
Expand All @@ -5621,13 +5625,17 @@ def test__deserialize_message_attachment_with_unset_fields(self, entity_factory_
del attachment_payload["height"]
del attachment_payload["width"]
del attachment_payload["ephemeral"]
del attachment_payload["duration_secs"]
del attachment_payload["waveform"]

attachment = entity_factory_impl._deserialize_message_attachment(attachment_payload)

assert attachment.media_type is None
assert attachment.height is None
assert attachment.width is None
assert attachment.is_ephemeral is False
assert attachment.duration is None
assert attachment.waveform is None

def test_deserialize_partial_message(
self,
Expand Down
2 changes: 2 additions & 0 deletions tests/hikari/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def test_str_operator(self):
size=543,
url="htttt",
is_ephemeral=False,
duration=1,
waveform="122111",
)
assert str(attachment) == "super_cool_file.cool"

Expand Down
2 changes: 1 addition & 1 deletion tests/hikari/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ def test_all_permissions(self):
all_perms = permissions.Permissions.all_permissions()

assert isinstance(all_perms, permissions.Permissions)
assert all_perms == 2199023255551
assert all_perms == 79164837199871

0 comments on commit 99704a1

Please sign in to comment.