Skip to content

Commit

Permalink
Add new channel_types field to CommandOption (#800)
Browse files Browse the repository at this point in the history
* Add new channel_types field to CommandOption

* Add changelog fragment

* Add tests

* Fix wording in docstring

Co-authored-by: Lucina <lucina@lmbyrne.dev>

Co-authored-by: Lucina <lucina@lmbyrne.dev>
  • Loading branch information
davfsa and FasterSpeeding authored Sep 29, 2021
1 parent 39cf4c2 commit dc6a4d6
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Bugfixes
- `is_webhook` will now return `undefined.UNDEFINED` if the information is not available
- Fix logic in `is_human` to account for the changes in the typing
- Set `PartialMessage.member` to `undefined.UNDEFINED` when Discord edit the message to display an embed/attachment ([#783](https://github.com/hikari-py/hikari/issues/783))
- `CommandOption.value` will now be cast to a `Snowflake` for types 6-9 ([#785](https://github.com/hikari-py/hikari/issues/785))
- `CommandInteractionOption.value` will now be cast to a `Snowflake` for types 6-9 ([#785](https://github.com/hikari-py/hikari/issues/785))


Improved Documentation
Expand Down
1 change: 1 addition & 0 deletions changes/800.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support new `channel_types` field in `CommandOption`
9 changes: 9 additions & 0 deletions hikari/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from hikari.internal import enums

if typing.TYPE_CHECKING:
from hikari import channels
from hikari import guilds


Expand Down Expand Up @@ -136,6 +137,14 @@ class CommandOption:
options: typing.Optional[typing.Sequence[CommandOption]] = attr.field(default=None, repr=False)
"""Sequence of up to (and including) 25 of the options for this command option."""

channel_types: typing.Optional[typing.Sequence[typing.Union[channels.ChannelType, int]]] = attr.field(
default=None, repr=False
)
"""The channel types that this option will accept.
If `builtins.None`, then all channel types will be accepted.
"""


@attr_extensions.with_copy
@attr.define(hash=True, kw_only=True, weakref_slot=False)
Expand Down
8 changes: 8 additions & 0 deletions hikari/impl/entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -1669,13 +1669,18 @@ def _deserialize_command_option(self, payload: data_binding.JSONObject) -> comma
if raw_options := payload.get("options"):
suboptions = [self._deserialize_command_option(option) for option in raw_options]

channel_types: typing.Optional[typing.Sequence[typing.Union[channel_models.ChannelType, int]]] = None
if raw_channel_types := payload.get("channel_types"):
channel_types = [channel_models.ChannelType(channel_type) for channel_type in raw_channel_types]

return commands.CommandOption(
type=commands.OptionType(payload["type"]),
name=payload["name"],
description=payload["description"],
is_required=payload.get("required", False),
choices=choices,
options=suboptions,
channel_types=channel_types,
)

def deserialize_command(
Expand Down Expand Up @@ -1889,6 +1894,9 @@ def serialize_command_option(self, option: commands.CommandOption) -> data_bindi
"required": option.is_required,
}

if option.channel_types is not None:
payload["channel_types"] = option.channel_types

if option.choices is not None:
payload["choices"] = [{"name": choice.name, "value": choice.value} for choice in option.choices]

Expand Down
4 changes: 2 additions & 2 deletions hikari/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ class ButtonComponent(PartialComponent):
"""Custom or unicode emoji which appears on the button."""

custom_id: typing.Optional[str] = attr.field(hash=True)
"""Developer defined identifier for this button (will be >= 100 characters).
"""Developer defined identifier for this button (will be <= 100 characters).
!!! note
This is required for the following button styles:
Expand Down Expand Up @@ -656,7 +656,7 @@ class SelectMenuComponent(PartialComponent):
"""

custom_id: str = attr.field(hash=True)
"""Developer defined identifier for this menu (will be >= 100 characters)."""
"""Developer defined identifier for this menu (will be <= 100 characters)."""

options: typing.Sequence[SelectMenuOption] = attr.field(eq=False)
"""Sequence of up to 25 of the options set for this menu."""
Expand Down
19 changes: 19 additions & 0 deletions tests/hikari/impl/test_entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3091,6 +3091,25 @@ def test_deserialize_interaction_handles_unknown_type(self, entity_factory_impl)
with pytest.raises(errors.UnrecognisedEntityError):
entity_factory_impl.deserialize_interaction({"type": -999})

def test_serialize_command_option_with_channel_type(self, entity_factory_impl):
option = commands.CommandOption(
type=commands.OptionType.INTEGER,
name="a name",
description="go away",
is_required=True,
channel_types=[channel_models.ChannelType.GUILD_STAGE, channel_models.ChannelType.GUILD_TEXT, 100],
)

result = entity_factory_impl.serialize_command_option(option)

assert result == {
"type": 4,
"name": "a name",
"description": "go away",
"required": True,
"channel_types": [13, 0, 100],
}

def test_serialize_command_option_with_choices(self, entity_factory_impl):
option = commands.CommandOption(
type=commands.OptionType.INTEGER,
Expand Down

0 comments on commit dc6a4d6

Please sign in to comment.