-
-
Notifications
You must be signed in to change notification settings - Fork 240
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
Collection enhancements #1277
Merged
Merged
Collection enhancements #1277
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some sneak previews! Before if (! $members_array = $members->toArray()) return $message->react(":x:"); // No members to process
$promise = array_shift($members_array)->addRole($this->civ13->role_ids['Verified']);
if (! $members_array) return $promise->then(static fn() => $message->react(":thumbsup:")); // There was only one member to process
return array_reduce($members_array, fn(PromiseInterface $carry_promise, Member $member) =>
$carry_promise->then(fn() => $member->addRole($this->civ13->role_ids['Verified'])),
$promise)
->then(static fn() => $message->react(":thumbsup:")); After return $message->react("⏱️")
->then(static fn() => $members->reduce(fn(PromiseInterface $carry_promise, Member $member): PromiseInterface =>
$carry_promise->then(fn() => $member->addRole($this->civ13->role_ids['Verified'])),
$members->shift()->addRole($this->civ13->role_ids['Verified'])))
->then(static fn() => $message->react("👍")); Before function getHighestRole(Collection $roles): ?Role
{
return array_reduce($roles->toArray(), fn($prev, $role) =>
($prev === null
? $role
: ($this->comparePositionTo($role, $prev) > 0
? $role
: $prev))
);
} After function getHighestRole(Collection $roles): ?Role
{
return $roles->reduce(fn($prev, $role) =>
($prev === null
? $role
: ($this->comparePositionTo($role, $prev) > 0
? $role
: $prev)))
->shift();
} Before blic function setRoles(
Member $member,
Collection|array|Role|string|int $add_roles = [],
Collection|array|Role|string|int $remove_roles = []
): PromiseInterface
{
if (! $add_roles = self::__rolesToIdArray($add_roles )) return resolve($member);
if (! $remove_roles = self::__rolesToIdArray($remove_roles)) return resolve($member);
foreach ($add_roles as &$role_id) if ( $member->roles->has($role_id)) unset($role_id);
foreach ($remove_roles as &$role_id) if (! $member->roles->has($role_id)) unset($role_id);
return ($updated_roles =
array_diff(
array_merge(
array_values(
$member->roles
->map(fn($role) => $role->id)->toArray()
), $add_roles),
$remove_roles
)
) ? $member->setRoles($updated_roles);
: resolve($member);
} After public function setRoles(
Member $member,
Collection|array|Role|string|int $add_roles = [],
Collection|array|Role|string|int $remove_roles = []
): PromiseInterface
{
if (! $add_roles = self::__rolesToIdArray($add_roles )) return resolve($member);
if (! $remove_roles = self::__rolesToIdArray($remove_roles)) return resolve($member);
foreach ($add_roles as &$role_id) if ( $member->roles->has($role_id)) unset($role_id);
foreach ($remove_roles as &$role_id) if (! $member->roles->has($role_id)) unset($role_id);
return ($updated_roles = $member->roles
->map(static fn($role) => $role->id)
//->values()
->merge($add_roles)
->diff($remove_roles))
? $member->setRoles($updated_roles)
: resolve($member);
} |
I'm merging this now as this PR has been thoroughly tested. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request includes several changes to the
CollectionInterface
andCollectionTrait
in theDiscord/Helpers
directory, as well as updates to various attributes in theChannel
,Message
,Embed
, andAuditLog
classes. The primary focus is on enhancing the functionality of the collection interface and traits, and updating the return types of attributes to useCollectionInterface
instead ofCollection
.Collection Interface Enhancements:
shift()
,walk(callable $callback, mixed $arg)
,reduce(callable $callback, $initial = null)
,collect()
,keys()
,values()
,diff($items, ?callable $callback)
,intersect($items, ?callable $callback)
, andunique(int $flags = SORT_STRING)
toCollectionInterface
. [1] [2] [3]Collection Trait Enhancements:
shift()
method to remove and return the first item of the collection.fill()
method to accept both arrays andCollectionInterface
instances.diff()
,intersect()
,walk()
,reduce()
,unique()
,collect()
,keys()
, andvalues()
to theCollectionTrait
. [1] [2] [3] [4] [5] [6]__unserialize()
method to accept both arrays andCollectionInterface
instances.Attribute Return Type Updates:
Collection
toCollectionInterface
inChannel
,Message
,Embed
, andAuditLog
classes. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16]