Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement to addRole and removeRole methods for Member class #1237

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
48 changes: 32 additions & 16 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 $patch 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 $patch = false): ExtendedPromiseInterface
{
if ($role instanceof Role) {
$role = $role->id;
Expand All @@ -279,12 +280,19 @@ 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)
->then(function () use ($role) {
if (! in_array($role, $this->attributes['roles'])) {
$this->attributes['roles'][] = $role;
}
});
return $patch
? $this->http->patch(Endpoint::bind(Endpoint::GUILD_MEMBER, $this->guild_id, $this->id), ['roles' => array_unique(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 $patch 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 $patch = 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 $patch
? $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