From f6759914715d2fff1867601efc79f199dc72a67f Mon Sep 17 00:00:00 2001 From: Valithor Obsidion Date: Sun, 23 Jun 2024 15:52:14 -0400 Subject: [PATCH 1/6] Update Member.php --- src/Discord/Parts/User/Member.php | 38 ++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/src/Discord/Parts/User/Member.php b/src/Discord/Parts/User/Member.php index 803e5f7d3..db7516024 100644 --- a/src/Discord/Parts/User/Member.php +++ b/src/Discord/Parts/User/Member.php @@ -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 */ - 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; @@ -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; @@ -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 */ - 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; @@ -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]); + } + }); } /** From 339f9d60d76d5c005019f91643dd0247580f7453 Mon Sep 17 00:00:00 2001 From: Valithor Obsidion Date: Sun, 23 Jun 2024 16:06:47 -0400 Subject: [PATCH 2/6] Renamed $set to $patch --- src/Discord/Parts/User/Member.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Discord/Parts/User/Member.php b/src/Discord/Parts/User/Member.php index db7516024..d2f303684 100644 --- a/src/Discord/Parts/User/Member.php +++ b/src/Discord/Parts/User/Member.php @@ -249,14 +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. + * @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|ExtendedPromiseInterface */ - public function addRole($role, ?string $reason = null, ?bool $set = false): ExtendedPromiseInterface + public function addRole($role, ?string $reason = null, ?bool $patch = false): ExtendedPromiseInterface { if ($role instanceof Role) { $role = $role->id; @@ -280,7 +280,7 @@ public function addRole($role, ?string $reason = null, ?bool $set = false): Exte $headers['X-Audit-Log-Reason'] = $reason; } - return $set + return $patch ? $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; @@ -302,13 +302,13 @@ public function addRole($role, ?string $reason = null, ?bool $set = false): Exte * * @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. + * @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|ExtendedPromiseInterface */ - public function removeRole($role, ?string $reason = null, ?bool $set = false): ExtendedPromiseInterface + public function removeRole($role, ?string $reason = null, ?bool $patch = false): ExtendedPromiseInterface { if ($role instanceof Role) { $role = $role->id; @@ -327,7 +327,7 @@ public function removeRole($role, ?string $reason = null, ?bool $set = false): E $headers['X-Audit-Log-Reason'] = $reason; } - return $set + 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; From 074525eafc89726b332c72790109903e346aec8a Mon Sep 17 00:00:00 2001 From: Valithor Obsidion Date: Sun, 23 Jun 2024 16:09:41 -0400 Subject: [PATCH 3/6] Change ?bool to bool --- src/Discord/Parts/User/Member.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Discord/Parts/User/Member.php b/src/Discord/Parts/User/Member.php index d2f303684..73ca7b83c 100644 --- a/src/Discord/Parts/User/Member.php +++ b/src/Discord/Parts/User/Member.php @@ -256,7 +256,7 @@ public function moveMember($channel, ?string $reason = null): ExtendedPromiseInt * * @return ExtendedPromiseInterface|ExtendedPromiseInterface */ - public function addRole($role, ?string $reason = null, ?bool $patch = false): ExtendedPromiseInterface + public function addRole($role, ?string $reason = null, bool $patch = false): ExtendedPromiseInterface { if ($role instanceof Role) { $role = $role->id; @@ -308,7 +308,7 @@ public function addRole($role, ?string $reason = null, ?bool $patch = false): Ex * * @return ExtendedPromiseInterface|ExtendedPromiseInterface */ - public function removeRole($role, ?string $reason = null, ?bool $patch = false): ExtendedPromiseInterface + public function removeRole($role, ?string $reason = null, bool $patch = false): ExtendedPromiseInterface { if ($role instanceof Role) { $role = $role->id; From ce6ba886dc3c2d6a646fd4a7a4f8cdd03df9b169 Mon Sep 17 00:00:00 2001 From: Valithor Obsidion Date: Sun, 23 Jun 2024 16:11:49 -0400 Subject: [PATCH 4/6] Use array_unique to prevent accidentally adding an existing role --- src/Discord/Parts/User/Member.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Discord/Parts/User/Member.php b/src/Discord/Parts/User/Member.php index 73ca7b83c..edfd28c9b 100644 --- a/src/Discord/Parts/User/Member.php +++ b/src/Discord/Parts/User/Member.php @@ -281,7 +281,7 @@ public function addRole($role, ?string $reason = null, bool $patch = false): Ext } return $patch - ? $this->http->patch(Endpoint::bind(Endpoint::GUILD_MEMBER, $this->guild_id, $this->id), ['roles' => array_merge($this->attributes['roles'], [$role])], $headers) + ? $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; From 6f5b9854fee8a49858599ef01e1b8226c60c85ae Mon Sep 17 00:00:00 2001 From: Valithor Obsidion Date: Sun, 23 Jun 2024 16:20:22 -0400 Subject: [PATCH 5/6] Doc block indentation --- src/Discord/Parts/User/Member.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Discord/Parts/User/Member.php b/src/Discord/Parts/User/Member.php index edfd28c9b..7d2cfaef9 100644 --- a/src/Discord/Parts/User/Member.php +++ b/src/Discord/Parts/User/Member.php @@ -249,7 +249,7 @@ 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. + * @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. @@ -302,7 +302,7 @@ public function addRole($role, ?string $reason = null, bool $patch = false): Ext * * @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. + * @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. * From bb6353f44c2ea3149e9b6861c521957d61a97f56 Mon Sep 17 00:00:00 2001 From: Valithor Obsidion Date: Sun, 23 Jun 2024 16:52:47 -0400 Subject: [PATCH 6/6] Fix indentation inside of ternary --- src/Discord/Parts/User/Member.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Discord/Parts/User/Member.php b/src/Discord/Parts/User/Member.php index 7d2cfaef9..675a4566d 100644 --- a/src/Discord/Parts/User/Member.php +++ b/src/Discord/Parts/User/Member.php @@ -288,11 +288,11 @@ public function addRole($role, ?string $reason = null, bool $patch = false): Ext 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; - } - }); + ->then(function () use ($role) { + if (! in_array($role, $this->attributes['roles'])) { + $this->attributes['roles'][] = $role; + } + }); } /**