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