Skip to content

Commit

Permalink
🎨 Update PollAnswer::getVoters to be uniform with Reaction::getUsers
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x committed Jul 21, 2024
1 parent 1430c6a commit bdfd48d
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/Discord/Parts/Channel/Poll/PollAnswer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
use Discord\Parts\Thread\Thread;
use Discord\Parts\User\User;
use React\Promise\ExtendedPromiseInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

use function Discord\normalizePartId;

/**
* An answer to a poll.
Expand Down Expand Up @@ -129,30 +132,35 @@ protected function getGuildAttribute(): ?Guild
/**
* Returns the users that voted for the answer.
*
* @param int $limit The maximum number of users to return.
* @param int|null $after The user ID to get users after.
* @param array $options An array of options. All fields are optional.
* @param string|null $options['after'] Get users after this user ID.
* @param int|null $options['limit'] Max number of users to return (1-100).
*
* @link https://discord.com/developers/docs/resources/poll#get-answer-voters
*
* @throws \OutOfRangeException
*
* @return ExtendedPromiseInterface<Collection|User[]>
*/
public function getVoters(int $limit = 25, ?int $after = null): ExtendedPromiseInterface
public function getVoters(array $options = []): ExtendedPromiseInterface
{
if ($limit < 1 || $limit > 100) {
throw new \OutOfRangeException('Limit must be between 1 and 100.');
}
$query = Endpoint::bind(Endpoint::CHANNEL_POLL_ANSWERS, $this->channel_id, $this->message_id, $this->answer_id);

$endpoint = Endpoint::bind(Endpoint::CHANNEL_POLL_ANSWERS, $this->channel_id, $this->message_id, $this->answer_id);
$resolver = new OptionsResolver();
$resolver
->setDefined(['after', 'limit'])
->setAllowedTypes('after', ['int', 'string', User::class])
->setAllowedTypes('limit', 'int')
->setNormalizer('after', normalizePartId())
->setAllowedValues('limit', fn ($value) => ($value >= 1 && $value <= 100));

$endpoint->addQuery('limit', $limit);
$options = $resolver->resolve($options);

if ($after) {
$endpoint->addQuery('after', $after);
foreach ($options as $key => $value) {
$query->addQuery($key, $value);
}

return $this->http->get($endpoint)
return $this->http->get($query)
->then(function ($response) {
$users = Collection::for(User::class);

Expand Down

0 comments on commit bdfd48d

Please sign in to comment.