blob: a99b38523008fb03efe624aa6046fc1597b5f88f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
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.
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
):
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
raise BadArgument("Couldn't find text to clean. Provide a string or reply to a message to use its content.")
|