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

Add new interaction channel attribute #1621

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/1621.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add new `BaseInteraction.channel` attribute
4 changes: 2 additions & 2 deletions hikari/impl/entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2542,7 +2542,7 @@ def deserialize_command_interaction(
guild_id=guild_id,
guild_locale=locales.Locale(payload["guild_locale"]) if "guild_locale" in payload else None,
locale=locales.Locale(payload["locale"]),
channel_id=snowflakes.Snowflake(payload["channel_id"]),
channel=self.deserialize_partial_channel(payload["channel"]),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the channel objects here include a lot more fields than just the partial channel's fields

e.g.

{
    "type": 0,
    "topic": null,
    "theme_color": null,
    "rate_limit_per_user": 0,
    "position": 20,
    "permissions": "140737488355327",
    "parent_id": "561885448721399808",
    "nsfw": false,
    "name": "dwads",
    "last_message_id": "1114922806820479057",
    "id": "717586538925523004",
    "icon_emoji": {
        "name": "\ud83e\udd16",
        "id": null
    },
    "guild_id": "561884984214814744",
    "flags": 0,
    "default_auto_archive_duration": 60
}
{
    "type": 1,
    "last_message_id": "1096465893028003910",
    "id": "463185043796262922",
    "flags": 0
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was also mentioned in the PR which documented this field discord/discord-api-docs#6051 (comment), just another case of Discord having shit docs tbh

member=member,
user=user,
token=payload["token"],
Expand Down Expand Up @@ -2584,7 +2584,7 @@ def deserialize_autocomplete_interaction(
id=snowflakes.Snowflake(payload["id"]),
type=base_interactions.InteractionType(payload["type"]),
guild_id=guild_id,
channel_id=snowflakes.Snowflake(payload["channel_id"]),
channel=self.deserialize_partial_channel(payload["channel"]),
member=member,
user=user,
token=payload["token"],
Expand Down
8 changes: 6 additions & 2 deletions hikari/interactions/command_interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ class BaseCommandInteraction(base_interactions.PartialInteraction):
May be a command interaction or an autocomplete interaction.
"""

channel_id: snowflakes.Snowflake = attrs.field(eq=False, hash=False, repr=True)
"""ID of the channel this command interaction event was triggered in."""
channel: channels.PartialChannel = attrs.field(eq=False, hash=False, repr=False)
"""The channel this command interaction event was triggered in."""

guild_id: typing.Optional[snowflakes.Snowflake] = attrs.field(eq=False, hash=False, repr=True)
"""ID of the guild this command interaction event was triggered in.
Expand Down Expand Up @@ -178,6 +178,10 @@ class BaseCommandInteraction(base_interactions.PartialInteraction):
command_type: typing.Union[commands.CommandType, int] = attrs.field(eq=False, hash=False, repr=True)
"""The type of the command."""

@property
def channel_id(self) -> snowflakes.Snowflake:
return self.channel.id

async def fetch_channel(self) -> channels.TextableChannel:
"""Fetch the guild channel this was triggered in.

Expand Down
24 changes: 16 additions & 8 deletions tests/hikari/impl/test_entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4348,7 +4348,9 @@ def interaction_resolved_data_payload(
}

@pytest.fixture()
def command_interaction_payload(self, interaction_member_payload, interaction_resolved_data_payload):
def command_interaction_payload(
self, interaction_member_payload, interaction_resolved_data_payload, guild_text_channel_payload
):
return {
"id": "3490190239012093",
"type": 2,
Expand All @@ -4369,7 +4371,7 @@ def command_interaction_payload(self, interaction_member_payload, interaction_re
],
"resolved": interaction_resolved_data_payload,
},
"channel_id": "49949494",
"channel": guild_text_channel_payload,
"member": interaction_member_payload,
"token": "moe cat girls",
"locale": "es-ES",
Expand All @@ -4386,6 +4388,7 @@ def test_deserialize_command_interaction(
command_interaction_payload,
interaction_member_payload,
interaction_resolved_data_payload,
guild_text_channel_payload,
):
interaction = entity_factory_impl.deserialize_command_interaction(command_interaction_payload)
assert interaction.app is mock_app
Expand All @@ -4394,7 +4397,7 @@ def test_deserialize_command_interaction(
assert interaction.type is base_interactions.InteractionType.APPLICATION_COMMAND
assert interaction.token == "moe cat girls"
assert interaction.version == 69420
assert interaction.channel_id == 49949494
assert interaction.channel == entity_factory_impl.deserialize_partial_channel(guild_text_channel_payload)
assert interaction.guild_id == 43123123
assert interaction.locale == "es-ES"
assert interaction.locale is locales.Locale.ES_ES
Expand Down Expand Up @@ -4438,7 +4441,9 @@ def test_deserialize_command_interaction(
assert isinstance(interaction, command_interactions.CommandInteraction)

@pytest.fixture()
def context_menu_command_interaction_payload(self, interaction_member_payload, user_payload):
def context_menu_command_interaction_payload(
self, interaction_member_payload, user_payload, guild_text_channel_payload
):
return {
"id": "3490190239012093",
"type": 4,
Expand All @@ -4450,7 +4455,7 @@ def context_menu_command_interaction_payload(self, interaction_member_payload, u
"target_id": "115590097100865541",
"resolved": {"users": {"115590097100865541": user_payload}},
},
"channel_id": "49949494",
"channel": guild_text_channel_payload,
"member": interaction_member_payload,
"token": "moe cat girls",
"locale": "es-ES",
Expand Down Expand Up @@ -4489,7 +4494,9 @@ def test_deserialize_command_interaction_with_null_attributes(
assert interaction.app_permissions is None

@pytest.fixture()
def autocomplete_interaction_payload(self, member_payload, user_payload, interaction_resolved_data_payload):
def autocomplete_interaction_payload(
self, member_payload, user_payload, interaction_resolved_data_payload, guild_text_channel_payload
):
return {
"id": "3490190239012093",
"type": 4,
Expand All @@ -4510,7 +4517,7 @@ def autocomplete_interaction_payload(self, member_payload, user_payload, interac
}
],
},
"channel_id": "49949494",
"channel": guild_text_channel_payload,
"user": user_payload,
"token": "moe cat girls",
"locale": "es-ES",
Expand All @@ -4526,6 +4533,7 @@ def test_deserialize_autocomplete_interaction(
member_payload,
autocomplete_interaction_payload,
interaction_resolved_data_payload,
guild_text_channel_payload,
):
entity_factory_impl._deserialize_interaction_member = mock.Mock()
entity_factory_impl._deserialize_resolved_option_data = mock.Mock()
Expand All @@ -4537,7 +4545,7 @@ def test_deserialize_autocomplete_interaction(
assert interaction.type is base_interactions.InteractionType.AUTOCOMPLETE
assert interaction.token == "moe cat girls"
assert interaction.version == 69420
assert interaction.channel_id == 49949494
assert interaction.channel == entity_factory_impl.deserialize_partial_channel(guild_text_channel_payload)
assert interaction.guild_id == 43123123
assert interaction.member is entity_factory_impl._deserialize_interaction_member.return_value
entity_factory_impl._deserialize_interaction_member.assert_called_once_with(member_payload, guild_id=43123123)
Expand Down
7 changes: 5 additions & 2 deletions tests/hikari/interactions/test_command_interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def mock_command_interaction(self, mock_app):
app=mock_app,
id=snowflakes.Snowflake(2312312),
type=base_interactions.InteractionType.APPLICATION_COMMAND,
channel_id=snowflakes.Snowflake(3123123),
channel=mock.Mock(id=3123123),
guild_id=snowflakes.Snowflake(5412231),
member=object(),
user=object(),
Expand Down Expand Up @@ -75,6 +75,9 @@ def test_build_deferred_response(self, mock_command_interaction, mock_app):
base_interactions.ResponseType.DEFERRED_MESSAGE_CREATE
)

def test_channel_id_property(self, mock_command_interaction):
assert mock_command_interaction.channel_id == 3123123

@pytest.mark.asyncio()
async def test_fetch_channel(self, mock_command_interaction, mock_app):
mock_app.rest.fetch_channel.return_value = mock.Mock(channels.TextableGuildChannel)
Expand Down Expand Up @@ -107,7 +110,7 @@ def mock_autocomplete_interaction(self, mock_app):
app=mock_app,
id=snowflakes.Snowflake(2312312),
type=base_interactions.InteractionType.APPLICATION_COMMAND,
channel_id=snowflakes.Snowflake(3123123),
channel=mock.Mock(3123123),
guild_id=snowflakes.Snowflake(5412231),
guild_locale="en-US",
locale="en-US",
Expand Down