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

refactor: allow archiving of threads in channel helper methods #676

Merged
merged 2 commits into from Mar 29, 2022
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
86 changes: 85 additions & 1 deletion interactions/api/models/channel.py
Expand Up @@ -284,11 +284,17 @@ async def modify(
permission_overwrites: Optional[List[Overwrite]] = MISSING,
parent_id: Optional[int] = MISSING,
nsfw: Optional[bool] = MISSING,
archived: Optional[bool] = MISSING,
auto_archive_duration: Optional[int] = MISSING,
locked: Optional[bool] = MISSING,
reason: Optional[str] = None,
) -> "Channel":
"""
Edits the channel.

.. note::
The fields `archived`, `auto_archive_duration` and `locked` require the provided channel to be a thread.

:param name?: The name of the channel, defaults to the current value of the channel
:type name: str
:param topic?: The topic of that channel, defaults to the current value of the channel
Expand All @@ -307,6 +313,12 @@ async def modify(
:type nsfw: Optional[bool]
:param permission_overwrites?: The permission overwrites, if any
:type permission_overwrites: Optional[List[Overwrite]]
:param archived?: Whether the thread is archived
:type archived: Optional[bool]
:param auto_archive_duration?: The time after the thread is automatically archived. One of 60, 1440, 4320, 10080
:type auto_archive_duration: Optional[int]
:param locked?: Whether the thread is locked
:type locked: Optional[bool]
:param reason?: The reason for the edit
:type reason: Optional[str]
:return: The modified channel as new object
Expand Down Expand Up @@ -343,10 +355,25 @@ async def modify(
nsfw=_nsfw,
permission_overwrites=_permission_overwrites,
)

payload = payload._json

if (
archived is not MISSING or auto_archive_duration is not MISSING or locked is not MISSING
) and not self.thread_metadata:
raise ValueError("The specified channel is not a Thread!")

if archived is not MISSING:
payload["archived"] = archived
if auto_archive_duration is not MISSING:
payload["auto_archive_duration"] = auto_archive_duration
if locked is not MISSING:
payload["locked"] = locked

res = await self._client.modify_channel(
channel_id=int(self.id),
reason=reason,
payload=payload._json,
payload=payload,
)
return Channel(**res, _client=self._client)

Expand Down Expand Up @@ -508,6 +535,63 @@ async def set_nsfw(

return await self.modify(nsfw=nsfw, reason=reason)

async def archive(
self,
archived: bool = True,
*,
reason: Optional[str] = None,
) -> "Channel":
"""
Sets the archived state of the thread.

:param archived: Whether the Thread is archived, defaults to True
:type archived: bool
:param reason?: The reason of the archiving
:type reason: Optional[str]
:return: The edited channel
:rtype: Channel
"""

return await self.modify(archived=archived, reason=reason)

async def set_auto_archive_duration(
self,
auto_archive_duration: int,
*,
reason: Optional[str] = None,
) -> "Channel":
"""
Sets the time after the thread is automatically archived.

:param auto_archive_duration: The time after the thread is automatically archived. One of 60, 1440, 4320, 10080
:type auto_archive_duration: int
:param reason?: The reason of the edit
:type reason: Optional[str]
:return: The edited channel
:rtype: Channel
"""

return await self.modify(auto_archive_duration=auto_archive_duration, reason=reason)

async def lock(
self,
locked: bool = True,
*,
reason: Optional[str] = None,
) -> "Channel":
"""
Sets the locked state of the thread.

:param locked: Whether the Thread is locked, defaults to True
:type locked: bool
:param reason?: The reason of the edit
:type reason: Optional[str]
:return: The edited channel
:rtype: Channel
"""

return await self.modify(locked=locked, reason=reason)

async def add_member(
self,
member_id: int,
Expand Down
21 changes: 21 additions & 0 deletions interactions/api/models/channel.pyi
Expand Up @@ -103,6 +103,9 @@ class Channel(DictSerializerMixin):
permission_overwrites: Optional[List[Overwrite]] = MISSING,
parent_id: Optional[int] = MISSING,
nsfw: Optional[bool] = MISSING,
archived: Optional[bool] = MISSING,
auto_archive_duration: Optional[int] = MISSING,
locked: Optional[bool] = MISSING,
reason: Optional[str] = None,
) -> "Channel": ...
async def set_name(
Expand Down Expand Up @@ -153,6 +156,24 @@ class Channel(DictSerializerMixin):
*,
reason: Optional[str] = None
) -> "Channel": ...
async def archive(
self,
archived: bool = True,
*,
reason: Optional[str] = None,
) -> "Channel": ...
async def set_auto_archive_duration(
self,
auto_archive_duration: int,
*,
reason: Optional[str] = None,
) -> "Channel": ...
async def lock(
self,
locked: bool = True,
*,
reason: Optional[str] = None,
) -> "Channel": ...
async def add_member(
self,
member_id: int,
Expand Down
28 changes: 27 additions & 1 deletion interactions/api/models/guild.py
Expand Up @@ -779,11 +779,17 @@ async def modify_channel(
permission_overwrites: Optional[List[Overwrite]] = MISSING,
parent_id: Optional[int] = MISSING,
nsfw: Optional[bool] = MISSING,
archived: Optional[bool] = MISSING,
auto_archive_duration: Optional[int] = MISSING,
locked: Optional[bool] = MISSING,
reason: Optional[str] = None,
) -> Channel:
"""
Edits a channel of the guild.

.. note::
The fields `archived`, `auto_archive_duration` and `locked` require the provided channel to be a thread.

:param channel_id: The id of the channel to modify
:type channel_id: int
:param name?: The name of the channel, defaults to the current value of the channel
Expand All @@ -804,6 +810,12 @@ async def modify_channel(
:type permission_overwrites: Optional[Overwrite]
:param nsfw?: Whether the channel is nsfw or not, defaults to the current value of the channel
:type nsfw: Optional[bool]
:param archived?: Whether the thread is archived
:type archived: Optional[bool]
:param auto_archive_duration?: The time after the thread is automatically archived. One of 60, 1440, 4320, 10080
:type auto_archive_duration: Optional[int]
:param locked?: Whether the thread is locked
:type locked: Optional[bool]
:param reason: The reason for the edit
:type reason: Optional[str]
:return: The modified channel
Expand Down Expand Up @@ -843,10 +855,24 @@ async def modify_channel(
nsfw=_nsfw,
)

payload = payload._json

if (
archived is not MISSING or auto_archive_duration is not MISSING or locked is not MISSING
) and not ch.thread_metadata:
raise ValueError("The specified channel is not a Thread!")

if archived is not MISSING:
payload["archived"] = archived
if auto_archive_duration is not MISSING:
payload["auto_archive_duration"] = auto_archive_duration
if locked is not MISSING:
payload["locked"] = locked

res = await self._client.modify_channel(
channel_id=channel_id,
reason=reason,
payload=payload._json,
payload=payload,
)
return Channel(**res, _client=self._client)

Expand Down
3 changes: 3 additions & 0 deletions interactions/api/models/guild.pyi
Expand Up @@ -236,6 +236,9 @@ class Guild(DictSerializerMixin):
permission_overwrites: Optional[List[Overwrite]] = MISSING,
parent_id: Optional[int] = MISSING,
nsfw: Optional[bool] = MISSING,
archived: Optional[bool] = MISSING,
auto_archive_duration: Optional[int] = MISSING,
locked: Optional[bool] = MISSING,
reason: Optional[str] = None,
) -> Channel: ...
async def modify_member(
Expand Down