diff options
author | 2022-11-05 14:15:11 +0000 | |
---|---|---|
committer | 2022-11-05 14:15:11 +0000 | |
commit | 3f55e7149a3197b7fa41fcf7dc7df47a3a209cfd (patch) | |
tree | 11d77db3a48d8226bc1cd14cb18a21d2423b6352 /pydis_core/utils/commands.py | |
parent | Use New Static Build Site API (#122) (diff) | |
parent | Add six as a dev dep (diff) |
Merge pull request #157 from python-discord/prepare-for-pypi-releasev9.0.0
Prepare for pypi release
Diffstat (limited to 'pydis_core/utils/commands.py')
-rw-r--r-- | pydis_core/utils/commands.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/pydis_core/utils/commands.py b/pydis_core/utils/commands.py new file mode 100644 index 00000000..7afd8137 --- /dev/null +++ b/pydis_core/utils/commands.py @@ -0,0 +1,38 @@ +from typing import Optional + +from discord import Message +from discord.ext.commands import BadArgument, Context, clean_content + + +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 / reply message content. + + Returns: + The cleaned version of `text`, if given, else replied message. + """ + 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 + ): + 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.") |