From f5c38c3559ba0ee39bee7204e324fb024189ec57 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Thu, 18 Apr 2019 20:54:16 -0400 Subject: [PATCH] Add abc.GuildChannel.clone to clone a channel with another name. Fixes #2093 --- discord/abc.py | 40 ++++++++++++++++++++++++++++++++++++++++ discord/channel.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/discord/abc.py b/discord/abc.py index ec668f5c4..b0ee61feb 100644 --- a/discord/abc.py +++ b/discord/abc.py @@ -603,6 +603,46 @@ class GuildChannel: else: raise InvalidArgument('Invalid overwrite type provided.') + async def _clone_impl(self, base_attrs, *, name=None, reason=None): + base_attrs['permission_overwrites'] = [ + x._asdict() for x in self._overwrites + ] + base_attrs['parent_id'] = self.category_id + base_attrs['name'] = name or self.name + guild_id = self.guild.id + cls = self.__class__ + data = await self._state.http.create_channel(guild_id, self._type, reason=reason, **base_attrs) + obj = cls(state=self._state, guild=self.guild, data=data) + + # temporarily add it to the cache + self.guild._channels[obj.id] = obj + return obj + + async def clone(self, *, name=None, reason=None): + """|coro| + + Clones this channel. This creates a channel with the same properties + as this channel. + + .. versionadded:: 1.1.0 + + Parameters + ------------ + name: Optional[:class:`str`] + The name of the new channel. If not provided, defaults to this + channel name. + reason: Optional[:class:`str`] + The reason for cloning this channel. Shows up on the audit log. + + Raises + ------- + Forbidden + You do not have the proper permissions to create this channel. + HTTPException + Creating the channel failed. + """ + raise NotImplementedError + async def create_invite(self, *, reason=None, **fields): """|coro| diff --git a/discord/channel.py b/discord/channel.py index 96f9e3c64..e3055f88c 100644 --- a/discord/channel.py +++ b/discord/channel.py @@ -214,6 +214,15 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable): """ await self._edit(options, reason=reason) + async def clone(self, *, name=None, reason=None): + return await self._clone_impl({ + 'topic': self.topic, + 'nsfw': self.nsfw, + 'rate_limit_per_user': self.slowmode_delay + }, name=name, reason=reason) + + clone.__doc__ = discord.abc.GuildChannel.clone.__doc__ + async def delete_messages(self, messages): """|coro| @@ -526,6 +535,14 @@ class VoiceChannel(discord.abc.Connectable, discord.abc.GuildChannel, Hashable): permissions_for.__doc__ = discord.abc.GuildChannel.permissions_for.__doc__ + async def clone(self, *, name=None, reason=None): + return await self._clone_impl({ + 'bitrate': self.bitrate, + 'user_limit': self.user_limit + }, name=name, reason=reason) + + clone.__doc__ = discord.abc.GuildChannel.clone.__doc__ + async def edit(self, *, reason=None, **options): """|coro| @@ -629,6 +646,13 @@ class CategoryChannel(discord.abc.GuildChannel, Hashable): """Checks if the category is NSFW.""" return self.nsfw + async def clone(self, *, name=None, reason=None): + return await self._clone_impl({ + 'nsfw': self.nsfw + }, name=name, reason=reason) + + clone.__doc__ = discord.abc.GuildChannel.clone.__doc__ + async def edit(self, *, reason=None, **options): """|coro| @@ -791,6 +815,13 @@ class StoreChannel(discord.abc.GuildChannel, Hashable): """Checks if the channel is NSFW.""" return self.nsfw + async def clone(self, *, name=None, reason=None): + return await self._clone_impl({ + 'nsfw': self.nsfw + }, name=name, reason=reason) + + clone.__doc__ = discord.abc.GuildChannel.clone.__doc__ + async def edit(self, *, reason=None, **options): """|coro|