Skip to content

Commit 97adb86

Browse files
committed
Refactor AdPreview to handle image attachments
This fixes a bug where, if the user didn't choose to upload their clan emblem, while entering the Slash Command, the bot will throw an excption, saying the URL is invalid.
1 parent e2b29b0 commit 97adb86

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

utils/helpers.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,12 @@ async def download(self, url):
105105
Args:
106106
url (str): The URL used to download the image.
107107
"""
108-
async with aiohttp.ClientSession() as session:
109-
async with session.get(url) as resp:
110-
if resp.status != 200:
111-
raise RequestFailedException()
112-
return io.BytesIO(await resp.read())
108+
109+
try:
110+
async with aiohttp.ClientSession() as session:
111+
async with session.get(url) as resp:
112+
if resp.status != 200:
113+
raise RequestFailedException()
114+
return io.BytesIO(await resp.read())
115+
except aiohttp.client.InvalidURL as exc:
116+
raise RequestFailedException(f"Invalid URL: {url}") from exc

views/billboard_views.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -330,14 +330,21 @@ async def edit_message(self, _content):
330330
"""
331331
member_id = self.member.id
332332
clan_ad_details = await self._get_clan_ad_details(member_id)
333-
color = self._determine_color(
334-
clan_ad_details[ClanAdKey.INVITE_STATUS]
335-
)
336-
emblem_img = await self._download_emblem_image(
337-
clan_ad_details[ClanAdKey.CLAN_EMBLEM_URL]
338-
)
333+
color = self._determine_color(clan_ad_details[ClanAdKey.INVITE_STATUS])
334+
335+
url = clan_ad_details[ClanAdKey.CLAN_EMBLEM_URL]
336+
if url:
337+
emblem_img = await self._download_emblem_image(url)
338+
else:
339+
emblem_img = None
340+
339341
embed = self._build_embed(clan_ad_details, color, emblem_img)
340-
await self._send_embed(embed, _content)
342+
file = None
343+
if url and emblem_img:
344+
file = discord.File(emblem_img, filename="emblem.png")
345+
embed.set_thumbnail(url="attachment://emblem.png")
346+
347+
await self._send_embed(embed, _content, file)
341348

342349
async def _get_clan_ad_details(self, member_id):
343350
keys = [
@@ -348,7 +355,7 @@ async def _get_clan_ad_details(self, member_id):
348355
key: await self.ad_manager.read(
349356
member_id, key=key
350357
) for key in keys
351-
}
358+
}
352359

353360
def _determine_color(self, invite_status):
354361
return Color.green() if invite_status == '0x0' else Color.red()
@@ -393,7 +400,9 @@ def _build_embed(self, details, color, emblem_img):
393400

394401
return embed
395402

396-
async def _send_embed(self, embed, _content):
403+
async def _send_embed(self, embed, _content, file=None):
397404
await self.interaction.edit_original_response(
398-
content=_content, embed=embed, view=self.view
399-
)
405+
content=_content, embed=embed,
406+
attachments=[] if file is None else [file],
407+
view=self.view
408+
)

0 commit comments

Comments
 (0)