Skip to content

Commit 6b49261

Browse files
authored
fix!: Fix changing role positions (#743)
* fix!: Fix changing role positions Changed the api request function to send an JSON array instead of a single object, and made the relevant changes in Guild and Role's corresponding helper functions. Also added a Guild.modify_role_positions() function to handle multiple position changes at once. * refactor: fixed payload type hint
1 parent 9dad167 commit 6b49261

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed

interactions/api/http/guild.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,21 +404,20 @@ async def create_guild_role(
404404

405405
return request
406406

407-
async def modify_guild_role_position(
408-
self, guild_id: int, role_id: int, position: int, reason: Optional[str] = None
407+
async def modify_guild_role_positions(
408+
self, guild_id: int, payload: List[dict], reason: Optional[str] = None
409409
) -> List[dict]:
410410
"""
411411
Modify the position of a role in the guild.
412412
413413
:param guild_id: Guild ID snowflake.
414-
:param role_id: Role ID snowflake.
415-
:param position: The new position of the associated role.
414+
:param payload: A list of dicts containing the role IDs and new positions for all the roles to be moved.
416415
:param reason: The reason for this action, if given.
417416
:return: List of guild roles with updated hierarchy.
418417
"""
419418
return await self._req.request(
420419
Route("PATCH", f"/guilds/{guild_id}/roles"),
421-
json={"id": role_id, "position": position},
420+
json=payload,
422421
reason=reason,
423422
)
424423

interactions/api/http/guild.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ class _GuildRequest:
6464
async def create_guild_role(
6565
self, guild_id: int, payload: dict, reason: Optional[str] = None
6666
) -> dict: ...
67-
async def modify_guild_role_position(
68-
self, guild_id: int, role_id: int, position: int, reason: Optional[str] = None
67+
async def modify_guild_role_positions(
68+
self, guild_id: int, payload: List[dict], reason: Optional[str] = None
6969
) -> List[dict]: ...
7070
async def modify_guild_role(
7171
self, guild_id: int, role_id: int, payload: dict, reason: Optional[str] = None

interactions/api/models/guild.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,11 +1606,36 @@ async def modify_role_position(
16061606
:return: List of guild roles with updated hierarchy
16071607
:rtype: List[Role]
16081608
"""
1609+
return await self.modify_role_positions(
1610+
changes=[{"id": role_id, "position": position}], reason=reason
1611+
)
1612+
1613+
async def modify_role_positions(
1614+
self,
1615+
changes: List[dict],
1616+
reason: Optional[str] = None,
1617+
) -> List[Role]:
1618+
"""
1619+
Modifies the positions of multiple roles in the guild.
1620+
1621+
:param changes: A list of dicts containing roles (id) and their new positions (position)
1622+
:type changes: List[dict]
1623+
:param reason?: The reason for the modifying
1624+
:type reason: Optional[str]
1625+
:return: List of guild roles with updated hierarchy
1626+
:rtype: List[Role]
1627+
"""
16091628
if not self._client:
16101629
raise AttributeError("HTTPClient not found!")
1611-
_role_id = role_id.id if isinstance(role_id, Role) else role_id
1612-
res = await self._client.modify_guild_role_position(
1613-
guild_id=int(self.id), position=position, role_id=_role_id, reason=reason
1630+
res = await self._client.modify_guild_role_positions(
1631+
guild_id=int(self.id),
1632+
payload=[
1633+
{"id": int(change["id"].id), "position": change["position"]}
1634+
if isinstance(change["id"], Role)
1635+
else change
1636+
for change in changes
1637+
],
1638+
reason=reason,
16141639
)
16151640
return [Role(**role, _client=self._client) for role in res]
16161641

interactions/api/models/guild.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,11 @@ class Guild(DictSerializerMixin):
416416
position: int,
417417
reason: Optional[str] = None,
418418
) -> List[Role]: ...
419+
async def modify_role_positions(
420+
self,
421+
changes: List[dict],
422+
reason: Optional[str] = None,
423+
) -> List[Role]: ...
419424
async def get_bans(
420425
self,
421426
limit: Optional[int] = 1000,

interactions/api/models/role.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ async def modify_position(
164164
"""
165165
if not self._client:
166166
raise AttributeError("HTTPClient not found!")
167-
res = await self._client.modify_guild_role_position(
168-
guild_id=guild_id, position=position, role_id=int(self.id), reason=reason
167+
res = await self._client.modify_guild_role_positions(
168+
guild_id=guild_id,
169+
payload=[{"position": position, "id": int(self.id)}],
170+
reason=reason,
169171
)
170172
return [Role(**role, _client=self._client) for role in res]

0 commit comments

Comments
 (0)