|
16 | 16 |
|
17 | 17 | use Discord\Http\Endpoint; |
18 | 18 | use Discord\Parts\Guild\ScheduledEvent; |
| 19 | +use Discord\Parts\User\User; |
19 | 20 | use Discord\Repository\AbstractRepository; |
20 | 21 | use React\Promise\PromiseInterface; |
| 22 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
21 | 23 |
|
22 | 24 | use function React\Promise\resolve; |
23 | 25 |
|
@@ -85,4 +87,79 @@ public function fetch(string $id, bool $fresh = false, bool $with_user_count = f |
85 | 87 | return $this->cache->set($id, $part)->then(fn ($success) => $part); |
86 | 88 | }); |
87 | 89 | } |
| 90 | + |
| 91 | + /** |
| 92 | + * Get the counts for users subscribed to a scheduled event. |
| 93 | + * |
| 94 | + * @param string $id The scheduled event id. |
| 95 | + * |
| 96 | + * @return PromiseInterface<int> The count of users subscribed to the scheduled event. |
| 97 | + * |
| 98 | + * @since 10.46.0 |
| 99 | + */ |
| 100 | + public function getUsersCount(string $id): PromiseInterface |
| 101 | + { |
| 102 | + return $this->http->get(Endpoint::bind(Endpoint::GUILD_SCHEDULED_EVENT_USERS_COUNT, $this->vars['guild_id'], $id)); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Get users for a specific scheduled event exception. |
| 107 | + * |
| 108 | + * Mirrors the query options of ScheduledEvent::getUsers. |
| 109 | + * |
| 110 | + * @param string $scheduledEventId |
| 111 | + * @param string $exceptionId |
| 112 | + * @param array $options |
| 113 | + * |
| 114 | + * @return PromiseInterface<ExCollectionInterface<User>> |
| 115 | + * |
| 116 | + * @since 10.46.0 |
| 117 | + */ |
| 118 | + public function getExceptionUsers(string $scheduledEventId, string $exceptionId, array $options = []): PromiseInterface |
| 119 | + { |
| 120 | + $resolver = new OptionsResolver(); |
| 121 | + $resolver->setDefaults(['limit' => 100, 'with_member' => false]); |
| 122 | + $resolver->setDefined(['before', 'after']); |
| 123 | + $resolver->setAllowedTypes('before', [User::class, 'string']); |
| 124 | + $resolver->setAllowedTypes('after', [User::class, 'string']); |
| 125 | + $resolver->setAllowedTypes('with_member', 'bool'); |
| 126 | + $resolver->setAllowedValues('limit', fn ($value) => ($value >= 1 && $value <= 100)); |
| 127 | + |
| 128 | + $options = $resolver->resolve($options); |
| 129 | + if (isset($options['before'], $options['after'])) { |
| 130 | + return \React\Promise\reject(new \RangeException('Can only specify one of before after.')); |
| 131 | + } |
| 132 | + |
| 133 | + $endpoint = Endpoint::bind(Endpoint::GUILD_SCHEDULED_EVENT_EXCEPTION_USERS, $this->vars['guild_id'], $scheduledEventId, $exceptionId); |
| 134 | + $endpoint->addQuery('limit', $options['limit']); |
| 135 | + $endpoint->addQuery('with_member', $options['with_member']); |
| 136 | + |
| 137 | + if (isset($options['before'])) { |
| 138 | + $endpoint->addQuery('before', $options['before'] instanceof User ? $options['before']->id : $options['before']); |
| 139 | + } |
| 140 | + if (isset($options['after'])) { |
| 141 | + $endpoint->addQuery('after', $options['after'] instanceof User ? $options['after']->id : $options['after']); |
| 142 | + } |
| 143 | + |
| 144 | + return $this->http->get($endpoint)->then(function ($responses) { |
| 145 | + /** @var ExCollectionInterface<User> $users */ |
| 146 | + $users = new ($this->discord->getCollectionClass()); |
| 147 | + |
| 148 | + $guild = $this->discord->guilds->get('id', $this->vars['guild_id']); |
| 149 | + |
| 150 | + foreach ($responses as $response) { |
| 151 | + if (isset($response->member) && ! $user = $guild->members->get('id', $response->user->id)) { |
| 152 | + $user = $guild->members->create($response->member, true); |
| 153 | + $guild->members->pushItem($user); |
| 154 | + } elseif (! $user = $this->discord->users->get('id', $response->user->id)) { |
| 155 | + $user = $this->discord->users->create($response->user, true); |
| 156 | + $this->discord->users->pushItem($user); |
| 157 | + } |
| 158 | + |
| 159 | + $users->pushItem($user); |
| 160 | + } |
| 161 | + |
| 162 | + return $users; |
| 163 | + }); |
| 164 | + } |
88 | 165 | } |
0 commit comments