Skip to content

Commit

Permalink
Add transformers.py for app command argument 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 571a0ed
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions snakecore/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .bot import *
from .converters import *
from .decorators import *
from .transformers import *


def init(global_client: discord.Client | None = None) -> None:
Expand Down
34 changes: 34 additions & 0 deletions snakecore/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 571a0ed

Please sign in to comment.