diff --git a/disco/api/client.py b/disco/api/client.py index f5c8fe4..a7f4b63 100644 --- a/disco/api/client.py +++ b/disco/api/client.py @@ -6,6 +6,7 @@ from contextlib import contextmanager from gevent.local import local from six.moves.urllib.parse import quote +from holster.enum import EnumAttr from disco.api.http import Routes, HTTPClient, to_bytes from disco.util.logging import LoggingClass from disco.util.sanitize import S @@ -286,32 +287,29 @@ class APIClient(LoggingClass): def guilds_channels_create(self, guild, - name, channel_type, + name, bitrate=None, user_limit=None, permission_overwrites=[], + nsfw=None, parent_id=None, + position=None, reason=None): payload = { 'name': name, - 'channel_type': channel_type, + 'type': channel_type.value if isinstance(channel_type, EnumAttr) else channel_type, 'permission_overwrites': [i.to_dict() for i in permission_overwrites], 'parent_id': parent_id, } - if channel_type == 'text': - pass - elif channel_type == 'voice': - if bitrate is not None: - payload['bitrate'] = bitrate - - if user_limit is not None: - payload['user_limit'] = user_limit - else: - # TODO: better error here? - raise Exception('Invalid channel type: {}'.format(channel_type)) + payload.update(optional( + nsfw=nsfw, + bitrate=bitrate, + user_limit=user_limit, + position=position, + )) r = self.http( Routes.GUILDS_CHANNELS_CREATE, diff --git a/disco/types/guild.py b/disco/types/guild.py index 95cc1eb..884bd9a 100644 --- a/disco/types/guild.py +++ b/disco/types/guild.py @@ -1,4 +1,5 @@ import six +import warnings from holster.enum import Enum @@ -12,7 +13,7 @@ from disco.types.base import ( ) from disco.types.user import User from disco.types.voice import VoiceState -from disco.types.channel import Channel +from disco.types.channel import Channel, ChannelType from disco.types.message import Emoji from disco.types.permissions import PermissionValue, Permissions, Permissible @@ -440,8 +441,54 @@ class Guild(SlottedModel, Permissible): self.client.api.guilds_bans_create(self.id, to_snowflake(user), *args, **kwargs) def create_channel(self, *args, **kwargs): + warnings.warn( + 'Guild.create_channel will be deprecated soon, please use:' + ' Guild.create_text_channel or Guild.create_category or Guild.create_voice_channel', + DeprecationWarning) + return self.client.api.guilds_channels_create(self.id, *args, **kwargs) + def create_category(self, name, permission_overwrites=[], position=None, reason=None): + ''' + Creates a category within the guild. + ''' + return self.client.api.guilds_channels_create( + self.id, ChannelType.GUILD_CATEGORY, name=name, permission_overwrites=permission_overwrites, + position=position, reason=reason, + ) + + def create_text_channel( + self, + name, + permission_overwrites=[], + parent_id=None, + nsfw=None, + position=None, + reason=None): + ''' + Creates a text channel within the guild. + ''' + return self.client.api.guilds_channels_create( + self.id, ChannelType.GUILD_TEXT, name=name, permission_overwrites=permission_overwrites, + parent_id=parent_id, nsfw=nsfw, position=position, reason=reason, + ) + + def create_voice_channel( + self, + name, + permission_overwrites=[], + parent_id=None, + bitrate=None, + user_limit=None, + position=None, + reason=None): + ''' + Creates a voice channel within the guild. + ''' + return self.client.api.guilds_channels_create( + self.id, ChannelType.GUILD_VOICE, name=name, permission_overwrites=permission_overwrites, + parent_id=parent_id, bitrate=bitrate, user_limit=user_limit, position=position, reason=None) + def leave(self): return self.client.api.users_me_guilds_delete(self.id) diff --git a/examples/basic_plugin.py b/examples/basic_plugin.py index b615aa8..4a02338 100644 --- a/examples/basic_plugin.py +++ b/examples/basic_plugin.py @@ -9,8 +9,15 @@ class BasicPlugin(Plugin): def on_auditme(self, event): invite = event.channel.create_invite(reason='TEST AUDIT') invite.delete(reason='TEST AUDIT 2') - # channel = event.guild.create_channel('audit-log-test', 'text', reason='TEST CREATE') - # channel.delete(reason='TEST AUDIT 2') + channel = event.guild.create_channel('audit-log-test', 'text', reason='TEST CREATE') + channel.delete(reason='TEST AUDIT 2') + + @Plugin.command('create-some-channels') + def on_create_some_channels(self, event): + category = event.guild.create_category('My Category') + event.guild.create_text_channel('text-channel', parent_id=category.id) + event.guild.create_voice_channel('voice-channel', parent_id=category.id) + event.msg.reply('Ok, created some channels') @Plugin.command('ratelimitme') def on_ratelimitme(self, event):