Skip to content
This repository has been archived by the owner on Aug 1, 2021. It is now read-only.

Commit

Permalink
[guild] rework role update interface
Browse files Browse the repository at this point in the history
Previously to update a role you would update attributes of the role
object, and then call save. This was very much an anti-pattern when
compared to the way things work elsewhere, and had the following
annoyances attached to it;

- Updating attributes of the object actually smashes state, we aren't
guarenteed that the user will call save nor that the API will accept our
save, and thus attributes on the object (which could be shared within
the state module) are smashed and innaccurate.
- Guild.update_role is effectively useless in this case, we can only
ever pass a role object in. This makes partial updates impossible, and
forces us to match the OO interface to use it.

The new style follows along with how we do things elsewhere, there is
still likely some additional work that can be done here to allow passing
in the role, but for now we'll just call this a deprecation
  • Loading branch information
b1naryth1ef committed Aug 13, 2017
1 parent 4244727 commit b755c62
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
19 changes: 17 additions & 2 deletions disco/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,26 @@ def guilds_roles_modify_batch(self, guild, roles, reason=None):
r = self.http(Routes.GUILDS_ROLES_MODIFY_BATCH, dict(guild=guild), json=roles, headers=_reason_header(reason))
return Role.create_map(self.client, r.json(), guild_id=guild)

def guilds_roles_modify(self, guild, role, reason=None, **kwargs):
def guilds_roles_modify(self, guild, role,
name=None,
hoist=None,
color=None,
permissions=None,
position=None,
mentionable=None,
reason=None):

r = self.http(
Routes.GUILDS_ROLES_MODIFY,
dict(guild=guild, role=role),
json=kwargs,
json=optional(
name=name,
hoist=hoist,
color=color,
permissions=permissions,
position=position,
mentionable=mentionable,
),
headers=_reason_header(reason))
return Role.create(self.client, r.json(), guild_id=guild)

Expand Down
18 changes: 7 additions & 11 deletions disco/types/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ def __str__(self):
def delete(self):
self.guild.delete_role(self)

def save(self):
self.guild.update_role(self)
def update(self, *args, **kwargs):
self.guild.update_role(self, *args, **kwargs)

@property
def mention(self):
Expand Down Expand Up @@ -410,15 +410,11 @@ def delete_role(self, role):
"""
self.client.api.guilds_roles_delete(self.id, to_snowflake(role))

def update_role(self, role):
return self.client.api.guilds_roles_modify(self.id, role.id, **{
'name': role.name,
'permissions': role.permissions.value,
'position': role.position,
'color': role.color,
'hoist': role.hoist,
'mentionable': role.mentionable,
})
def update_role(self, role, **kwargs):
if 'permissions' in kwargs and isinstance(kwargs['permissions'], PermissionValue):
kwargs['permissions'] = kwargs['permissions'].value

return self.client.api.guilds_roles_modify(self.id, to_snowflake(role), **kwargs)

def sync(self):
if self.synced:
Expand Down

0 comments on commit b755c62

Please sign in to comment.