Skip to content

Commit

Permalink
Partially implement CollectionInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
valzargaming committed Jan 17, 2025
1 parent 39b428d commit 71c24b4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
32 changes: 25 additions & 7 deletions src/Discord/Parts/Guild/Guild.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Carbon\Carbon;
use Discord\Exceptions\FileNotFoundException;
use Discord\Helpers\Collection;
use Discord\Helpers\CollectionInterface;
use Discord\Helpers\Multipart;
use Discord\Http\Endpoint;
use Discord\Http\Exceptions\NoPermissionsException;
Expand Down Expand Up @@ -335,12 +336,22 @@ protected function setChannelsAttribute(?array $channels): void
/**
* Sets the roles attribute.
*
* @param ?array $roles
* @param CollectionInterface|array|null $roles
*/
protected function setRolesAttribute(?array $roles): void
protected function setRolesAttribute($roles): void
{
if ($roles instanceof CollectionInterface) {
$roles = $roles->toArray();
}
if ($roles === null) {
$roles = [];
}
if (! is_array($roles)) {
throw new \InvalidArgumentException('Roles must be an array or CollectionInterface.');
}

$rolesDiscrim = $this->roles->discrim;
foreach ($roles ?? [] as $role) {
foreach ($roles as $role) {
$role = (array) $role;
/** @var ?Role */
if ($rolePart = $this->roles->offsetGet($role[$rolesDiscrim])) {
Expand Down Expand Up @@ -1106,16 +1117,23 @@ public function getBotPermissions(): ?RolePermission
*
* @link https://discord.com/developers/docs/resources/guild#modify-guild-role-positions
*
* @param array $roles Associative array where the LHS key is the position,
* and the RHS value is a `Role` object or a string ID,
* e.g. `[1 => 'role_id_1', 3 => 'role_id_3']`.
* @param CollectionInterface|array $roles Associative array where the LHS key is the position,
* and the RHS value is a `Role` object or a string ID,
* e.g. `[1 => 'role_id_1', 3 => 'role_id_3']`.
*
* @throws NoPermissionsException Missing manage_roles permission.
*
* @return PromiseInterface<self>
*/
public function updateRolePositions(array $roles): PromiseInterface
public function updateRolePositions($roles): PromiseInterface
{
if ($roles instanceof CollectionInterface) {
$roles = $roles->toArray();
}
if (! is_array($roles)) {
return reject(new \InvalidArgumentException('Roles must be an array of Role instances or Role IDs.'));
}

$botperms = $this->getBotPermissions();
if ($botperms && ! $botperms->manage_roles) {
return reject(new NoPermissionsException("You do not have permission to update role positions in the guild {$this->id}."));
Expand Down
14 changes: 11 additions & 3 deletions src/Discord/Parts/User/Member.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Discord\Builders\MessageBuilder;
use Discord\Helpers\BigInt;
use Discord\Helpers\Collection;
use Discord\Helpers\CollectionInterface;
use Discord\Http\Endpoint;
use Discord\Http\Exceptions\NoPermissionsException;
use Discord\Parts\Channel\Channel;
Expand Down Expand Up @@ -331,15 +332,22 @@ public function removeRole($role, ?string $reason = null): PromiseInterface
*
* @link https://discord.com/developers/docs/resources/guild#modify-guild-member
*
* @param Role[]|string[] $roles The roles to set to the member.
* @param string|null $reason Reason for Audit Log.
* @param CollectionInterface|Role[]|string[] $roles The roles to set to the member.
* @param string|null $reason Reason for Audit Log.
*
* @throws NoPermissionsException Missing manage_roles permission.
*
* @return PromiseInterface<self>
*/
public function setRoles(array $roles, ?string $reason = null): PromiseInterface
public function setRoles($roles, ?string $reason = null): PromiseInterface
{
if ($roles instanceof CollectionInterface) {
$roles = $roles->toArray();
}
if (! is_array($roles)) {
return reject(new \InvalidArgumentException('Roles must be an array of Role instances or Role IDs.'));
}

foreach ($roles as $i => $role) {
if ($role instanceof Role) {
$roles[$i] = $role->id;
Expand Down

0 comments on commit 71c24b4

Please sign in to comment.