Skip to content

Commit

Permalink
Create transformers.py for app command transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
Mega-JC committed Jun 6, 2024
1 parent f0136c2 commit 32832fe
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions commands/transformers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""This file is a part of the source code for snakecore.
This project has been licensed under the MIT license.
Copyright (c) 2024-present pygame-community.
This file defines some transformers for application command argument parsing.
"""

import discord
from discord import app_commands
from discord.ext import commands

_DECBotT = commands.Bot | commands.AutoShardedBot


class MessageTransformer(app_commands.Transformer):
"Transforms to a :class:`discord.Message`."

async def transform(
self, interaction: discord.Interaction[_DECBotT], value: str
) -> discord.Message:
try:
return await commands.MessageConverter().convert(
await commands.Context.from_interaction(interaction), value
)
except commands.BadArgument:
raise app_commands.TransformerError(
value, discord.AppCommandOptionType.string, self
)


Message = app_commands.Transform[discord.Message, MessageTransformer]
"Transforms to a :class:`discord.Message`."

__all__ = ("MessageTransformer", "Message")

0 comments on commit 32832fe

Please sign in to comment.