From 711dfb83abae5486b70489e0151195ac4f316d52 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Fri, 29 Sep 2017 05:51:40 -0400 Subject: [PATCH] Allow creating a channel with a category. --- discord/guild.py | 23 +++++++++++++++++------ discord/http.py | 5 ++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/discord/guild.py b/discord/guild.py index 9609dade4..2e1701430 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -515,7 +515,7 @@ class Guild(Hashable): return utils.find(pred, members) - def _create_channel(self, name, overwrites, channel_type, reason): + def _create_channel(self, name, overwrites, channel_type, category=None, reason=None): if overwrites is None: overwrites = {} elif not isinstance(overwrites, dict): @@ -540,10 +540,12 @@ class Guild(Hashable): perms.append(payload) - return self._state.http.create_channel(self.id, name, channel_type.value, permission_overwrites=perms, reason=reason) + parent_id = category.id if category else None + return self._state.http.create_channel(self.id, name, channel_type.value, parent_id=parent_id, + permission_overwrites=perms, reason=reason) @asyncio.coroutine - def create_text_channel(self, name, *, overwrites=None, reason=None): + def create_text_channel(self, name, *, overwrites=None, category=None, reason=None): """|coro| Creates a :class:`TextChannel` for the guild. @@ -583,6 +585,10 @@ class Guild(Hashable): A `dict` of target (either a role or a member) to :class:`PermissionOverwrite` to apply upon creation of a channel. Useful for creating secret channels. + category: Optional[:class:`CategoryChannel`] + The category to place the newly created channel under. + The permissions will be automatically synced to category if no + overwrites are provided. reason: Optional[str] The reason for creating this channel. Shows up on the audit log. @@ -600,7 +606,7 @@ class Guild(Hashable): :class:`TextChannel` The channel that was just created. """ - data = yield from self._create_channel(name, overwrites, ChannelType.text, reason=reason) + data = yield from self._create_channel(name, overwrites, ChannelType.text, category, reason=reason) channel = TextChannel(state=self._state, guild=self, data=data) # temporarily add to the cache @@ -608,12 +614,12 @@ class Guild(Hashable): return channel @asyncio.coroutine - def create_voice_channel(self, name, *, overwrites=None, reason=None): + def create_voice_channel(self, name, *, overwrites=None, category=None, reason=None): """|coro| Same as :meth:`create_text_channel` except makes a :class:`VoiceChannel` instead. """ - data = yield from self._create_channel(name, overwrites, ChannelType.voice, reason=reason) + data = yield from self._create_channel(name, overwrites, ChannelType.voice, category, reason=reason) channel = VoiceChannel(state=self._state, guild=self, data=data) # temporarily add to the cache @@ -625,6 +631,11 @@ class Guild(Hashable): """|coro| Same as :meth:`create_text_channel` except makes a :class:`CategoryChannel` instead. + + .. note:: + + The ``category`` parameter is not supported in this function since categories + cannot have categories. """ data = yield from self._create_channel(name, overwrites, ChannelType.category, reason=reason) channel = CategoryChannel(state=self._state, guild=self, data=data) diff --git a/discord/http.py b/discord/http.py index ab2534e20..bcdfcfe64 100644 --- a/discord/http.py +++ b/discord/http.py @@ -511,7 +511,7 @@ class HTTPClient: r = Route('PATCH', '/guilds/{guild_id}/channels', guild_id=guild_id) return self.request(r, json=data, reason=reason) - def create_channel(self, guild_id, name, channel_type, permission_overwrites=None, *, reason=None): + def create_channel(self, guild_id, name, channel_type, parent_id=None, permission_overwrites=None, *, reason=None): payload = { 'name': name, 'type': channel_type @@ -520,6 +520,9 @@ class HTTPClient: if permission_overwrites is not None: payload['permission_overwrites'] = permission_overwrites + if parent_id is not None: + payload['parent_id'] = parent_id + return self.request(Route('POST', '/guilds/{guild_id}/channels', guild_id=guild_id), json=payload, reason=reason) def delete_channel(self, channel_id, *, reason=None):