Browse Source

Use the improved role creation endpoint for making roles.

pull/468/head
Rapptz 8 years ago
parent
commit
ced617e786
  1. 55
      discord/guild.py
  2. 4
      discord/http.py

55
discord/guild.py

@ -33,6 +33,7 @@ from .member import Member, VoiceState
from .emoji import Emoji from .emoji import Emoji
from .game import Game from .game import Game
from .permissions import PermissionOverwrite from .permissions import PermissionOverwrite
from .colour import Colour
from .errors import InvalidArgument from .errors import InvalidArgument
from .channel import * from .channel import *
from .enums import GuildRegion, Status, ChannelType, try_enum, VerificationLevel from .enums import GuildRegion, Status, ChannelType, try_enum, VerificationLevel
@ -784,22 +785,60 @@ class Guild(Hashable):
Creates a :class:`Role` for the guild. Creates a :class:`Role` for the guild.
This function is similar to :meth:`Role.edit` in both All fields are optional.
the fields taken and exceptions thrown.
Parameters
-----------
name: str
The role name. Defaults to 'new role'.
permissions: :class:`Permissions`
The permissions to have. Defaults to no permissions.
colour: :class:`Colour`
The colour for the role. Defaults to :meth:`Colour.default`.
This is aliased to ``color`` as well.
hoist: bool
Indicates if the role should be shown separately in the member list.
Defaults to False.
mentionable: bool
Indicates if the role should be mentionable by others.
Defaults to False.
Returns Returns
-------- --------
:class:`Role` :class:`Role`
The newly created role. The newly created role.
Raises
-------
Forbidden
You do not have permissions to change the role.
HTTPException
Editing the role failed.
InvalidArgument
An invalid keyword argument was given.
""" """
data = yield from self._state.http.create_role(self.id) try:
role = Role(guild=self, data=data, state=self._state) perms = fields.pop('permissions')
except KeyError:
fields['permissions'] = 0
else:
fields['permissions'] = perms.value
if fields: try:
# we have to call edit because you can't pass a payload to the colour = fields.pop('colour')
# http request currently. except KeyError:
yield from role.edit(**fields) colour = fields.get('color', Colour.default())
finally:
fields['color'] = colour.value
valid_keys = ('name', 'permissions', 'color', 'hoist', 'mentionable')
for key in fields:
if key not in valid_keys:
raise InvalidArgument('%r is not a valid field.' % key)
data = yield from self._state.http.create_role(self.id, **fields)
role = Role(guild=self, data=data, state=self._state)
# TODO: add to cache # TODO: add to cache
return role return role

4
discord/http.py

@ -596,9 +596,9 @@ class HTTPClient:
def replace_roles(self, user_id, guild_id, role_ids): def replace_roles(self, user_id, guild_id, role_ids):
return self.edit_member(guild_id=guild_id, user_id=user_id, roles=role_ids) return self.edit_member(guild_id=guild_id, user_id=user_id, roles=role_ids)
def create_role(self, guild_id): def create_role(self, guild_id, **fields):
r = Route('POST', '/guilds/{guild_id}/roles', guild_id=guild_id) r = Route('POST', '/guilds/{guild_id}/roles', guild_id=guild_id)
return self.request(r) return self.request(r, json=fields)
def add_role(self, guild_id, user_id, role_id): def add_role(self, guild_id, user_id, role_id):
r = Route('PUT', '/guilds/{guild_id}/members/{user_id}/roles/{role_id}', r = Route('PUT', '/guilds/{guild_id}/members/{user_id}/roles/{role_id}',

Loading…
Cancel
Save