Allow additional parameters on channel creation

This commit is contained in:
Tyler 2018-12-18 14:14:13 -06:00 committed by Rapptz
parent eab0a4f38d
commit febb8a965c
2 changed files with 46 additions and 18 deletions

View File

@ -555,7 +555,7 @@ class Guild(Hashable):
return utils.find(pred, members) return utils.find(pred, members)
def _create_channel(self, name, overwrites, channel_type, category=None, reason=None): def _create_channel(self, name, overwrites, channel_type, category=None, **options):
if overwrites is None: if overwrites is None:
overwrites = {} overwrites = {}
elif not isinstance(overwrites, dict): elif not isinstance(overwrites, dict):
@ -580,11 +580,16 @@ class Guild(Hashable):
perms.append(payload) perms.append(payload)
parent_id = category.id if category else None try:
return self._state.http.create_channel(self.id, name, channel_type.value, parent_id=parent_id, options['rate_limit_per_user'] = options.pop('slowmode_delay')
permission_overwrites=perms, reason=reason) except KeyError:
pass
async def create_text_channel(self, name, *, overwrites=None, category=None, reason=None): parent_id = category.id if category else None
return self._state.http.create_channel(self.id, channel_type.value, name=name, parent_id=parent_id,
permission_overwrites=perms, **options)
async def create_text_channel(self, name, *, overwrites=None, category=None, reason=None, **options):
"""|coro| """|coro|
Creates a :class:`TextChannel` for the guild. Creates a :class:`TextChannel` for the guild.
@ -596,6 +601,12 @@ class Guild(Hashable):
channel upon creation. This parameter expects a :class:`dict` of channel upon creation. This parameter expects a :class:`dict` of
overwrites with the target (either a :class:`Member` or a :class:`Role`) overwrites with the target (either a :class:`Member` or a :class:`Role`)
as the key and a :class:`PermissionOverwrite` as the value. as the key and a :class:`PermissionOverwrite` as the value.
Note
--------
Creating a channel of a specified position will not update the position of
other channels to follow suit. A follow-up call to :meth:`~TextChannel.edit`
will be required to update the position of the channel in the channel list.
Examples Examples
---------- ----------
@ -619,7 +630,7 @@ class Guild(Hashable):
Parameters Parameters
----------- -----------
name: str name: :class:`str`
The channel's name. The channel's name.
overwrites overwrites
A :class:`dict` of target (either a role or a member) to A :class:`dict` of target (either a role or a member) to
@ -629,7 +640,17 @@ class Guild(Hashable):
The category to place the newly created channel under. The category to place the newly created channel under.
The permissions will be automatically synced to category if no The permissions will be automatically synced to category if no
overwrites are provided. overwrites are provided.
reason: Optional[str] position: :class:`int`
The position in the channel list. This is a number that starts
at 0. e.g. the top channel is position 0.
topic: Optional[:class:`str`]
The new channel's topic.
slowmode_delay: :class:`int`
Specifies the slowmode rate limit for user in this channel.
The maximum value possible is `120`.
nsfw: :class:`bool`
To mark the channel as NSFW or not.
reason: Optional[:class:`str`]
The reason for creating this channel. Shows up on the audit log. The reason for creating this channel. Shows up on the audit log.
Raises Raises
@ -646,19 +667,27 @@ class Guild(Hashable):
:class:`TextChannel` :class:`TextChannel`
The channel that was just created. The channel that was just created.
""" """
data = await self._create_channel(name, overwrites, ChannelType.text, category, reason=reason) data = await self._create_channel(name, overwrites, ChannelType.text, category, reason=reason, **options)
channel = TextChannel(state=self._state, guild=self, data=data) channel = TextChannel(state=self._state, guild=self, data=data)
# temporarily add to the cache # temporarily add to the cache
self._channels[channel.id] = channel self._channels[channel.id] = channel
return channel return channel
async def create_voice_channel(self, name, *, overwrites=None, category=None, reason=None): async def create_voice_channel(self, name, *, overwrites=None, category=None, reason=None, **options):
"""|coro| """|coro|
Same as :meth:`create_text_channel` except makes a :class:`VoiceChannel` instead. This is similar to :meth:`create_text_channel` except makes a :class:`VoiceChannel` instead, in addition
to having the following new parameters.
Parameters
-----------
bitrate: :class:`int`
The channel's preferred audio bitrate in bits per second.
user_limit: :class:`int`
The channel's limit for number of members that can be in a voice channel.
""" """
data = await self._create_channel(name, overwrites, ChannelType.voice, category, reason=reason) data = await self._create_channel(name, overwrites, ChannelType.voice, category, reason=reason, **options)
channel = VoiceChannel(state=self._state, guild=self, data=data) channel = VoiceChannel(state=self._state, guild=self, data=data)
# temporarily add to the cache # temporarily add to the cache

View File

@ -508,17 +508,16 @@ class HTTPClient:
r = Route('PATCH', '/guilds/{guild_id}/channels', guild_id=guild_id) r = Route('PATCH', '/guilds/{guild_id}/channels', guild_id=guild_id)
return self.request(r, json=data, reason=reason) return self.request(r, json=data, reason=reason)
def create_channel(self, guild_id, name, channel_type, parent_id=None, permission_overwrites=None, *, reason=None): def create_channel(self, guild_id, channel_type, *, reason=None, **options):
payload = { payload = {
'name': name,
'type': channel_type 'type': channel_type
} }
if permission_overwrites is not None: valid_keys = ('name', 'parent_id', 'topic', 'bitrate', 'nsfw',
payload['permission_overwrites'] = permission_overwrites 'user_limit', 'position', 'permission_overwrites', 'rate_limit_per_user')
payload.update({
if parent_id is not None: k: v for k, v in options.items() if k in valid_keys and v 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) return self.request(Route('POST', '/guilds/{guild_id}/channels', guild_id=guild_id), json=payload, reason=reason)