Skip to content

Commit c168781

Browse files
authored
Merge pull request #256 from Dorukyum/restructure
Implement custom converter types
2 parents c61f348 + fc43b56 commit c168781

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

discord/commands/commands.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def __eq__(self, other) -> bool:
392392
)
393393

394394
async def _invoke(self, ctx: ApplicationContext) -> None:
395-
# TODO: Parse the args better, apply custom converters etc.
395+
# TODO: Parse the args better
396396
kwargs = {}
397397
for arg in ctx.interaction.data.get("options", []):
398398
op = find(lambda x: x.name == arg["name"], self.options)
@@ -413,6 +413,9 @@ async def _invoke(self, ctx: ApplicationContext) -> None:
413413
if arg is None:
414414
arg = ctx.guild.get_role(arg_id) or arg_id
415415

416+
elif op.input_type == SlashCommandOptionType.string and op._converter is not None:
417+
arg = await op._converter.convert(ctx, arg)
418+
416419
kwargs[op.name] = arg
417420

418421
for o in self.options:
@@ -470,8 +473,13 @@ def __init__(
470473
) -> None:
471474
self.name: Optional[str] = kwargs.pop("name", None)
472475
self.description = description or "No description provided"
476+
self._converter = None
473477
if not isinstance(input_type, SlashCommandOptionType):
474-
input_type = SlashCommandOptionType.from_datatype(input_type)
478+
to_assign = input_type() if isinstance(input_type, type) else input_type
479+
_type = SlashCommandOptionType.from_datatype(to_assign.__class__)
480+
if _type == SlashCommandOptionType.custom:
481+
self._converter = to_assign
482+
input_type = SlashCommandOptionType.string
475483
self.input_type = input_type
476484
self.required: bool = kwargs.pop("required", True)
477485
self.choices: List[OptionChoice] = [

discord/enums.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,9 @@ def from_datatype(cls, datatype):
612612
if issubclass(datatype, float):
613613
return cls.number
614614

615+
if hasattr(datatype, "convert"):
616+
return cls.custom
617+
615618
if datatype.__name__ == "Member":
616619
return cls.user
617620
if datatype.__name__ == "GuildChannel":

0 commit comments

Comments
 (0)