Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
✨ Added Channel.bulk_delete_messages() and `Channel.edit_permission…
Browse files Browse the repository at this point in the history
…s()` (#316)

* ~~trying to~~ implement channel.Channel.edit_permissions

* hmm trying to do it

* implement Channel.edit_permissions

* implement bulk_delete_messages

* ~~trying to~~ implement channel.Channel.edit_permissions

* hmm trying to do it

* implement Channel.edit_permissions

* implement bulk_delete_messages

* multiline parameters

* 💬 Update pincer/objects/guild/channel.py

Co-authored-by: Lunarmagpie <[email protected]>
  • Loading branch information
darvil82 and Lunarmagpie authored Dec 17, 2021
1 parent 4e28ff2 commit 7343160
Showing 1 changed file with 102 additions and 32 deletions.
134 changes: 102 additions & 32 deletions pincer/objects/guild/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ..message.user_message import UserMessage
from ..._config import GatewayConfig
from ...utils.api_object import APIObject, GuildProperty
from ...utils.conversion import construct_client_dict
from ...utils.conversion import construct_client_dict, remove_none
from ...utils.convert_message import convert_message
from ...utils.types import MISSING

Expand Down Expand Up @@ -236,9 +236,9 @@ async def edit(
...

async def edit(
self,
reason: Optional[str] = None,
**kwargs
self,
reason: Optional[str] = None,
**kwargs
):
"""Edit a channel with the given keyword arguments.
Expand Down Expand Up @@ -271,11 +271,74 @@ async def edit(
channel_cls = _channel_type_map.get(data["type"], Channel)
return channel_cls.from_dict(data)

async def edit_permissions(
self,
overwrite: Overwrite,
allow: str,
deny: str,
type: int,
reason: Optional[str] = None
):
"""
Edit the channel permission overwrites for a user or role in a channel.
Only usable for guild channels. Requires the ``MANAGE_ROLES`` permission.
Only permissions your bot has in the guild or channel can be
allowed/denied (unless your bot has a ``MANAGE_ROLES`` overwrite in the channel).
Parameters
----------
overwrite: :class:`~pincer.objects.guild.overwrite.Overwrite`
The overwrite object.
allow: :class:`str`
The bitwise value of all allowed permissions.
deny: :class:`str`
The bitwise value of all denied permissions.
type: :class:`int`
0 for a role or 1 for a member.
reason: Optional[:class:`str`]
The reason of the channel delete.
"""
await self._http.put(
f"channels/{self.id}/permissions/{overwrite.id}",
headers=remove_none({"X-Audit-Log-Reason": reason}),
data={
"allow": allow,
"deny": deny,
"type": type
}
)

async def bulk_delete_messages(
self,
messages: List[Snowflake],
reason: Optional[str] = None
):
"""Delete multiple messages in a single request.
This endpoint can only be used on guild channels and requires
the ``MANAGE_MESSAGES`` permission.
This endpoint will not delete messages older than 2 weeks, and will
fail with a 400 BAD REQUEST if any message provided is older than that
or if any duplicate message IDs are provided.
Parameters
----------
messages: List[:class:`Snowflake`]
The list of message IDs to delete (2-100).
reason: Optional[:class:`str`]
The reason of the channel delete.
"""
await self._http.post(
f"channels/{self.id}/messages/bulk_delete",
headers=remove_none({"X-Audit-Log-Reason": reason}),
data={"messages": messages}
)

async def delete(
self,
reason: Optional[str] = None,
/,
channel_id: Optional[Snowflake] = None
self,
reason: Optional[str] = None,
/,
channel_id: Optional[Snowflake] = None
):
"""|coro|
Expand All @@ -290,14 +353,9 @@ async def delete(
"""
channel_id = channel_id or self.id

headers = {}

if reason is not None:
headers["X-Audit-Log-Reason"] = str(reason)

await self._http.delete(
f"channels/{channel_id}",
headers
headers=remove_none({"X-Audit-Log-Reason": reason})
)

async def __post_send_handler(self, message: Message):
Expand All @@ -314,8 +372,8 @@ async def __post_send_handler(self, message: Message):
await self.delete()

def __post_sent(
self,
message: Message
self,
message: Message
):
"""Ensure the `__post_send_handler` method its future.
Expand Down Expand Up @@ -380,12 +438,16 @@ class TextChannel(Channel):

@overload
async def edit(
self, name: str = None, type: ChannelType = None,
position: int = None, topic: str = None, nsfw: bool = None,
rate_limit_per_user: int = None,
permissions_overwrites: List[Overwrite] = None,
parent_id: Snowflake = None,
default_auto_archive_duration: int = None
self,
name: str = None,
type: ChannelType = None,
position: int = None,
topic: str = None,
nsfw: bool = None,
rate_limit_per_user: int = None,
permissions_overwrites: List[Overwrite] = None,
parent_id: Snowflake = None,
default_auto_archive_duration: int = None
) -> Union[TextChannel, NewsChannel]:
...

Expand Down Expand Up @@ -430,10 +492,14 @@ class VoiceChannel(Channel):

@overload
async def edit(
self, name: str = None, position: int = None, bitrate: int = None,
user_limit: int = None,
permissions_overwrites: List[Overwrite] = None,
rtc_region: str = None, video_quality_mod: int = None
self,
name: str = None,
position: int = None,
bitrate: int = None,
user_limit: int = None,
permissions_overwrites: List[Overwrite] = None,
rtc_region: str = None,
video_quality_mod: int = None
) -> VoiceChannel:
...

Expand Down Expand Up @@ -465,11 +531,15 @@ class NewsChannel(Channel):

@overload
async def edit(
self, name: str = None, type: ChannelType = None,
position: int = None, topic: str = None, nsfw: bool = None,
permissions_overwrites: List[Overwrite] = None,
parent_id: Snowflake = None,
default_auto_archive_duration: int = None
self,
name: str = None,
type: ChannelType = None,
position: int = None,
topic: str = None,
nsfw: bool = None,
permissions_overwrites: List[Overwrite] = None,
parent_id: Snowflake = None,
default_auto_archive_duration: int = None
) -> Union[TextChannel, NewsChannel]:
...

Expand All @@ -492,7 +562,7 @@ async def edit(self, **kwargs):
class PublicThread(Channel):
"""A subclass of ``Channel`` for public threads with all the same attributes."""


class PrivateThread(Channel):
"""A subclass of ``Channel`` for private threads with all the same attributes."""

Expand Down

0 comments on commit 7343160

Please sign in to comment.