From f9ca81a385fb72e12e9ca7c4a542e5d5c29501f1 Mon Sep 17 00:00:00 2001 From: blord0 Date: Wed, 16 Jul 2025 23:30:06 +0100 Subject: [PATCH] Found a much simpler method to send voice messages --- discord/abc.py | 13 +++++++++---- discord/http.py | 45 +-------------------------------------------- 2 files changed, 10 insertions(+), 48 deletions(-) diff --git a/discord/abc.py b/discord/abc.py index 52e3b8e58..4e10e1305 100644 --- a/discord/abc.py +++ b/discord/abc.py @@ -1404,6 +1404,7 @@ class Messageable: suppress_embeds: bool = ..., silent: bool = ..., poll: Poll = ..., + voice: bool = ..., ) -> Message: ... @@ -1425,6 +1426,7 @@ class Messageable: suppress_embeds: bool = ..., silent: bool = ..., poll: Poll = ..., + voice: bool = ..., ) -> Message: ... @@ -1446,6 +1448,7 @@ class Messageable: suppress_embeds: bool = ..., silent: bool = ..., poll: Poll = ..., + voice: bool = ..., ) -> Message: ... @@ -1467,6 +1470,7 @@ class Messageable: suppress_embeds: bool = ..., silent: bool = ..., poll: Poll = ..., + voice: bool = ..., ) -> Message: ... @@ -1489,6 +1493,7 @@ class Messageable: suppress_embeds: bool = False, silent: bool = False, poll: Optional[Poll] = None, + voice: bool = False, ) -> Message: """|coro| @@ -1624,12 +1629,13 @@ class Messageable: if view and not hasattr(view, '__discord_ui_view__'): raise TypeError(f'view parameter must be View not {view.__class__.__name__}') - if suppress_embeds or silent: + if suppress_embeds or silent or voice: from .message import MessageFlags # circular import flags = MessageFlags._from_value(0) flags.suppress_embeds = suppress_embeds flags.suppress_notifications = silent + flags.voice = voice else: flags = MISSING @@ -1937,9 +1943,8 @@ class Messageable: :class:`~discord.Message` The message that was sent. """ - channel = await self._get_channel() - data = await self._state.http.send_voice_message(channel.id, file) - return self._state.create_message(channel=channel, data=data) + + return await self.send(file=file, voice=True) class Connectable(Protocol): diff --git a/discord/http.py b/discord/http.py index f2ea685ea..592bbb884 100644 --- a/discord/http.py +++ b/discord/http.py @@ -53,7 +53,7 @@ import aiohttp from .errors import HTTPException, RateLimited, Forbidden, NotFound, LoginFailure, DiscordServerError, GatewayNotFound from .gateway import DiscordClientWebSocketResponse -from .file import File, VoiceMessageFile +from .file import File from .mentions import AllowedMentions from . import __version__, utils from .utils import MISSING @@ -1060,49 +1060,6 @@ class HTTPClient: def pins_from(self, channel_id: Snowflake) -> Response[List[message.Message]]: return self.request(Route('GET', '/channels/{channel_id}/pins', channel_id=channel_id)) - async def send_voice_message(self, channel_id: Snowflake, voice_message: VoiceMessageFile) -> Any: - upload_route = Route('POST', '/channels/{channel_id}/attachments', channel_id=channel_id) - payload = { - "files": [ - { - "filename": "voice-message.ogg", - "file_size": voice_message.size(), - "id": 0, - } - ] - } - response = await self.request(upload_route, json=payload) - - upload_data = response['attachments'][0] - upload_url = upload_data["upload_url"] - uploaded_filename = upload_data["upload_filename"] - - import requests - - t = requests.put(upload_url, headers={"Content-Type": "audio/ogg"}, data=voice_message.fp) - print(f"Status code: {t.status_code}") - - # x = await self.__session.request("PUT", upload_url, headers={"Content-Type": "audio/ogg"}, data=voice_message.fp) - # print("*********") - # print(upload_url) - # print(x.read()) - # print("*********") - - voice_message.uploaded_filename = uploaded_filename - - r = Route('POST', '/channels/{channel_id}/messages', channel_id=channel_id) - - message_payload = { - "flags": 8192, # IS_VOICE_MESSAGE - "attachments": [voice_message.to_dict(0)], - } - - headers = {"Authorization": f"Bot {self.token}", "Content-Type": "application/json"} - - response = await self.__session.request("post", r.url, headers=headers, json=message_payload) - data = await json_or_text(response) - return data - # Member management def kick(self, user_id: Snowflake, guild_id: Snowflake, reason: Optional[str] = None) -> Response[None]: