Skip to content
This repository was archived by the owner on Mar 13, 2023. It is now read-only.

feat: role.move function #735

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions naff/api/http/http_requests/guild.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, cast, Mapping, Any
from typing import TYPE_CHECKING, List, cast, Mapping, Any

import discord_typings

Expand Down Expand Up @@ -371,22 +371,29 @@ async def create_guild_role(
return cast(discord_typings.RoleData, result)

async def modify_guild_role_positions(
self, guild_id: "Snowflake_Type", role_id: "Snowflake_Type", position: int, reason: str | None = None
self,
guild_id: "Snowflake_Type",
position_changes: List[dict["Snowflake_Type", int]],
reason: str | None = None,
) -> list[discord_typings.RoleData]:
"""
Modify the position of a role in the guild.

Args:
guild_id: The ID of the guild
role_id: The ID of the role to move
position_changes: A list of dicts representing the roles to move and their new positions

``{"id": role_id, "position": new_position}``
position: The new position of this role in the hierarchy
reason: The reason for this action

Returns:
List of guild roles

"""
payload: PAYLOAD_TYPE = [{"id": int(role_id), "position": position}]
payload: PAYLOAD_TYPE = [
{"id": int(role["id"]), "position": int(role["position"])} for role in position_changes
]
result = await self.request(Route("PATCH", f"/guilds/{int(guild_id)}/roles"), payload=payload, reason=reason)
return cast(list[discord_typings.RoleData], result)

Expand Down
17 changes: 17 additions & 0 deletions naff/models/discord/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,20 @@ async def edit(
r_data = dict(r_data) # to convert typed dict to regular dict
r_data["guild_id"] = self._guild_id
return self.from_dict(r_data, self._client)

async def move(self, position: int, reason: str | Missing = MISSING) -> "Role":
"""
Move this role to a new position.

Args:
position: The new position of the role
reason: An optional reason for this move

Returns:
The role object

"""
await self._client.http.modify_guild_role_positions(
self._guild_id, [{"id": self.id, "position": position}], reason
)
return self