Merge branch 'iDevision:2.0' into 2.0

This commit is contained in:
Sengolda 2021-10-18 12:08:40 +05:30 committed by GitHub
commit 387f1bb915
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 8 deletions

View File

@ -1032,6 +1032,8 @@ class Option(Generic[T, DT]): # type: ignore
The default for this option, overwrites Option during parsing. The default for this option, overwrites Option during parsing.
description: :class:`str` description: :class:`str`
The description for this option, is unpacked to :attr:`.Command.option_descriptions` The description for this option, is unpacked to :attr:`.Command.option_descriptions`
name: :class:`str`
The name of the option. This defaults to the parameter name.
""" """
description: DT description: DT
@ -1039,16 +1041,20 @@ class Option(Generic[T, DT]): # type: ignore
__slots__ = ( __slots__ = (
"default", "default",
"description", "description",
"name",
) )
def __init__(self, default: T = inspect.Parameter.empty, *, description: DT) -> None: def __init__(
self, default: T = inspect.Parameter.empty, *, description: DT, name: str = discord.utils.MISSING
) -> None:
self.description = description self.description = description
self.default = default self.default = default
self.name: str = name
if TYPE_CHECKING: if TYPE_CHECKING:
# Terrible workaround for type checking reasons # Terrible workaround for type checking reasons
def Option(default: T = inspect.Parameter.empty, *, description: str) -> T: def Option(default: T = inspect.Parameter.empty, *, description: str, name: str = discord.utils.MISSING) -> T:
... ...

View File

@ -174,8 +174,12 @@ def get_signature_parameters(
annotation = parameter.annotation annotation = parameter.annotation
if isinstance(parameter.default, Option): # type: ignore if isinstance(parameter.default, Option): # type: ignore
option = parameter.default option = parameter.default
descriptions[name] = option.description
parameter = parameter.replace(default=option.default) parameter = parameter.replace(default=option.default)
if option.name is not MISSING:
name = option.name
parameter.replace(name=name)
descriptions[name] = option.description
if annotation is parameter.empty: if annotation is parameter.empty:
params[name] = parameter params[name] = parameter
@ -1234,15 +1238,25 @@ class Command(_BaseCommand, Generic[CogT, P, T]):
ctx.command = original ctx.command = original
def _param_to_options( def _param_to_options(
self, name: str, annotation: Any, required: bool, varadic: bool self, name: str, annotation: Any, required: bool, varadic: bool, description: Optional[str] = None
) -> List[Optional[ApplicationCommandInteractionDataOption]]: ) -> List[Optional[ApplicationCommandInteractionDataOption]]:
if description is not None:
self.option_descriptions[name] = description
description = self.option_descriptions[name]
origin = getattr(annotation, "__origin__", None) origin = getattr(annotation, "__origin__", None)
if inspect.isclass(annotation) and issubclass(annotation, FlagConverter): if inspect.isclass(annotation) and issubclass(annotation, FlagConverter):
return [ return [
param param
for name, flag in annotation.get_flags().items() for name, flag in annotation.get_flags().items()
for param in self._param_to_options( for param in self._param_to_options(
name, flag.annotation, required=flag.required, varadic=flag.annotation is tuple name,
flag.annotation,
required=flag.required,
varadic=flag.annotation is tuple,
description=flag.description if flag.description is not MISSING else None,
) )
] ]
@ -1258,7 +1272,7 @@ class Command(_BaseCommand, Generic[CogT, P, T]):
"type": 3, "type": 3,
"name": name, "name": name,
"required": required, "required": required,
"description": self.option_descriptions[name], "description": description,
} }
if origin is None: if origin is None:
@ -1284,7 +1298,11 @@ class Command(_BaseCommand, Generic[CogT, P, T]):
elif all([arg in application_option_channel_types for arg in annotation.__args__]): elif all([arg in application_option_channel_types for arg in annotation.__args__]):
option["type"] = 7 option["type"] = 7
option["channel_types"] = [discord_value for arg in annotation.__args__ for discord_value in application_option_channel_types[arg]] option["channel_types"] = [
discord_value
for arg in annotation.__args__
for discord_value in application_option_channel_types[arg]
]
elif origin is Literal: elif origin is Literal:
literal_values = annotation.__args__ literal_values = annotation.__args__

View File

@ -81,6 +81,8 @@ class Flag:
------------ ------------
name: :class:`str` name: :class:`str`
The name of the flag. The name of the flag.
description: :class:`str`
The description of the flag.
aliases: List[:class:`str`] aliases: List[:class:`str`]
The aliases of the flag name. The aliases of the flag name.
attribute: :class:`str` attribute: :class:`str`
@ -97,6 +99,7 @@ class Flag:
""" """
name: str = MISSING name: str = MISSING
description: str = MISSING
aliases: List[str] = field(default_factory=list) aliases: List[str] = field(default_factory=list)
attribute: str = MISSING attribute: str = MISSING
annotation: Any = MISSING annotation: Any = MISSING
@ -117,6 +120,7 @@ class Flag:
def flag( def flag(
*, *,
name: str = MISSING, name: str = MISSING,
description: str = MISSING,
aliases: List[str] = MISSING, aliases: List[str] = MISSING,
default: Any = MISSING, default: Any = MISSING,
max_args: int = MISSING, max_args: int = MISSING,
@ -129,6 +133,8 @@ def flag(
------------ ------------
name: :class:`str` name: :class:`str`
The flag name. If not given, defaults to the attribute name. The flag name. If not given, defaults to the attribute name.
description: :class:`str`
Description of the flag for the slash commands options. The default value is `'no description'`.
aliases: List[:class:`str`] aliases: List[:class:`str`]
Aliases to the flag name. If not given no aliases are set. Aliases to the flag name. If not given no aliases are set.
default: Any default: Any
@ -143,7 +149,9 @@ def flag(
Whether multiple given values overrides the previous value. The default Whether multiple given values overrides the previous value. The default
value depends on the annotation given. value depends on the annotation given.
""" """
return Flag(name=name, aliases=aliases, default=default, max_args=max_args, override=override) return Flag(
name=name, description=description, aliases=aliases, default=default, max_args=max_args, override=override
)
def validate_flag_name(name: str, forbidden: Set[str]): def validate_flag_name(name: str, forbidden: Set[str]):