From e26f123955a14cdd13fa304fe9688d9372806102 Mon Sep 17 00:00:00 2001 From: Exanlv Date: Sat, 4 Feb 2023 20:07:43 +0100 Subject: [PATCH] Add update presence & request guild members --- src/Discord.php | 76 ++++++++++++++++++- src/Enums/Gateway/StatusType.php | 12 +++ src/Websocket/Helpers/ActivityBuilder.php | 11 +++ tests/Discord/DiscordTestCase.php | 8 +- tests/Discord/HandlesHeartbeatTest.php | 2 +- tests/Discord/RequestGuildMembersTest.php | 90 +++++++++++++++++++++++ tests/Discord/UpdatePresenceTest.php | 44 +++++++++++ 7 files changed, 236 insertions(+), 7 deletions(-) create mode 100644 src/Enums/Gateway/StatusType.php create mode 100644 src/Websocket/Helpers/ActivityBuilder.php create mode 100644 tests/Discord/RequestGuildMembersTest.php create mode 100644 tests/Discord/UpdatePresenceTest.php diff --git a/src/Discord.php b/src/Discord.php index 23d4e408..0d0a9ec0 100644 --- a/src/Discord.php +++ b/src/Discord.php @@ -8,7 +8,9 @@ use Discord\Http\Http; use Exan\Dhp\Const\Events as Events; use Exan\Dhp\Const\WebsocketEvents; +use Exan\Dhp\Enums\Gateway\StatusType; use Exan\Dhp\Rest\Rest; +use Exan\Dhp\Websocket\Helpers\ActivityBuilder; use Exan\Dhp\Websocket\Objects\D\Hello; use Exan\Dhp\Websocket\Objects\Payload; use JsonMapper; @@ -225,9 +227,9 @@ private function handleEvent(Payload $payload) $this->events->handle($payload); } - private function sendPayload(array $data) + private function sendPayload(array $data, bool $useBucket = true) { - $this->websocket->send(json_encode($data)); + $this->websocket->send(json_encode($data), $useBucket); } private function startHeartbeat() @@ -250,4 +252,74 @@ private function stopHeartbeat() unset($this->heartbeatTimer); } } + + /** + * @see https://discord.com/developers/docs/topics/gateway-events#update-presence + */ + public function updatePresence( + StatusType $status, + array $activities, + bool $afk = false, + ?int $since = null + ): void { + $presenceUpdate = [ + 'status' => $status->value, + 'activities' => array_map(fn (ActivityBuilder $builder) => $builder->get(), $activities), + 'afk' => $afk, + ]; + + if (!is_null($since)) { + $presenceUpdate['since'] = $since; + } + + $this->sendPayload($presenceUpdate); + } + + /** + * @see https://discord.com/developers/docs/topics/gateway-events#request-guild-members + */ + public function requestGuildMembersByQuery( + string $guildId, + string $query = '', + int $limit = 0, + bool $presences = false, + ?string $nonce = null + ): void { + $guildMemberRequest = [ + 'guild_id' => $guildId, + 'query' => $query, + 'limit' => $limit, + 'presences' => $presences, + ]; + + if (!is_null($nonce)) { + $guildMemberRequest['nonce'] = $nonce; + } + + $this->sendPayload($guildMemberRequest); + } + + /** + * @see https://discord.com/developers/docs/topics/gateway-events#request-guild-members + */ + public function requestGuildMembersByUserIds( + string $guildId, + array $userIds, + int $limit = 0, + bool $presences = false, + ?string $nonce = null + ): void { + $guildMemberRequest = [ + 'guild_id' => $guildId, + 'user_ids' => $userIds, + 'limit' => $limit, + 'presences' => $presences, + ]; + + if (!is_null($nonce)) { + $guildMemberRequest['nonce'] = $nonce; + } + + $this->sendPayload($guildMemberRequest); + } } diff --git a/src/Enums/Gateway/StatusType.php b/src/Enums/Gateway/StatusType.php new file mode 100644 index 00000000..8694c60a --- /dev/null +++ b/src/Enums/Gateway/StatusType.php @@ -0,0 +1,12 @@ +websocketHandlers[WebsocketEvents::MESSAGE])($messageMock); } - protected function assertMessageSent(array $message) + protected function assertMessageSent(array $message, bool $useBucket = true) { - $this->discord->websocket->shouldHaveReceived('send', [json_encode($message)]); + $this->discord->websocket->shouldHaveReceived('send', [json_encode($message), $useBucket]); } - protected function assertMessageNotSent(array $message) + protected function assertMessageNotSent(array $message, bool $useBucket = true) { - $this->discord->websocket->shouldNotHaveReceived('send', [json_encode($message)]); + $this->discord->websocket->shouldNotHaveReceived('send', [json_encode($message), $useBucket]); } } diff --git a/tests/Discord/HandlesHeartbeatTest.php b/tests/Discord/HandlesHeartbeatTest.php index 74f0337f..90c370cb 100644 --- a/tests/Discord/HandlesHeartbeatTest.php +++ b/tests/Discord/HandlesHeartbeatTest.php @@ -58,7 +58,7 @@ public function testHandlesHello() $this->assertMessageSent([ 'op' => 1, 'd' => null - ]); + ], false); } public function testAcknowledgesHearbeat() diff --git a/tests/Discord/RequestGuildMembersTest.php b/tests/Discord/RequestGuildMembersTest.php new file mode 100644 index 00000000..f8c4e650 --- /dev/null +++ b/tests/Discord/RequestGuildMembersTest.php @@ -0,0 +1,90 @@ +discord->requestGuildMembersByQuery( + '::guild id::', + '', + 123, + true + ); + + $this->assertMessageSent( + [ + 'guild_id' => '::guild id::', + 'query' => '', + 'limit' => 123, + 'presences' => true + ] + ); + } + + public function testRequestGuildMembersByQueryWithNonce() + { + $this->discord->requestGuildMembersByQuery( + '::guild id::', + '', + 123, + true, + '::nonce::' + ); + + $this->assertMessageSent( + [ + 'guild_id' => '::guild id::', + 'query' => '', + 'limit' => 123, + 'presences' => true, + 'nonce' => '::nonce::' + ] + ); + } + + public function testRequestGuildMembersByUserIds() + { + $this->discord->requestGuildMembersByUserIds( + '::guild id::', + ['::user id::'], + 123, + true + ); + + $this->assertMessageSent( + [ + 'guild_id' => '::guild id::', + 'user_ids' => ['::user id::'], + 'limit' => 123, + 'presences' => true + ] + ); + } + + public function testRequestGuildMembersByUserIdsWithNonce() + { + $this->discord->requestGuildMembersByUserIds( + '::guild id::', + ['::user id::'], + 123, + true, + '::testRequestGuildMembersByUserIdsWithNonce::' + ); + + $this->assertMessageSent( + [ + 'guild_id' => '::guild id::', + 'user_ids' => ['::user id::'], + 'limit' => 123, + 'presences' => true, + 'nonce' => '::testRequestGuildMembersByUserIdsWithNonce::' + ] + ); + } +} diff --git a/tests/Discord/UpdatePresenceTest.php b/tests/Discord/UpdatePresenceTest.php new file mode 100644 index 00000000..b32fa231 --- /dev/null +++ b/tests/Discord/UpdatePresenceTest.php @@ -0,0 +1,44 @@ +discord->updatePresence( + StatusType::ONLINE, + [new ActivityBuilder()], + ); + + $this->assertMessageSent([ + 'status' => StatusType::ONLINE->value, + 'activities' => [[]], + 'afk' => false, + ]); + } + + public function testUpdatePresenceWithSince() + { + $this->discord->updatePresence( + StatusType::ONLINE, + [new ActivityBuilder()], + since: 12345 + ); + + $this->assertMessageSent([ + 'status' => StatusType::ONLINE->value, + 'activities' => [[]], + 'afk' => false, + 'since' => 12345 + ]); + } +}