Skip to content

Commit

Permalink
Update Member.php
Browse files Browse the repository at this point in the history
  • Loading branch information
valzargaming authored Jun 23, 2024
1 parent 6c6b6fc commit f675991
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/Discord/Parts/User/Member.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,14 @@ public function moveMember($channel, ?string $reason = null): ExtendedPromiseInt
*
* @param Role|string $role The role to add to the member.
* @param string|null $reason Reason for Audit Log.
* @param bool|null $set Whether to set the roles using PATCH instead of PUT and return the updated member part on resolved promise.
*
* @throws \RuntimeException
* @throws NoPermissionsException Missing manage_roles permission.
*
* @return ExtendedPromiseInterface
* @return ExtendedPromiseInterface|ExtendedPromiseInterface<static>
*/
public function addRole($role, ?string $reason = null): ExtendedPromiseInterface
public function addRole($role, ?string $reason = null, ?bool $set = false): ExtendedPromiseInterface
{
if ($role instanceof Role) {
$role = $role->id;
Expand All @@ -279,7 +280,14 @@ public function addRole($role, ?string $reason = null): ExtendedPromiseInterface
$headers['X-Audit-Log-Reason'] = $reason;
}

return $this->http->put(Endpoint::bind(Endpoint::GUILD_MEMBER_ROLE, $this->guild_id, $this->id, $role), null, $headers)
return $set
? $this->http->patch(Endpoint::bind(Endpoint::GUILD_MEMBER, $this->guild_id, $this->id), ['roles' => array_merge($this->attributes['roles'], [$role])], $headers)
->then(function ($response) {
$this->attributes['roles'] = $response->roles;

return $this;
})
: $this->http->put(Endpoint::bind(Endpoint::GUILD_MEMBER_ROLE, $this->guild_id, $this->id, $role), null, $headers)
->then(function () use ($role) {
if (! in_array($role, $this->attributes['roles'])) {
$this->attributes['roles'][] = $role;
Expand All @@ -294,12 +302,13 @@ public function addRole($role, ?string $reason = null): ExtendedPromiseInterface
*
* @param Role|string $role The role to remove from the member.
* @param string|null $reason Reason for Audit Log.
* @param bool|null $set Whether to set the roles using PATCH instead of DELETE and return the updated member part on resolved promise.
*
* @throws NoPermissionsException Missing manage_roles permission.
*
* @return ExtendedPromiseInterface
* @return ExtendedPromiseInterface|ExtendedPromiseInterface<static>
*/
public function removeRole($role, ?string $reason = null): ExtendedPromiseInterface
public function removeRole($role, ?string $reason = null, ?bool $set = false): ExtendedPromiseInterface
{
if ($role instanceof Role) {
$role = $role->id;
Expand All @@ -318,12 +327,19 @@ public function removeRole($role, ?string $reason = null): ExtendedPromiseInterf
$headers['X-Audit-Log-Reason'] = $reason;
}

return $this->http->delete(Endpoint::bind(Endpoint::GUILD_MEMBER_ROLE, $this->guild_id, $this->id, $role), null, $headers)
->then(function () use ($role) {
if (($removeRole = array_search($role, $this->attributes['roles'])) !== false) {
unset($this->attributes['roles'][$removeRole]);
}
});
return $set
? $this->http->patch(Endpoint::bind(Endpoint::GUILD_MEMBER, $this->guild_id, $this->id), ['roles' => array_diff($this->attributes['roles'], [$role])], $headers)
->then(function ($response) {
$this->attributes['roles'] = $response->roles;

return $this;
})
: $this->http->delete(Endpoint::bind(Endpoint::GUILD_MEMBER_ROLE, $this->guild_id, $this->id, $role), null, $headers)
->then(function () use ($role) {
if (($removeRole = array_search($role, $this->attributes['roles'])) !== false) {
unset($this->attributes['roles'][$removeRole]);
}
});
}

/**
Expand Down

0 comments on commit f675991

Please sign in to comment.