Skip to content

Commit

Permalink
Merge pull request #105 from JakyeRU/add-get-guildmember
Browse files Browse the repository at this point in the history
Add getGuildMember method to InteractsWithDiscord Trait
  • Loading branch information
JakyeRU authored Jul 27, 2023
2 parents ada377c + f2faeca commit 26c9b3a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
41 changes: 41 additions & 0 deletions docs/pages/interacts_with_discord.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,47 @@ try {
}
```

## getGuildMember
<Callout type="warning">
This method will make a request to Discord's API. It is recommended to cache the response.
</Callout>
```php copy=false
/**
* Returns the user's guild member from Discord's API.
*
* @return Jakyeru\Larascord\Types\GuildMember|null
* @throws Illuminate\Http\Client\RequestException
* @throws Exception
*/
```

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

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

try {
$guildMember = $user->getGuildMember('123456789012345678');

Log::info(json_encode($guildMember));
/*
{
"avatar": null,
"joined_at": "2023-01-04T09:26:07.172000Z",
"nick": "My Server Nickname",
"roles": [
"1082025544411000832"
],
"deaf": false,
"mute": false
}
*/
} catch (\Exception $exception) {
Log::error('Something went wrong.');
}
```

## joinGuild
<Callout type="warning">
The bot's access token must be set in `.env` as `LARASCORD_ACCESS_TOKEN`.
Expand Down
2 changes: 1 addition & 1 deletion src/LarascordServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class LarascordServiceProvider extends ServiceProvider
*
* @var string
*/
const VERSION = '5.0.4';
const VERSION = '5.0.5';

/*
* Register the application services.
Expand Down
19 changes: 19 additions & 0 deletions src/Traits/InteractsWithDiscord.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ public function getGuilds(): Collection
return collect($response);
}

/**
* Get the user's guild member.
*
* @throws RequestException
* @throws Exception
*/
public function getGuildMember(string $guildId): GuildMember|null
{
$accessToken = $this->getAccessToken();

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

$response = (new DiscordService())->getGuildMember($accessToken, $guildId);

return new GuildMember($response);
}

/**
* Join a guild.
*
Expand Down

1 comment on commit 26c9b3a

@vercel
Copy link

@vercel vercel bot commented on 26c9b3a Jul 27, 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.