From 0c8c721d60f6ac881afdfaa456962e85a0246d52 Mon Sep 17 00:00:00 2001 From: EdVraz Date: Thu, 11 Aug 2022 18:54:40 +0200 Subject: [PATCH] feat: add download helper method for Attachments --- interactions/api/models/message.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/interactions/api/models/message.py b/interactions/api/models/message.py index ad10e27d1..4334e670b 100644 --- a/interactions/api/models/message.py +++ b/interactions/api/models/message.py @@ -1,6 +1,7 @@ import contextlib from datetime import datetime from enum import IntEnum +from io import BytesIO from typing import TYPE_CHECKING, List, Optional, Union from ...client.models.component import ActionRow, Button, SelectMenu @@ -148,6 +149,22 @@ class Attachment(ClientSerializerMixin, IDMixin): width: Optional[int] = field(default=None) ephemeral: Optional[bool] = field(default=None) + async def download(self) -> BytesIO: + """ + Downloads the attachment. + + :returns: The attachment's bytes as BytesIO object + :rtype: BytesIO + """ + + if not self._client: + raise LibraryException(code=13) + + async with self._client._req._session.get(self.url) as response: + _bytes: bytes = await response.content.read() + + return BytesIO(_bytes) + @define() class MessageInteraction(ClientSerializerMixin, IDMixin):