Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: option and BridgeOption adjustments #2417

Merged
merged 6 commits into from Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
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
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
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",
)