Skip to content

Commit

Permalink
Further improve ReferencedMessageConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
Mega-JC committed Jun 6, 2024
1 parent 3138ae5 commit 75aab47
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions snakecore/commands/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1201,10 +1201,22 @@ async def convert(self, ctx: commands.Context[_BotT], argument: str) -> str:


class ReferencedMessageConverter(commands.Converter[discord.Message]):
"""A converter that retrieves a message referenced (replied to) by a command invocation
text message.
This converter doesn't attempt to parse any text inputs and passes them on to
the next converter.
NOTE: Due to the way discord.py parses text commands, this converter will not
be called upon if a command invocation uses a message reference as the sole argument,
without any other text arguments. Instead, ``discord.MissingRequiredArgument``
will be raised.
To work around this, you must at least specify one character (e.g. '.')
to be 'consumed' by this converter, even though that character wouldn't be parsed.
"""
async def convert(
self, ctx: commands.Context[_BotT], argument: str
) -> discord.Message:

if not ctx.message.reference:
raise commands.UserInputError(
"The target message does not reference any other message."
Expand Down Expand Up @@ -1233,7 +1245,8 @@ async def convert(
else:
raise commands.UserInputError("Failed to retrieve the referenced message.")

ctx.view.undo() # backtrack for next converter to continue from here
if not ctx.view.eof: # don't backtrack if no other converters follow
ctx.view.undo() # backtrack to pass on argument to next converter

return message

Expand Down Expand Up @@ -1298,7 +1311,15 @@ async def convert(
"""A converter that retrieves a message referenced (replied to) by a command invocation
text message.
Does not actually consume command arguments.
This converter doesn't attempt to parse any text inputs and passes them on to
the next converter.
NOTE: Due to the way discord.py parses text commands, this converter will not
be called upon if a command invocation uses a message reference as the sole argument,
without any other text arguments. Instead, ``discord.MissingRequiredArgument``
will be raised.
To work around this, you must at least specify one character (e.g. '.')
to be 'consumed' by this converter, even though that character wouldn't be parsed.
"""


Expand Down

0 comments on commit 75aab47

Please sign in to comment.