DiscordPHP Documentation

Session
in package

Acts as one user, with their own OAuth2 token rather than the bot's.

This is how a game's backend does what the Social SDK does for a player: joining a lobby, chatting in it, linking a channel. Sessions come from SessionManager — $discord->sessions — which opens, stores, resumes and refreshes them.

Each session has its own HTTP client, because Discord rate-limits each token separately, but shares the bot's driver and connection pool.

Tags
since
10.59.0

Table of Contents

Properties

$discord  : Discord
$http  : Http
$key  : ?string
$lobbies  : SessionLobbyRepository
$token  : AccessToken

Methods

__construct()  : mixed
__debugInfo()  : array<string, mixed>
__get()  : mixed
Exposes the read-only `lobbies` repository.
getApplicationRoleConnection()  : PromiseInterface<string|int, ApplicationRoleConnection>
Returns the user's role connection for an application. Requires the `role_connections.write` scope.
getAuthorization()  : PromiseInterface<string|int, Authorization>
Returns what the user has authorised: the application, the scopes, when the token expires, and the user, when the token has the `identify` scope.
getChannelMessages()  : PromiseInterface<string|int, ExCollectionInterface<string|int, Message>|array<string|int, Message>>
Returns a channel's message history as the user, newest first.
getConnections()  : PromiseInterface<string|int, ExCollectionInterface<string|int, Connection>|array<string|int, Connection>>
Returns the user's connected accounts. Requires the `connections` scope.
getCurrentUser()  : PromiseInterface<string|int, User>
Returns the user the token belongs to.
getEntitlements()  : PromiseInterface<string|int, ExCollectionInterface<string|int, Entitlement>|array<string|int, Entitlement>>
Returns the user's entitlements for an application.
getHttpClient()  : Http
The HTTP client that sends the user's token.
getKey()  : ?string
What the token is stored under, or null if it is not stored.
getToken()  : AccessToken
The user's token.
getUserInfo()  : PromiseInterface<string|int, UserInfo>
Returns the user as OpenID Connect describes them. Requires the `openid` scope; the email claims also need the `email` scope.
searchGuildMessages()  : PromiseInterface<string|int, GuildSearch>
Searches a guild's messages as the user.
setCommandPermissions()  : PromiseInterface<string|int, CommandPermissions>
Sets who can use one of an application's commands in a guild, replacing the permissions it had there.
updateApplicationRoleConnection()  : PromiseInterface<string|int, ApplicationRoleConnection>
Updates the user's role connection for an application. Requires the `role_connections.write` scope.
clientFor()  : Http
A client that sends this token, sharing the bot's driver.
collect()  : ExCollectionInterface
Hydrates a list of parts into a collection.
forApplication()  : PromiseInterface
Runs a request for an application given as a part or an id, or for the bot's own.

Properties

Methods

__construct()

public __construct(Discord $discord, AccessToken $token[, string|null $key = null ]) : mixed
Parameters
$discord : Discord

The client.

$token : AccessToken

The user's token.

$key : string|null = null

What the token is stored under, if it is stored.

__debugInfo()

public __debugInfo() : array<string, mixed>
Return values
array<string, mixed>

__get()

Exposes the read-only `lobbies` repository.

public __get(string $name) : mixed
Parameters
$name : string

getApplicationRoleConnection()

Returns the user's role connection for an application. Requires the `role_connections.write` scope.

public getApplicationRoleConnection([Application|string|null $application = null ]) : PromiseInterface<string|int, ApplicationRoleConnection>
Parameters
$application : Application|string|null = null

The application or its id; the bot's own if omitted.

Tags
link
https://docs.discord.com/developers/resources/user#get-current-user-application-role-connection
Return values
PromiseInterface<string|int, ApplicationRoleConnection>

getChannelMessages()

Returns a channel's message history as the user, newest first.

public getChannelMessages(Channel|string $channel[, int|null $options = [] ]) : PromiseInterface<string|int, ExCollectionInterface<string|int, Message>|array<string|int, Message>>

Discord accepts the user's token here when it has the messages.read scope and the user can read the channel's history.

Parameters
$channel : Channel|string

The channel or its id.

$options : int|null = []

['limit'] Max number of messages to return (1-100). Defaults to 50.

Tags
link
https://docs.discord.com/developers/resources/channel#get-channel-messages
since
10.66.2
Return values
PromiseInterface<string|int, ExCollectionInterface<string|int, Message>|array<string|int, Message>>

getEntitlements()

Returns the user's entitlements for an application.

public getEntitlements([Application|string|null $application = null ]) : PromiseInterface<string|int, ExCollectionInterface<string|int, Entitlement>|array<string|int, Entitlement>>
Parameters
$application : Application|string|null = null

The application or its id; the bot's own if omitted.

Tags
link
https://docs.discord.com/developers/resources/entitlement
Return values
PromiseInterface<string|int, ExCollectionInterface<string|int, Entitlement>|array<string|int, Entitlement>>

getHttpClient()

The HTTP client that sends the user's token.

public getHttpClient() : Http
Return values
Http

getKey()

What the token is stored under, or null if it is not stored.

public getKey() : ?string
Return values
?string

searchGuildMessages()

Searches a guild's messages as the user.

public searchGuildMessages(Guild|string $guild, array<string|int, mixed> $queryparams) : PromiseInterface<string|int, GuildSearch>

Discord's OpenAPI description accepts a user's token here as well as a bot's, without naming the scope it needs. The user needs the read_message_history permission in the channels searched. Takes the same query parameters as the guild's messages repository.

Parameters
$guild : Guild|string

The guild or its id.

$queryparams : array<string|int, mixed>

Query string params to add to the request, at least one (no validation).

Tags
link
https://docs.discord.com/developers/resources/message#search-guild-messages
see
MessageRepository::freshen()

for the query parameters.

since
10.67.0
Return values
PromiseInterface<string|int, GuildSearch>

setCommandPermissions()

Sets who can use one of an application's commands in a guild, replacing the permissions it had there.

public setCommandPermissions(Guild|string $guild, Command|string $command, array<string|int, Permission|array<string|int, mixed>> $permissions[, Application|string|null $application = null ]) : PromiseInterface<string|int, CommandPermissions>

Discord only accepts this with a user's token that has the applications.commands.permissions.update scope, never a bot's. The user needs the manage_guild and manage_roles permissions in the guild, and must be able to use the command themselves.

Parameters
$guild : Guild|string

The guild.

$command : Command|string

The command or its ID; the application's ID sets the default for all its commands.

$permissions : array<string|int, Permission|array<string|int, mixed>>

At most 100, each with id, type and permission.

$application : Application|string|null = null

The application or its id; the bot's own if omitted.

Tags
link
https://docs.discord.com/developers/interactions/application-commands#edit-application-command-permissions
since
10.60.0
Return values
PromiseInterface<string|int, CommandPermissions>

updateApplicationRoleConnection()

Updates the user's role connection for an application. Requires the `role_connections.write` scope.

public updateApplicationRoleConnection(?array<string|int, mixed> $connection[, Application|string|null $application = null ]) : PromiseInterface<string|int, ApplicationRoleConnection>
Parameters
$connection : ?array<string|int, mixed>

['metadata'] Metadata keys mapped to their string values (max 100 characters each).

$application : Application|string|null = null

The application or its id; the bot's own if omitted.

Tags
link
https://docs.discord.com/developers/resources/user#update-current-user-application-role-connection
Return values
PromiseInterface<string|int, ApplicationRoleConnection>

clientFor()

A client that sends this token, sharing the bot's driver.

protected clientFor(AccessToken $token) : Http
Parameters
$token : AccessToken
Return values
Http

collect()

Hydrates a list of parts into a collection.

protected collect(string $class, object|array<string|int, mixed> $response) : ExCollectionInterface
Parameters
$class : string
$response : object|array<string|int, mixed>
Return values
ExCollectionInterface

forApplication()

Runs a request for an application given as a part or an id, or for the bot's own.

protected forApplication(Application|string|null $application, callable(string): PromiseInterface $request) : PromiseInterface
Parameters
$application : Application|string|null
$request : callable(string): PromiseInterface

Given the application id.

Return values
PromiseInterface
On this page

Search results