Browse Source

Rework Role.edit to not rely on previous role state

pull/7139/head
Rapptz 4 years ago
parent
commit
2d7c709235
  1. 64
      discord/role.py

64
discord/role.py

@ -23,7 +23,7 @@ DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations
from typing import Any, List, Optional, TypeVar, Union, overload, TYPE_CHECKING
from typing import Any, Dict, List, Optional, TypeVar, Union, overload, TYPE_CHECKING
from .permissions import Permissions
from .errors import InvalidArgument
@ -339,25 +339,18 @@ class Role(Hashable):
payload = [{"id": z[0], "position": z[1]} for z in zip(roles, change_range)]
await http.move_role_position(self.guild.id, payload, reason=reason)
@overload
async def edit(
self,
*,
reason: Optional[str] = ...,
name: str = ...,
permissions: Permissions = ...,
colour: Union[Colour, int] = ...,
hoist: bool = ...,
mentionable: bool = ...,
position: int = ...,
name: str = MISSING,
permissions: Permissions = MISSING,
colour: Union[Colour, int] = MISSING,
color: Union[Colour, int] = MISSING,
hoist: bool = MISSING,
mentionable: bool = MISSING,
position: int = MISSING,
reason: Optional[str] = MISSING,
) -> None:
...
@overload
async def edit(self) -> None:
...
async def edit(self, *, reason=None, **fields) -> None:
"""|coro|
Edits the role.
@ -399,26 +392,31 @@ class Role(Hashable):
role was asked to be moved.
"""
position = fields.get('position')
if position is not None:
if position is not MISSING:
await self._move(position, reason=reason)
self.position = position
try:
colour = fields['colour']
except KeyError:
colour = fields.get('color', self.colour)
if isinstance(colour, int):
colour = Colour(value=colour)
payload = {
'name': fields.get('name', self.name),
'permissions': str(fields.get('permissions', self.permissions).value),
'color': colour.value,
'hoist': fields.get('hoist', self.hoist),
'mentionable': fields.get('mentionable', self.mentionable),
}
payload: Dict[str, Any] = {}
if color is not MISSING:
colour = color
if colour is not MISSING:
if isinstance(colour, int):
payload['color'] = colour
else:
payload['color'] = colour.value
if name is not MISSING:
payload['name'] = name
if permissions is not MISSING:
payload['permissions'] = permissions.value
if hoist is not MISSING:
payload['hoist'] = hoist
if mentionable is not MISSING:
payload['mentionable'] = mentionable
data = await self._state.http.edit_role(self.guild.id, self.id, reason=reason, **payload)
self._update(data)

Loading…
Cancel
Save