From 19e68e26b64af53a66754db11d97466585621c6c Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 13 Feb 2021 19:17:35 +0100 Subject: [PATCH 1/7] Utils: fix broken type annotations --- ryan/utils/messages.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ryan/utils/messages.py b/ryan/utils/messages.py index fc6bb13..020319b 100644 --- a/ryan/utils/messages.py +++ b/ryan/utils/messages.py @@ -24,7 +24,7 @@ ) -def msg_success(message: str) -> discord.Embed(): +def msg_success(message: str) -> discord.Embed: """Create a success embed with `message`.""" embed = discord.Embed( description=message, @@ -34,7 +34,7 @@ def msg_success(message: str) -> discord.Embed(): return embed -def msg_error(message: str) -> discord.Embed(): +def msg_error(message: str) -> discord.Embed: """Create an error embed with `message`.""" embed = discord.Embed( description=message, From 2dd7bd7a39f3958cafb4873832ac53863a3f7193 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 13 Feb 2021 19:17:54 +0100 Subject: [PATCH 2/7] Utils: upper-case constant names --- ryan/utils/messages.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ryan/utils/messages.py b/ryan/utils/messages.py index 020319b..190d9a1 100644 --- a/ryan/utils/messages.py +++ b/ryan/utils/messages.py @@ -5,15 +5,14 @@ from ryan.constants import Images -title_success = ( +TITLE_SUCCESS = ( "Good point", "Great moves Ethan", "Keep it up", "Life is excellent!", "The daemon congratulates you", ) - -title_error = ( +TITLE_ERROR = ( "Galooned again", "Put on full blast", "Reduced you to memes", @@ -30,7 +29,7 @@ def msg_success(message: str) -> discord.Embed: description=message, colour=discord.Colour.green(), ) - embed.set_author(name=random.choice(title_success), icon_url=Images.gm_creepy) + embed.set_author(name=random.choice(TITLE_SUCCESS), icon_url=Images.gm_creepy) return embed @@ -40,7 +39,7 @@ def msg_error(message: str) -> discord.Embed: description=message, colour=discord.Colour.red(), ) - embed.set_author(name=random.choice(title_error), icon_url=Images.gm_creepy) + embed.set_author(name=random.choice(TITLE_ERROR), icon_url=Images.gm_creepy) return embed From 5a62a30812344b4cffbeb2e559c2dd6cb2f7c4c2 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 13 Feb 2021 19:27:35 +0100 Subject: [PATCH 3/7] Utils: add log instance --- ryan/utils/messages.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ryan/utils/messages.py b/ryan/utils/messages.py index 190d9a1..8bfc64a 100644 --- a/ryan/utils/messages.py +++ b/ryan/utils/messages.py @@ -1,3 +1,4 @@ +import logging import random from datetime import datetime @@ -22,6 +23,8 @@ "I hold my head", ) +log = logging.getLogger(__name__) + def msg_success(message: str) -> discord.Embed: """Create a success embed with `message`.""" From 1fe39b6df4f8727c4f2aa056a3dcd96978ae3283 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 13 Feb 2021 19:29:41 +0100 Subject: [PATCH 4/7] Utils: add attachment download helper function --- ryan/utils/messages.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ryan/utils/messages.py b/ryan/utils/messages.py index 8bfc64a..2300645 100644 --- a/ryan/utils/messages.py +++ b/ryan/utils/messages.py @@ -1,5 +1,6 @@ import logging import random +import typing as t from datetime import datetime import discord @@ -46,6 +47,19 @@ def msg_error(message: str) -> discord.Embed: return embed +async def download_file(attachment: discord.Attachment) -> t.Optional[discord.File]: + """ + Download & return `attachment` file. + + If the download fails, the reason is logged and None will be returned. + """ + log.debug(f"Attempting to download attachment: {attachment.url}") + try: + return await attachment.to_file() + except discord.HTTPException as http_exc: + log.warning("Failed to download attachment!", exc_info=http_exc) + + async def relay_message(message: discord.Message, target: discord.TextChannel) -> None: """Relays a quote embed to the `target` channel.""" author = message.author From eb27ceae09578e76926d68a386e89dcba61f40b4 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 13 Feb 2021 19:43:58 +0100 Subject: [PATCH 5/7] Utils: improve attachment relay logic Now the first attachment will actually get relayed with the quotation embed. The previous version was (incorrectly) attempting to only relay the CDN URLs. --- ryan/utils/messages.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/ryan/utils/messages.py b/ryan/utils/messages.py index 2300645..b84c7b9 100644 --- a/ryan/utils/messages.py +++ b/ryan/utils/messages.py @@ -61,7 +61,12 @@ async def download_file(attachment: discord.Attachment) -> t.Optional[discord.Fi async def relay_message(message: discord.Message, target: discord.TextChannel) -> None: - """Relays a quote embed to the `target` channel.""" + """ + Relays an embed quoting `message` to the `target` channel. + + If the `message` contains attachments, the first one will be sent with the quotation. + User-sent messages do not normally contain multiple attachments, so this case is ignored. + """ author = message.author if author.display_name != author.name: name = f"{author.display_name} ({author.name})" @@ -77,9 +82,10 @@ async def relay_message(message: discord.Message, target: discord.TextChannel) - text=f"{datetime.now()} - {message.guild.name} - {message.channel.name}" ) if message.attachments: - quote_embed.add_field( - name="Attachments:", - value="\n".join(message.attachments) - ) + attachment = message.attachments[0] # We will only relay the first attachment + if (att_file := await download_file(attachment)) is not None: + quote_embed.set_image(url=f"attachment://{attachment.filename}") # Embed displays the attached file + else: + att_file = None - await target.send(embed=quote_embed) + await target.send(embed=quote_embed, file=att_file) From 569411263b3a2fd7dfd275a264179ddc3c61d65f Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 13 Feb 2021 19:49:21 +0100 Subject: [PATCH 6/7] Utils: improve message relay code style & add debug logging --- ryan/utils/messages.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/ryan/utils/messages.py b/ryan/utils/messages.py index b84c7b9..b740aa6 100644 --- a/ryan/utils/messages.py +++ b/ryan/utils/messages.py @@ -67,25 +67,27 @@ async def relay_message(message: discord.Message, target: discord.TextChannel) - If the `message` contains attachments, the first one will be sent with the quotation. User-sent messages do not normally contain multiple attachments, so this case is ignored. """ - author = message.author + author = message.author # For short since we'll access this a lot + log.debug(f"Building quotation embed for message from: {author}") + if author.display_name != author.name: name = f"{author.display_name} ({author.name})" else: name = author.name quote_embed = discord.Embed(description=message.content) - quote_embed.set_author( - name=name, - icon_url=author.avatar_url - ) - quote_embed.set_footer( - text=f"{datetime.now()} - {message.guild.name} - {message.channel.name}" - ) + quote_embed.set_author(name=name, icon_url=author.avatar_url) + quote_embed.set_footer(text=f"{datetime.now()} - {message.guild.name} - {message.channel.name}") + if message.attachments: + log.debug(f"Relaying first of {len(message.attachments)} attachments") attachment = message.attachments[0] # We will only relay the first attachment + if (att_file := await download_file(attachment)) is not None: quote_embed.set_image(url=f"attachment://{attachment.filename}") # Embed displays the attached file else: + log.debug("Message contains no attachments") att_file = None + log.debug("Dispatching quotation embed") await target.send(embed=quote_embed, file=att_file) From 5f64be8a8ea92e0eece438ecfb57f5aef36673c0 Mon Sep 17 00:00:00 2001 From: kwzrd Date: Sat, 13 Feb 2021 19:56:45 +0100 Subject: [PATCH 7/7] Utils: use proper timestamp fields in quotation embeds --- ryan/utils/messages.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ryan/utils/messages.py b/ryan/utils/messages.py index b740aa6..38e3504 100644 --- a/ryan/utils/messages.py +++ b/ryan/utils/messages.py @@ -75,9 +75,9 @@ async def relay_message(message: discord.Message, target: discord.TextChannel) - else: name = author.name - quote_embed = discord.Embed(description=message.content) + quote_embed = discord.Embed(description=message.content, timestamp=datetime.utcnow()) quote_embed.set_author(name=name, icon_url=author.avatar_url) - quote_embed.set_footer(text=f"{datetime.now()} - {message.guild.name} - {message.channel.name}") + quote_embed.set_footer(text=f"{message.guild.name} | {message.channel.name}") if message.attachments: log.debug(f"Relaying first of {len(message.attachments)} attachments")