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 thread arguments to GuildChannel.edit + applied tags to edit_cannel #1474

Merged
merged 21 commits into from
Feb 5, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 2 additions & 0 deletions changes/1474.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Implement `GuildThreadChannel.edit`
- Update `RESTClient.edit_channnel` to support applied tags on forum threads
ZechCodes marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions hikari/api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ async def edit_channel(
locked: undefined.UndefinedOr[bool] = undefined.UNDEFINED,
invitable: undefined.UndefinedOr[bool] = undefined.UNDEFINED,
auto_archive_duration: undefined.UndefinedOr[time.Intervalish] = undefined.UNDEFINED,
applied_tags: undefined.UndefinedOr[typing.Sequence[channels_.ForumTag]] = undefined.UNDEFINED,
reason: undefined.UndefinedOr[str] = undefined.UNDEFINED,
) -> channels_.PartialChannel:
"""Edit a channel.
Expand Down Expand Up @@ -306,6 +307,10 @@ async def edit_channel(
This should be either 60, 1440, 4320 or 10080 seconds and, as of
writing, ignores the parent channel's set default_auto_archive_duration
when passed as `hikari.undefined.UNDEFINED`.
applied_tags : hikari.undefined.UndefinedOr[typing.Sequence[hikari.channels.ForumTag]]
If provided, the new tags applied to the thread.
ZechCodes marked this conversation as resolved.
Show resolved Hide resolved

This only applies to threads in a forum channel.
reason : hikari.undefined.UndefinedOr[str]
If provided, the reason that will be recorded in the audit logs.
Maximum of 512 characters.
Expand Down
84 changes: 84 additions & 0 deletions hikari/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,90 @@ class GuildThreadChannel(TextableGuildChannel):
Will be `None` for threads created before 2020-01-09.
"""

async def edit(
self,
*,
name: undefined.UndefinedOr[str] = undefined.UNDEFINED,
archived: undefined.UndefinedOr[bool] = undefined.UNDEFINED,
auto_archive_duration: undefined.UndefinedOr[time.Intervalish] = undefined.UNDEFINED,
locked: undefined.UndefinedOr[bool] = undefined.UNDEFINED,
invitable: undefined.UndefinedOr[bool] = undefined.UNDEFINED,
rate_limit_per_user: undefined.UndefinedOr[time.Intervalish] = undefined.UNDEFINED,
flags: undefined.UndefinedOr[ChannelFlag] = undefined.UNDEFINED,
applied_tags: undefined.UndefinedOr[typing.Sequence[ForumTag]] = undefined.UNDEFINED,
reason: undefined.UndefinedOr[str] = undefined.UNDEFINED,
) -> PartialChannel:
ZechCodes marked this conversation as resolved.
Show resolved Hide resolved
"""Edit the guild thread.

Other Parameters
----------------
name : hikari.undefined.UndefinedOr[str]
If provided, the new name for the thread.
archived : hikari.undefined.UndefinedOr[bool]
If provided, the new archived state for the thread.
auto_archive_duration : hikari.undefined.UndefinedOr[time.Intervalish]
If provided, the new auto archive duration for this thread.

This should be either 60, 1440, 4320 or 10080 seconds and, as of
ZechCodes marked this conversation as resolved.
Show resolved Hide resolved
writing.
locked : hikari.undefined.UndefinedOr[bool]
If provided, the new locked state for the thread.
invitable : hikari.undefined.UndefinedOr[bool]
If provided, the new setting for whether non-moderators can invite
new members to a private thread.
rate_limit_per_user : hikari.undefined.UndefinedOr[hikari.internal.time.Intervalish]
If provided, the new rate limit per user in the thread.
flags : hikari.undefined.UndefinedOr[ChannelFlag]
If provided, the new channel flags to use for the thread. PINNED can
only be used on threads in forum channels.
applied_tags : hikari.undefined.UndefinedOr[typing.Sequence[ForumTag]]
If provided, the new tags to use for the thread in a forum channel.
ZechCodes marked this conversation as resolved.
Show resolved Hide resolved
reason : hikari.undefined.UndefinedOr[str]
If provided, the reason that will be recorded in the audit logs.
Maximum of 512 characters.

Returns
-------
hikari.channels.PartialChannel
The edited thread.

Raises
------
hikari.errors.BadRequestError
If any of the fields that are passed have an invalid value.
hikari.errors.UnauthorizedError
If you are unauthorized to make the request (invalid/missing token).
hikari.errors.ForbiddenError
If you are missing permissions to edit the channel.
hikari.errors.NotFoundError
If the thread is not found.
hikari.errors.RateLimitTooLongError
Raised in the event that a rate limit occurs that is
longer than `max_rate_limit` when making a request.
hikari.errors.RateLimitedError
Usually, Hikari will handle and retry on hitting
rate-limits automatically. This includes most bucket-specific
rate-limits and global rate-limits. In some rare edge cases,
however, Discord implements other undocumented rules for
rate-limiting, such as limits per attribute. These cannot be
detected or handled normally by Hikari due to their undocumented
nature, and will trigger this exception if they occur.
hikari.errors.InternalServerError
If an internal error occurs on Discord while handling the request.
"""
return await self.app.rest.edit_channel(
self.id,
name=name,
archived=archived,
auto_archive_duration=auto_archive_duration,
locked=locked,
invitable=invitable,
rate_limit_per_user=rate_limit_per_user,
flags=flags,
applied_tags=applied_tags,
reason=reason,
)


class GuildNewsThread(GuildThreadChannel):
"""Represents a guild news channel public thread."""
Expand Down
2 changes: 2 additions & 0 deletions hikari/impl/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,7 @@ async def edit_channel(
locked: undefined.UndefinedOr[bool] = undefined.UNDEFINED,
invitable: undefined.UndefinedOr[bool] = undefined.UNDEFINED,
auto_archive_duration: undefined.UndefinedOr[time.Intervalish] = undefined.UNDEFINED,
applied_tags: undefined.UndefinedOr[typing.Sequence[channels_.ForumTag]] = undefined.UNDEFINED,
ZechCodes marked this conversation as resolved.
Show resolved Hide resolved
reason: undefined.UndefinedOr[str] = undefined.UNDEFINED,
) -> channels_.PartialChannel:
if isinstance(auto_archive_duration, datetime.timedelta):
Expand Down Expand Up @@ -1054,6 +1055,7 @@ async def edit_channel(
body.put("auto_archive_duration", auto_archive_duration, conversion=time.timespan_to_int)
body.put("locked", locked)
body.put("invitable", invitable)
body.put_array("applied_tags", applied_tags, conversion=self._entity_factory.serialize_forum_tag)
ZechCodes marked this conversation as resolved.
Show resolved Hide resolved

response = await self._request(route, json=body, reason=reason)
assert isinstance(response, dict)
Expand Down
55 changes: 55 additions & 0 deletions tests/hikari/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,61 @@ async def test_edit(self, model):
)


class TestGuildThreadChannel:
@pytest.fixture()
def model(self, mock_app):
return hikari_test_helpers.mock_class_namespace(channels.GuildThreadChannel)(
app=mock_app,
id=snowflakes.Snowflake(69420),
name="foo2",
type=channels.ChannelType.GUILD_PUBLIC_THREAD,
guild_id=snowflakes.Snowflake(123456789),
parent_id=snowflakes.Snowflake(987654321),
is_archived=False,
auto_archive_duration=679,
is_locked=False,
owner_id=snowflakes.Snowflake(123039302),
thread_created_at=234,
last_message_id=snowflakes.Snowflake(2345342),
last_pin_timestamp=4457867,
rate_limit_per_user=60,
approximate_message_count=10,
approximate_member_count=2,
archive_timestamp=0,
member=None
)

@pytest.mark.asyncio()
async def test_thread_edit(self, model):
ZechCodes marked this conversation as resolved.
Show resolved Hide resolved
model.app.rest.edit_channel = mock.AsyncMock()

result = await model.edit(
name="Supa fast boike",
locked=True,
archived=True,
auto_archive_duration=2345,
invitable=True,
flags=123,
applied_tags=[100001, 100002],
rate_limit_per_user=54123123,
reason="Foo",
)

assert result is model.app.rest.edit_channel.return_value
model.app.rest.edit_channel.assert_awaited_once_with(
69420,
name="Supa fast boike",
locked=True,
archived=True,
auto_archive_duration=2345,
invitable=True,
flags=123,
applied_tags=[100001, 100002],
rate_limit_per_user=54123123,
reason="Foo",
)


class TestPermissibleGuildChannel:
@pytest.fixture()
def model(self, mock_app):
Expand Down