Skip to content

Commit

Permalink
fix: option and BridgeOption adjustments (Pycord-Development#2417)
Browse files Browse the repository at this point in the history
  • Loading branch information
NeloBlivion authored Apr 9, 2024
1 parent 8cec636 commit bb61cf7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ These changes are available on the `master` branch, but have not yet been releas
([#2396](https://github.com/Pycord-Development/pycord/pull/2396))
- Added `user` argument to `Paginator.edit`.
([#2390](https://github.com/Pycord-Development/pycord/pull/2390))
- Added `bridge_option` decorator. Required for `bridge.Bot` in 2.7.
([#2417](https://github.com/Pycord-Development/pycord/pull/2417))

### Fixed

Expand All @@ -34,13 +36,19 @@ These changes are available on the `master` branch, but have not yet been releas
([#2407](https://github.com/Pycord-Development/pycord/pull/2407))
- Fixed invalid data being passed to `Interaction._guild` in certain cases.
([#2411](https://github.com/Pycord-Development/pycord/pull/2411))
- Fixed option typehints being ignored when using `parameter_name`.
([#2417](https://github.com/Pycord-Development/pycord/pull/2417))

### Changed

- Changed the type of `Guild.bitrate_limit` to `int`.
([#2387](https://github.com/Pycord-Development/pycord/pull/2387))
- HTTP requests that fail with a 503 status are now re-tried.
([#2395](https://github.com/Pycord-Development/pycord/pull/2395))
- `option` decorator now accepts `input_type`.
([#2417](https://github.com/Pycord-Development/pycord/pull/2417))
- `Option` may be used instead of `BridgeOption` until 2.7.
([#2417](https://github.com/Pycord-Development/pycord/pull/2417))

## [2.5.0] - 2024-03-02

Expand Down
15 changes: 8 additions & 7 deletions discord/commands/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def to_dict(self) -> dict[str, str | int | float]:
return as_dict


def option(name, type=None, **kwargs):
def option(name, input_type=None, **kwargs):
"""A decorator that can be used instead of typehinting :class:`.Option`.
.. versionadded:: 2.0
Expand All @@ -408,12 +408,13 @@ def option(name, type=None, **kwargs):
"""

def decorator(func):
nonlocal type
type = type or func.__annotations__.get(name, str)
if parameter := kwargs.get("parameter_name"):
func.__annotations__[parameter] = Option(type, name=name, **kwargs)
else:
func.__annotations__[name] = Option(type, **kwargs)
resolved_name = kwargs.pop("parameter_name", None) or name
itype = (
kwargs.pop("type", None)
or input_type
or func.__annotations__.get(resolved_name, str)
)
func.__annotations__[resolved_name] = Option(itype, name=name, **kwargs)
return func

return decorator
38 changes: 37 additions & 1 deletion discord/ext/bridge/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
SlashCommandOptionType,
)

from ...utils import MISSING, find, get
from ...utils import MISSING, find, get, warn_deprecated
from ..commands import BadArgument
from ..commands import Bot as ExtBot
from ..commands import (
Expand All @@ -63,6 +63,7 @@
"BridgeCommandGroup",
"bridge_command",
"bridge_group",
"bridge_option",
"BridgeExtCommand",
"BridgeSlashCommand",
"BridgeExtGroup",
Expand Down Expand Up @@ -627,3 +628,38 @@ async def convert(self, ctx, argument: str) -> Any:
return converted
except ValueError as exc:
raise BadArgument() from exc


def bridge_option(name, input_type=None, **kwargs):
"""A decorator that can be used instead of typehinting :class:`.BridgeOption`.
.. versionadded:: 2.6
Attributes
----------
parameter_name: :class:`str`
The name of the target parameter this option is mapped to.
This allows you to have a separate UI ``name`` and parameter name.
"""

def decorator(func):
resolved_name = kwargs.pop("parameter_name", None) or name
itype = (
kwargs.pop("type", None)
or input_type
or func.__annotations__.get(resolved_name, str)
)
func.__annotations__[resolved_name] = BridgeOption(itype, name=name, **kwargs)
return func

return decorator


discord.commands.options.Option = BridgeOption
discord.Option = BridgeOption
warn_deprecated(
"Option",
"BridgeOption",
"2.5",
reference="https://github.com/Pycord-Development/pycord/pull/2417",
)

0 comments on commit bb61cf7

Please sign in to comment.