What is InteractsWithDiscord Trait?
The InteractsWithDiscord.php
trait adds useful methods to the User
model that can be used to interact with Discord
's API.
getTagAttribute
This method will return the user's tag in the format of username#discriminator
. If the user migrated to the new username system, it will return the user's display name, for example Jakye
.
/**
* Get the user's tag attribute.
*
* @return string
*/
accessToken
/**
* Returns the user's access token relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
getAccessToken
This method will try getting a new access_token
using the refresh_token
if the current access_token
is expired.
/**
* Returns the user's access token.
*
* @return Jakyeru\Larascord\Types\AccessToken|null
*/
getConnections
This method will make a request to Discord's API. It is recommended to cache the response.
/**
* Get the user's connections.
*
* @return Illuminate\Support\Collection
* @throws Illuminate\Http\Client\RequestException
* @throws Exception
*/
Example
use \Illuminate\Support\Facades\Log;
$user = auth()->user();
try {
$connections = $user->getConnections();
Log::info(json_encode($connections->first()));
/*
{
"type": "github",
"id": "36800842",
"name": "JakyeRU",
"visibility": true,
"friend_sync": false,
"show_activity": false,
"verified": true,
"two_way_link": false,
"metadata_visibility": 1
}
*/
} catch (\Exception $exception) {
Log::error('Something went wrong.');
}
getGuilds
This method will make a request to Discord's API. It is recommended to cache the response.
/**
* Returns the user's guilds from Discord's API.
*
* @return Illuminate\Support\Collection
* @throws Illuminate\Http\Client\RequestException
* @throws Exception
*/
Example
use \Illuminate\Support\Facades\Log;
$user = auth()->user();
try {
$guilds = $user->getGuilds();
Log::info(json_encode($guilds->first()));
/*
{
"id": "81384788765712384",
"name": "Discord API",
"icon": "a363a84e969bcbe1353eb2fdfb2e50e6",
"owner": false,
"permissions": 104189632,
"features": [
"ANIMATED_ICON",
"INVITE_SPLASH"
],
"permissions_new": "110917634608832"
}
*/
} catch (\Exception $exception) {
Log::error('Something went wrong.');
}
joinGuild
The bot's access token must be set in .env
as LARASCORD_ACCESS_TOKEN
.
The bot must belong to the same application used for authorization and must be a member of the guild with CREATE_INSTANT_INVITE
permission.
The OAuth2 access token must have the guilds.join
scope.
/**
* Join a guild.
*
* @return Jakyeru\Larascord\Types\GuildMember|null
* @throws Illuminate\Http\Client\RequestException
* @throws Exception
*/
Parameters
The options
parameter is optional.
Parameter | Type | Description |
---|---|---|
guildId | string | The guild ID. |
options | array | The options. |
options.nick | string | The nickname to give the user. |
options.roles | array | The roles to give the user. |
options.mute | bool | Whether to mute the user. |
options.deaf | bool | Whether to deafen the user. |
Example
use \Illuminate\Support\Facades\Log;
$user = auth()->user();
try {
$guildMember = $user->joinGuild('81384788765712384', [
'roles' => ['81384788765712384'],
'nick' => 'Test'
]);
Log::info('Joined guild.');
} catch (\Exception $exception) {
Log::error('Something went wrong.');
}