Skip to content

Commit d4a411c

Browse files
authored
feat: add support for voice messages (#1376)
1 parent f1ade5d commit d4a411c

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

interactions/models/discord/enums.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,8 @@ class Permissions(DiscordIntFlag): # type: ignore
559559
"""Allows for creating emojis, stickers, and soundboard sounds"""
560560
USE_EXTERNAL_SOUNDS = 1 << 45
561561
"""Allows the usage of custom sounds from other servers"""
562+
SEND_VOICE_MESSAGES = 1 << 46
563+
"""Allows for sending audio messages"""
562564

563565
# Shortcuts/grouping/aliases
564566
REQUIRES_MFA = (

interactions/models/discord/message.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import base64
23
import re
34
from dataclasses import dataclass
45
from typing import (
@@ -89,12 +90,22 @@ class Attachment(DiscordObject):
8990
"""width of file (if image)"""
9091
ephemeral: bool = attrs.field(repr=False, default=False)
9192
"""whether this attachment is ephemeral"""
93+
duration_secs: Optional[int] = attrs.field(repr=False, default=None)
94+
"""the duration of the audio file (currently for voice messages)"""
95+
waveform: bytearray = attrs.field(repr=False, default=None)
96+
"""base64 encoded bytearray representing a sampled waveform (currently for voice messages)"""
9297

9398
@property
9499
def resolution(self) -> tuple[Optional[int], Optional[int]]:
95100
"""Returns the image resolution of the attachment file"""
96101
return self.height, self.width
97102

103+
@classmethod
104+
def _process_dict(cls, data: Dict[str, Any], _) -> Dict[str, Any]:
105+
if waveform := data.pop("waveform", None):
106+
data["waveform"] = bytearray(base64.b64decode(waveform))
107+
return data
108+
98109

99110
@attrs.define(eq=False, order=False, hash=False, kw_only=True)
100111
class ChannelMention(DiscordObject):
@@ -369,6 +380,13 @@ def thread(self) -> "models.TYPE_THREAD_CHANNEL":
369380
"""The thread that was started from this message, if any"""
370381
return self._client.cache.get_channel(self.id)
371382

383+
@property
384+
def editable(self) -> bool:
385+
"""Whether this message can be edited by the current user"""
386+
if self.author.id == self._client.user.id:
387+
return MessageFlags.VOICE_MESSAGE not in self.flags
388+
return False
389+
372390
async def fetch_referenced_message(self, *, force: bool = False) -> Optional["Message"]:
373391
"""
374392
Fetch the message this message is referencing, if any.

0 commit comments

Comments
 (0)