From c035a756bac9f2d4c24dc232bda3a6d46b0c8a0f Mon Sep 17 00:00:00 2001 From: wookie184 Date: Fri, 2 Oct 2020 19:52:23 +0100 Subject: Changed send_attachments so kwargs could be given and would be passed to send() --- bot/utils/messages.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/bot/utils/messages.py b/bot/utils/messages.py index d0b2342b3..c4ac1e360 100644 --- a/bot/utils/messages.py +++ b/bot/utils/messages.py @@ -56,15 +56,22 @@ async def wait_for_deletion( async def send_attachments( message: discord.Message, destination: Union[discord.TextChannel, discord.Webhook], - link_large: bool = True + link_large: bool = True, + **kwargs ) -> List[str]: """ Re-upload the message's attachments to the destination and return a list of their new URLs. Each attachment is sent as a separate message to more easily comply with the request/file size limit. If link_large is True, attachments which are too large are instead grouped into a single - embed which links to them. + embed which links to them. Extra kwargs will be passed to send() when sending the attachment. """ + webhook_send_kwargs = { + 'username': sub_clyde(message.author.display_name), + 'avatar_url': message.author.avatar_url, + } + webhook_send_kwargs.update(kwargs) + large = [] urls = [] for attachment in message.attachments: @@ -82,14 +89,10 @@ async def send_attachments( attachment_file = discord.File(file, filename=attachment.filename) if isinstance(destination, discord.TextChannel): - msg = await destination.send(file=attachment_file) + msg = await destination.send(file=attachment_file, **kwargs) urls.append(msg.attachments[0].url) else: - await destination.send( - file=attachment_file, - username=sub_clyde(message.author.display_name), - avatar_url=message.author.avatar_url - ) + await destination.send(file=attachment_file, **webhook_send_kwargs) elif link_large: large.append(attachment) else: @@ -106,13 +109,9 @@ async def send_attachments( embed.set_footer(text="Attachments exceed upload size limit.") if isinstance(destination, discord.TextChannel): - await destination.send(embed=embed) + await destination.send(embed=embed, **kwargs) else: - await destination.send( - embed=embed, - username=sub_clyde(message.author.display_name), - avatar_url=message.author.avatar_url - ) + await destination.send(embed=embed, **webhook_send_kwargs) return urls -- cgit v1.2.3 From f7015232947198f2a3d05c680df0da0bfaff4a8e Mon Sep 17 00:00:00 2001 From: wookie184 Date: Fri, 2 Oct 2020 19:55:06 +0100 Subject: Add use_cached argument to send_attachments, and change it to default to False --- bot/utils/messages.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/utils/messages.py b/bot/utils/messages.py index c4ac1e360..9fd571a20 100644 --- a/bot/utils/messages.py +++ b/bot/utils/messages.py @@ -57,6 +57,7 @@ async def send_attachments( message: discord.Message, destination: Union[discord.TextChannel, discord.Webhook], link_large: bool = True, + use_cached: bool = False, **kwargs ) -> List[str]: """ @@ -85,7 +86,7 @@ async def send_attachments( # but some may get through hence the try-catch. if attachment.size <= destination.guild.filesize_limit - 512: with BytesIO() as file: - await attachment.save(file, use_cached=True) + await attachment.save(file, use_cached=use_cached) attachment_file = discord.File(file, filename=attachment.filename) if isinstance(destination, discord.TextChannel): -- cgit v1.2.3 From 1481d8feaa4c155e13da2b1c5f9f9544d89e90c4 Mon Sep 17 00:00:00 2001 From: wookie184 Date: Fri, 2 Oct 2020 19:57:07 +0100 Subject: Changed dm_relay to include user id in webhook when sending attachments. --- bot/exts/moderation/dm_relay.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bot/exts/moderation/dm_relay.py b/bot/exts/moderation/dm_relay.py index 14263e004..4d5142b55 100644 --- a/bot/exts/moderation/dm_relay.py +++ b/bot/exts/moderation/dm_relay.py @@ -90,7 +90,11 @@ class DMRelay(Cog): # Handle any attachments if message.attachments: try: - await send_attachments(message, self.webhook) + await send_attachments( + message, + self.webhook, + username=f"{message.author.display_name} ({message.author.id})" + ) except (discord.errors.Forbidden, discord.errors.NotFound): e = discord.Embed( description=":x: **This message contained an attachment, but it could not be retrieved**", -- cgit v1.2.3 From 64b70160d63d28b1b2b2215cf484a825ca516160 Mon Sep 17 00:00:00 2001 From: wookie184 Date: Wed, 7 Oct 2020 20:06:06 +0100 Subject: made sure to use sub_clyde on username passed to send_attachments --- bot/utils/messages.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/utils/messages.py b/bot/utils/messages.py index 9fd571a20..b6c7cab50 100644 --- a/bot/utils/messages.py +++ b/bot/utils/messages.py @@ -68,10 +68,11 @@ async def send_attachments( embed which links to them. Extra kwargs will be passed to send() when sending the attachment. """ webhook_send_kwargs = { - 'username': sub_clyde(message.author.display_name), + 'username': message.author.display_name, 'avatar_url': message.author.avatar_url, } webhook_send_kwargs.update(kwargs) + webhook_send_kwargs['username'] = sub_clyde(webhook_send_kwargs['username']) large = [] urls = [] -- cgit v1.2.3