diff --git a/docs/pages/interacts_with_discord.mdx b/docs/pages/interacts_with_discord.mdx index c735105..7ae3a74 100644 --- a/docs/pages/interacts_with_discord.mdx +++ b/docs/pages/interacts_with_discord.mdx @@ -92,6 +92,14 @@ try { */ ``` +### Parameters + + The `withCounts` parameter is optional. + +| Parameter | Type | Description | +|:--------------|:-------|:-------------------------------| +| withCounts | bool | Include member counts | + ### Example ```php showLineNumbers use \Illuminate\Support\Facades\Log; @@ -99,7 +107,7 @@ use \Illuminate\Support\Facades\Log; $user = auth()->user(); try { - $guilds = $user->getGuilds(); + $guilds = $user->getGuilds(true); Log::info(json_encode($guilds->first())); /* @@ -113,7 +121,9 @@ try { "ANIMATED_ICON", "INVITE_SPLASH" ], - "permissions_new": "110917634608832" + "permissions_new": "110917634608832", + "approximate_member_count": 425, + "approximate_presence_count": 312 } */ } catch (\Exception $exception) { @@ -211,4 +221,4 @@ try { } catch (\Exception $exception) { Log::error('Something went wrong.'); } -``` \ No newline at end of file +``` diff --git a/src/Services/DiscordService.php b/src/Services/DiscordService.php index 9672f40..d6945da 100644 --- a/src/Services/DiscordService.php +++ b/src/Services/DiscordService.php @@ -99,11 +99,17 @@ public function getCurrentUser(AccessToken $accessToken): \Jakyeru\Larascord\Typ * @throws RequestException * @throws Exception */ - public function getCurrentUserGuilds(AccessToken $accessToken): array + public function getCurrentUserGuilds(AccessToken $accessToken, bool $withCounts = false): array { if (!$accessToken->hasScope('guilds')) throw new Exception(config('larascord.error_messages.missing_guilds_scope.message')); - $response = Http::withToken($accessToken->access_token, $accessToken->token_type)->get($this->baseApi . '/users/@me/guilds'); + $endpoint = '/users/@me/guilds'; + + if ($withCounts) { + $endpoint .= '?with_counts=true'; + } + + $response = Http::withToken($accessToken->access_token, $accessToken->token_type)->get($this->baseApi . $endpoint); $response->throw(); @@ -241,4 +247,4 @@ public function revokeAccessToken(string $accessToken): object return json_decode($response->body()); } -} \ No newline at end of file +} diff --git a/src/Traits/InteractsWithDiscord.php b/src/Traits/InteractsWithDiscord.php index fe87ddf..9f885a9 100644 --- a/src/Traits/InteractsWithDiscord.php +++ b/src/Traits/InteractsWithDiscord.php @@ -81,7 +81,7 @@ public function refreshAccessToken(): ?AccessToken * @throws RequestException * @throws Exception */ - public function getGuilds(): Collection + public function getGuilds(bool $withCounts = false): Collection { $accessToken = $this->getAccessToken(); @@ -89,7 +89,7 @@ public function getGuilds(): Collection throw new Exception('The access token is invalid.'); } - $response = (new DiscordService())->getCurrentUserGuilds($accessToken); + $response = (new DiscordService())->getCurrentUserGuilds($accessToken, $withCounts); return collect($response); } @@ -148,4 +148,4 @@ public function getConnections(): Collection return collect($response); } -} \ No newline at end of file +} diff --git a/src/Types/Guild.php b/src/Types/Guild.php index a53df10..949fcc2 100644 --- a/src/Types/Guild.php +++ b/src/Types/Guild.php @@ -41,6 +41,16 @@ class Guild */ public string $permissions_new; + /* + * The approximate count of members in the guild. + */ + public ?int $approximate_member_count; + + /* + * The approximate count of active members in the guild. + */ + public ?int $approximate_presence_count; + /* * Guild constructor. */ @@ -53,5 +63,7 @@ public function __construct(object $data) $this->permissions = $data->permissions; $this->features = $data->features; $this->permissions_new = $data->permissions_new; + $this->approximate_member_count = $data->approximate_member_count ?? null; + $this->approximate_presence_count = $data->approximate_presence_count ?? null; } -} \ No newline at end of file +}