From 65410a9c11d70cadd8a6d16ffc386a7cad3d1f0b Mon Sep 17 00:00:00 2001 From: Izan Date: Thu, 14 Jul 2022 21:41:32 +0100 Subject: Add `clean_text_or_reply` util. --- botcore/utils/commands.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 botcore/utils/commands.py (limited to 'botcore/utils/commands.py') diff --git a/botcore/utils/commands.py b/botcore/utils/commands.py new file mode 100644 index 00000000..2d380bef --- /dev/null +++ b/botcore/utils/commands.py @@ -0,0 +1,21 @@ +from typing import Optional + +from discord import Message +from discord.ext.commands import Context, clean_content + + +async def clean_text_or_reply(ctx: Context, text: Optional[str] = None) -> Optional[str]: + """Returns cleaned version of `text`, if given, else referenced message, if found, else `None`.""" + clean_content_converter = clean_content(fix_channel_mentions=True) + + if text: + return await clean_content_converter.convert(ctx, text) + + if ( + (replied_message := getattr(ctx.message.reference, "resolved", None)) # message has a cached reference + and isinstance(replied_message, Message) # referenced message hasn't been deleted + ): + return await clean_content_converter.convert(ctx, ctx.message.reference.resolved.content) + + # No text provided, and either no message was referenced or we can't access the content + return None -- cgit v1.2.3 From 85063ab4cd2ad6a7d6621645c049d0917affbfbe Mon Sep 17 00:00:00 2001 From: Izan Date: Sat, 23 Jul 2022 14:22:30 +0100 Subject: Reformat docstring to use Google's style & raise error instead of returning None --- botcore/utils/commands.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'botcore/utils/commands.py') diff --git a/botcore/utils/commands.py b/botcore/utils/commands.py index 2d380bef..a99b3852 100644 --- a/botcore/utils/commands.py +++ b/botcore/utils/commands.py @@ -1,11 +1,24 @@ from typing import Optional from discord import Message -from discord.ext.commands import Context, clean_content +from discord.ext.commands import BadArgument, Context, clean_content -async def clean_text_or_reply(ctx: Context, text: Optional[str] = None) -> Optional[str]: - """Returns cleaned version of `text`, if given, else referenced message, if found, else `None`.""" +async def clean_text_or_reply(ctx: Context, text: Optional[str] = None) -> str: + """ + Cleans a text argument or replied message's content. + + Args: + ctx: The command's context + text: The provided text argument of the command (if given) + + Raises: + :exc:`discord.ext.commands.BadArgument` + `text` wasn't provided and there's no reply message. + + Returns: + The cleaned version of `text`, if given, else replied message. + """ clean_content_converter = clean_content(fix_channel_mentions=True) if text: @@ -18,4 +31,4 @@ async def clean_text_or_reply(ctx: Context, text: Optional[str] = None) -> Optio return await clean_content_converter.convert(ctx, ctx.message.reference.resolved.content) # No text provided, and either no message was referenced or we can't access the content - return None + raise BadArgument("Couldn't find text to clean. Provide a string or reply to a message to use its content.") -- cgit v1.2.3 From 4e43f3c4705c8c26cb2b4c5ca01e1f2885766c80 Mon Sep 17 00:00:00 2001 From: Izan Date: Sat, 23 Jul 2022 14:25:45 +0100 Subject: Raise error when referenced message has no content --- botcore/utils/commands.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'botcore/utils/commands.py') diff --git a/botcore/utils/commands.py b/botcore/utils/commands.py index a99b3852..7afd8137 100644 --- a/botcore/utils/commands.py +++ b/botcore/utils/commands.py @@ -14,7 +14,7 @@ async def clean_text_or_reply(ctx: Context, text: Optional[str] = None) -> str: Raises: :exc:`discord.ext.commands.BadArgument` - `text` wasn't provided and there's no reply message. + `text` wasn't provided and there's no reply message / reply message content. Returns: The cleaned version of `text`, if given, else replied message. @@ -28,7 +28,11 @@ async def clean_text_or_reply(ctx: Context, text: Optional[str] = None) -> str: (replied_message := getattr(ctx.message.reference, "resolved", None)) # message has a cached reference and isinstance(replied_message, Message) # referenced message hasn't been deleted ): - return await clean_content_converter.convert(ctx, ctx.message.reference.resolved.content) + if not (content := ctx.message.reference.resolved.content): + # The referenced message doesn't have a content (e.g. embed/image), so raise error + raise BadArgument("The referenced message doesn't have a text content.") + + return await clean_content_converter.convert(ctx, content) # No text provided, and either no message was referenced or we can't access the content raise BadArgument("Couldn't find text to clean. Provide a string or reply to a message to use its content.") -- cgit v1.2.3