Skip to content

Commit

Permalink
Merge pull request #114 from DamoFD/feat/member-counts
Browse files Browse the repository at this point in the history
Feat: Guild Member Counts Closes #112
  • Loading branch information
JakyeRU authored Aug 7, 2023
2 parents c2f5c3e + 6c13dd9 commit e7f3685
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
16 changes: 13 additions & 3 deletions docs/pages/interacts_with_discord.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,22 @@ try {
*/
```

### Parameters
<Callout type="info">
The `withCounts` parameter is optional.
</Callout>
| Parameter | Type | Description |
|:--------------|:-------|:-------------------------------|
| withCounts | bool | Include member counts |

### Example
```php showLineNumbers
use \Illuminate\Support\Facades\Log;

$user = auth()->user();

try {
$guilds = $user->getGuilds();
$guilds = $user->getGuilds(true);

Log::info(json_encode($guilds->first()));
/*
Expand All @@ -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) {
Expand Down Expand Up @@ -211,4 +221,4 @@ try {
} catch (\Exception $exception) {
Log::error('Something went wrong.');
}
```
```
12 changes: 9 additions & 3 deletions src/Services/DiscordService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -241,4 +247,4 @@ public function revokeAccessToken(string $accessToken): object

return json_decode($response->body());
}
}
}
6 changes: 3 additions & 3 deletions src/Traits/InteractsWithDiscord.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ public function refreshAccessToken(): ?AccessToken
* @throws RequestException
* @throws Exception
*/
public function getGuilds(): Collection
public function getGuilds(bool $withCounts = false): Collection
{
$accessToken = $this->getAccessToken();

if (!$accessToken) {
throw new Exception('The access token is invalid.');
}

$response = (new DiscordService())->getCurrentUserGuilds($accessToken);
$response = (new DiscordService())->getCurrentUserGuilds($accessToken, $withCounts);

return collect($response);
}
Expand Down Expand Up @@ -148,4 +148,4 @@ public function getConnections(): Collection

return collect($response);
}
}
}
14 changes: 13 additions & 1 deletion src/Types/Guild.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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;
}
}
}

1 comment on commit e7f3685

@vercel
Copy link

@vercel vercel bot commented on e7f3685 Aug 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.