Discord
in package
uses
EventEmitterTrait
The Discord client class.
Tags
Table of Contents
Constants
- GATEWAY_VERSION = 10
- The gateway version the client uses.
- REFERRER = 'https://github.com/discord-php/DiscordPHP'
- VERSION = 'v10.39.0'
- The client version.
Properties
- $application : Application
- $avatar : string
- $avatar_hash : string
- $bot : bool
- $discriminator : string
- $email : string
- $emojis : EmojiRepository
- $guilds : GuildRepository
- $id : string
- $lobbies : LobbyRepository
- $password : string
- $private_channels : PrivateChannelRepository
- $sounds : SoundRepository
- $user : User
- $username : string
- $users : UserRepository
- $verified : bool
- $application_commands : array<string|int, RegisteredCommand>
- An array of registered slash commands.
- $cacheConfig : array<string|int, CacheConfig>
- The cache configuration.
- $client : Client
- The Client class.
- $closing : bool
- Whether the client is closing.
- $connected : bool
- Whether the client is connected to the gateway.
- $emittedInit : bool
- Whether `init` has been emitted.
- $encoding : string
- What encoding the client will use, either `json` or `etf`.
- $factory : Factory
- The part/repository factory.
- $gateway : string
- The gateway URL that the WebSocket client will connect to.
- $handlers : Handlers
- The event handlers.
- $heartbeatAckTimer : TimerInterface
- The timer that resends the heartbeat packet if a HEARTBEAT_ACK packet is not received in 5 seconds.
- $heartbeatInterval : int
- The heartbeat interval.
- $heartbeatTime : int
- The time that the last heartbeat packet was sent.
- $heartbeatTimer : TimerInterface
- The timer that sends the heartbeat packet.
- $http : Http
- The HTTP client.
- $largeGuilds : array<string|int, mixed>
- An array of large guilds that need to be requested for members.
- $largeSent : array<string|int, mixed>
- An array of large guilds that have been requested for members.
- $logger : LoggerInterface
- The logger.
- $loop : LoopInterface
- The ReactPHP event loop.
- $options : array<string|int, mixed>
- An array of options passed to the client.
- $payloadBuffer : string
- Gateway compressed message payload buffer.
- $payloadCount : int
- Tracks the number of payloads the client has sent in the past 60 seconds.
- $payloadTimer : TimerInterface
- Payload count reset timer.
- $reconnectCount : int
- How many times the client has reconnected.
- $reconnecting : bool
- Whether the client is currently reconnecting.
- $regions : ExCollectionInterface<string|int, Region>|null
- An array of valid regions.
- $resume_gateway_url : string
- The resume_gateway_url that the WebSocket client will reconnect to.
- $seq : int
- The packet sequence that the client is up to.
- $sessionId : string
- The session ID of the current session.
- $token : string
- The authentication token.
- $unparsedPackets : array<string|int, mixed>
- An array of unparsed packets.
- $usePayloadCompression : bool
- The payload compression setting.
- $useTransportCompression : bool
- The transport compression setting.
- $voice_sessions : array<string|int, string>
- An array of voice session IDs.
- $voiceClients : array<string|int, VoiceClient>
- An array of voice clients that are currently connected.
- $voiceLoggers : array<string|int, LoggerInterface>|null
- An array of loggers for voice clients.
- $ws : WebSocket
- The WebSocket instance.
- $wsFactory : Connector
- The WebSocket client factory.
- $zlibDecompressor : InflateContext|false
- zlib decompressor.
Methods
- __call() : mixed
- Handles dynamic calls to the client.
- __construct() : mixed
- Creates a Discord client instance.
- __debugInfo() : array<string|int, mixed>
- Returns an array that can be used to describe the internal state of this object.
- __get() : mixed
- Handles dynamic get calls to the client.
- __set() : void
- Handles dynamic set calls to the client.
- addLargeGuild() : void
- Adds a large guild to the large guild array.
- close() : void
- Closes the Discord client.
- connectWs() : void
- Initializes the connection with the Discord gateway.
- factory() : Part|AbstractRepository
- Allows access to the part/repository factory.
- getCacheConfig() : CacheConfig|null
- Gets the cache configuration.
- getChannel() : Channel|null
- Gets a cached channel.
- getFactory() : Factory
- Gets the factory.
- getHttp() : Http
- Gets the HTTP client.
- getHttpClient() : Http
- Gets the HTTP client.
- getLogger() : LoggerInterface
- Gets the logger being used.
- getLoop() : LoopInterface
- Gets the loop being used by the client.
- getVoiceClient() : VoiceClient|null
- Gets a voice client from a guild ID. Returns null if there is no voice client.
- handleWsClose() : void
- Handles WebSocket closes received by the client.
- handleWsConnection() : void
- Handles WebSocket connections received by the client.
- handleWsConnectionFailed() : void
- Handles cases when the WebSocket cannot be connected to.
- handleWsError() : void
- Handles WebSocket errors received by the client.
- handleWsMessage() : void
- Handles WebSocket messages received by the client.
- heartbeat() : void
- Sends a heartbeat packet to the Discord gateway.
- identify() : void
- Used to trigger the initial handshake with the gateway.
- joinVoiceChannel() : PromiseInterface<string|int, VoiceClient>
- Joins a voice channel.
- listenCommand() : RegisteredCommand
- Add listener for incoming application command from interaction.
- listVoiceRegions() : PromiseInterface<string|int, ExCollectionInterface<string|int, Region>|array<string|int, Region>>
- Lists voice regions.
- requestGuildMembers() : void
- Used to request all members for a guild or a list of guilds.
- requestSoundboardSounds() : void
- Used to request soundboard sounds for a list of guilds. The server will send Soundboard Sounds events for each guild in response.
- resume() : void
- Used to replay missed events when a disconnected client resumes.
- run() : void
- Starts the ReactPHP event loop.
- setClient() : void
- Sets the client instance.
- updatePresence() : void
- Sent by the client to indicate a presence or status update.
- updateVoiceState() : void
- Sent when a client wants to join, move, or disconnect from a voice channel.
- buildParams() : void
- Builds the gateway connection parameters and resolves the deferred with the gateway URL and session information.
- checkForChunks() : void
- Checks for any large guilds that need to be chunked.
- handleDispatch() : void
- Handles dispatch events received by the WebSocket.
- handleGuildMembersChunk() : void
- Handles `GUILD_MEMBERS_CHUNK` packets.
- handleHeartbeat() : void
- Handles heartbeat packets received by the client.
- handleHeartbeatAck() : void
- Handles heartbeat ACK packets received by the client.
- handleHello() : void
- Handles HELLO packets received by the websocket.
- handleInvalidSession() : void
- Handles invalid session packets received by the client.
- handleReady() : false|void
- Handles `READY` packets.
- handleReconnect() : void
- Handles reconnect packets received by the client.
- handleResume() : void
- Handles `RESUME` packets.
- handleVoiceServerUpdate() : void
- Handles `VOICE_SERVER_UPDATE` packets.
- handleVoiceStateUpdate() : void
- Handles `VOICE_STATE_UPDATE` packets.
- processWsMessage() : void
- Process WebSocket message payloads.
- ready() : false|void
- Emits init if it has not been emitted already.
- resolveOptions() : array<string|int, mixed>
- Resolves the options.
- send() : void
- Sends a packet to the Discord gateway.
- sendChunks() : void
- Sends chunks of guild member requests.
- setGateway() : PromiseInterface
- Retrieves and sets the gateway URL for the client.
- setupChunking() : false|void
- Sets guild member chunking up.
- setupHeartbeat() : void
- Sets the heartbeat timer up.
- voiceServerUpdate() : mixed
- voiceStateUpdate() : mixed
Constants
GATEWAY_VERSION
The gateway version the client uses.
public
int
GATEWAY_VERSION
= 10
Gateway version.
REFERRER
public
mixed
REFERRER
= 'https://github.com/discord-php/DiscordPHP'
VERSION
The client version.
public
string
VERSION
= 'v10.39.0'
Version.
Properties
$application
public
Application
$application
The OAuth2 application of the bot.
$avatar
public
string
$avatar
The avatar URL of the client.
$avatar_hash
public
string
$avatar_hash
The avatar hash of the client.
$bot
public
bool
$bot
Whether the client is a bot.
$discriminator
public
string
$discriminator
The unique discriminator of the client.
public
string
$email
The email of the client.
$emojis
public
EmojiRepository
$emojis
$guilds
public
GuildRepository
$guilds
$id
public
string
$id
The unique identifier of the client.
$lobbies
public
LobbyRepository
$lobbies
$password
public
string
$password
The password of the client (if they have provided it).
$private_channels
public
PrivateChannelRepository
$private_channels
$sounds
public
SoundRepository
$sounds
$user
public
User
$user
The user instance of the client.
$username
public
string
$username
The username of the client.
$users
public
UserRepository
$users
$verified
public
bool
$verified
Whether the client has verified their email.
$application_commands
An array of registered slash commands.
protected
array<string|int, RegisteredCommand>
$application_commands
$cacheConfig
The cache configuration.
protected
array<string|int, CacheConfig>
$cacheConfig
$client
The Client class.
protected
Client
$client
Discord client.
$closing
Whether the client is closing.
protected
bool
$closing
= false
Closing.
$connected
Whether the client is connected to the gateway.
protected
bool
$connected
= false
Connected.
$emittedInit
Whether `init` has been emitted.
protected
bool
$emittedInit
= false
Emitted.
$encoding
What encoding the client will use, either `json` or `etf`.
protected
string
$encoding
= 'json'
Encoding.
$factory
The part/repository factory.
protected
Factory
$factory
Part factory.
$gateway
The gateway URL that the WebSocket client will connect to.
protected
string
$gateway
Gateway URL.
$handlers
The event handlers.
protected
Handlers
$handlers
Handlers.
$heartbeatAckTimer
The timer that resends the heartbeat packet if a HEARTBEAT_ACK packet is not received in 5 seconds.
protected
TimerInterface
$heartbeatAckTimer
Timer.
$heartbeatInterval
The heartbeat interval.
protected
int
$heartbeatInterval
Heartbeat interval.
$heartbeatTime
The time that the last heartbeat packet was sent.
protected
int
$heartbeatTime
Epoch time.
$heartbeatTimer
The timer that sends the heartbeat packet.
protected
TimerInterface
$heartbeatTimer
Timer.
$http
The HTTP client.
protected
Http
$http
Client.
$largeGuilds
An array of large guilds that need to be requested for members.
protected
array<string|int, mixed>
$largeGuilds
= []
Large guilds.
$largeSent
An array of large guilds that have been requested for members.
protected
array<string|int, mixed>
$largeSent
= []
Large guilds.
$logger
The logger.
protected
LoggerInterface
$logger
Logger.
$loop
The ReactPHP event loop.
protected
LoopInterface
$loop
Event loop.
$options
An array of options passed to the client.
protected
array<string|int, mixed>
$options
Options.
$payloadBuffer
Gateway compressed message payload buffer.
protected
string
$payloadBuffer
= ''
Buffer.
$payloadCount
Tracks the number of payloads the client has sent in the past 60 seconds.
protected
int
$payloadCount
= 0
$payloadTimer
Payload count reset timer.
protected
TimerInterface
$payloadTimer
$reconnectCount
How many times the client has reconnected.
protected
int
$reconnectCount
= 0
Reconnect count.
$reconnecting
Whether the client is currently reconnecting.
protected
bool
$reconnecting
= false
Reconnecting.
$regions
An array of valid regions.
protected
ExCollectionInterface<string|int, Region>|null
$regions
$resume_gateway_url
The resume_gateway_url that the WebSocket client will reconnect to.
protected
string
$resume_gateway_url
resume_gateway_url URL.
$seq
The packet sequence that the client is up to.
protected
int
$seq
Sequence.
$sessionId
The session ID of the current session.
protected
string
$sessionId
Session ID.
$token
The authentication token.
protected
string
$token
Token.
$unparsedPackets
An array of unparsed packets.
protected
array<string|int, mixed>
$unparsedPackets
= []
Unparsed packets.
$usePayloadCompression
The payload compression setting.
protected
bool
$usePayloadCompression
Whether to use payload compression.
$useTransportCompression
The transport compression setting.
protected
bool
$useTransportCompression
Whether to use transport compression.
$voice_sessions
An array of voice session IDs.
protected
array<string|int, string>
$voice_sessions
= []
Voice Sessions.
$voiceClients
An array of voice clients that are currently connected.
protected
array<string|int, VoiceClient>
$voiceClients
= []
Voice Clients.
$voiceLoggers
An array of loggers for voice clients.
protected
array<string|int, LoggerInterface>|null
$voiceLoggers
= []
Loggers.
$ws
The WebSocket instance.
protected
WebSocket
$ws
Instance.
$wsFactory
The WebSocket client factory.
protected
Connector
$wsFactory
Factory.
$zlibDecompressor
zlib decompressor.
protected
InflateContext|false
$zlibDecompressor
Methods
__call()
Handles dynamic calls to the client.
public
__call(string $name, array<string|int, mixed> $params) : mixed
Parameters
- $name : string
-
Function name.
- $params : array<string|int, mixed>
-
Function parameters.
__construct()
Creates a Discord client instance.
public
__construct([array<string|int, mixed> $options = [] ]) : mixed
Parameters
- $options : array<string|int, mixed> = []
-
Array of options.
Tags
__debugInfo()
Returns an array that can be used to describe the internal state of this object.
public
__debugInfo() : array<string|int, mixed>
Return values
array<string|int, mixed>__get()
Handles dynamic get calls to the client.
public
__get(string $name) : mixed
Parameters
- $name : string
-
Variable name.
__set()
Handles dynamic set calls to the client.
public
__set(string $name, mixed $value) : void
Parameters
- $name : string
-
Variable name.
- $value : mixed
-
Value to set.
addLargeGuild()
Adds a large guild to the large guild array.
public
addLargeGuild(Guild $guild) : void
Parameters
- $guild : Guild
-
The guild.
close()
Closes the Discord client.
public
close([bool $closeLoop = true ]) : void
Parameters
- $closeLoop : bool = true
-
Whether to close the loop as well. Default true.
connectWs()
Initializes the connection with the Discord gateway.
public
connectWs() : void
factory()
Allows access to the part/repository factory.
public
factory(string $class[, mixed $data = [] ][, bool $created = false ]) : Part|AbstractRepository
Parameters
- $class : string
-
The class to build.
- $data : mixed = []
-
Data to create the object.
- $created : bool = false
-
Whether the object is created (if part).
Tags
Return values
Part|AbstractRepositorygetCacheConfig()
Gets the cache configuration.
public
getCacheConfig([string $repository_class = AbstractRepository::class ]) : CacheConfig|null
Parameters
- $repository_class : string = AbstractRepository::class
-
Repository class name.
Return values
CacheConfig|nullgetChannel()
Gets a cached channel.
public
getChannel(string|int $channel_id) : Channel|null
Parameters
- $channel_id : string|int
-
ID of the channel.
Return values
Channel|null —null if not found in the cache.
getFactory()
Gets the factory.
public
getFactory() : Factory
Return values
FactorygetHttp()
Gets the HTTP client.
public
getHttp() : Http
Use Discord::getHttpClient()
Return values
HttpgetHttpClient()
Gets the HTTP client.
public
getHttpClient() : Http
Return values
HttpgetLogger()
Gets the logger being used.
public
getLogger() : LoggerInterface
Return values
LoggerInterfacegetLoop()
Gets the loop being used by the client.
public
getLoop() : LoopInterface
Return values
LoopInterfacegetVoiceClient()
Gets a voice client from a guild ID. Returns null if there is no voice client.
public
getVoiceClient(string $guild_id) : VoiceClient|null
Parameters
- $guild_id : string
-
The guild ID to look up.
Return values
VoiceClient|nullhandleWsClose()
Handles WebSocket closes received by the client.
public
handleWsClose(int $op, string $reason) : void
Parameters
- $op : int
-
The close code.
- $reason : string
-
The reason the WebSocket closed.
handleWsConnection()
Handles WebSocket connections received by the client.
public
handleWsConnection(WebSocket $ws) : void
Parameters
- $ws : WebSocket
-
WebSocket client.
handleWsConnectionFailed()
Handles cases when the WebSocket cannot be connected to.
public
handleWsConnectionFailed(Throwable $e) : void
Parameters
- $e : Throwable
handleWsError()
Handles WebSocket errors received by the client.
public
handleWsError(Exception $e) : void
Parameters
- $e : Exception
-
The error.
handleWsMessage()
Handles WebSocket messages received by the client.
public
handleWsMessage(Message $message) : void
Parameters
- $message : Message
-
Message object.
heartbeat()
Sends a heartbeat packet to the Discord gateway.
public
heartbeat() : void
identify()
Used to trigger the initial handshake with the gateway.
public
identify() : void
Tags
joinVoiceChannel()
Joins a voice channel.
public
joinVoiceChannel(Channel $channel[, bool $mute = false ][, bool $deaf = true ][, LoggerInterface|null $logger = null ]) : PromiseInterface<string|int, VoiceClient>
Parameters
- $channel : Channel
-
The channel to join.
- $mute : bool = false
-
Whether you should be mute when you join the channel.
- $deaf : bool = true
-
Whether you should be deaf when you join the channel.
- $logger : LoggerInterface|null = null
-
Voice client logger. If null, uses same logger as Discord client.
Tags
Return values
PromiseInterface<string|int, VoiceClient>listenCommand()
Add listener for incoming application command from interaction.
public
listenCommand(array<string|int, mixed>|string $names[, callable|null $callback = null ][, callable|null $autocomplete_callback = null ]) : RegisteredCommand
Parameters
- $names : array<string|int, mixed>|string
- $callback : callable|null = null
- $autocomplete_callback : callable|null = null
Tags
Return values
RegisteredCommandlistVoiceRegions()
Lists voice regions.
public
listVoiceRegions() : PromiseInterface<string|int, ExCollectionInterface<string|int, Region>|array<string|int, Region>>
Return values
PromiseInterface<string|int, ExCollectionInterface<string|int, Region>|array<string|int, Region>> —A promise that resolves to a collection of voice regions.
requestGuildMembers()
Used to request all members for a guild or a list of guilds.
public
requestGuildMembers(Guild|string $guild_id[, int $options = [] ]) : void
Ratelimited, once every 30 seconds per guild.
The server will send Guild Members Chunk events in response with up to 1000 members per chunk until all members that match the request have been sent.
Parameters
- $guild_id : Guild|string
-
ID of the guild or Guild object. Required.
- $options : int = []
-
['limit'] Maximum number of members to send matching the query. 0 with empty query returns all. Required when including a query.
Tags
requestSoundboardSounds()
Used to request soundboard sounds for a list of guilds. The server will send Soundboard Sounds events for each guild in response.
public
requestSoundboardSounds(array<string|int, mixed> $guildIds) : void
Parameters
- $guildIds : array<string|int, mixed>
-
Array of guild IDs.
Tags
resume()
Used to replay missed events when a disconnected client resumes.
public
resume(string $token, string $session_id, int $seq) : void
Parameters
- $token : string
- $session_id : string
- $seq : int
Tags
run()
Starts the ReactPHP event loop.
public
run() : void
setClient()
Sets the client instance.
public
setClient(Client $client) : void
Parameters
- $client : Client
-
The client instance.
updatePresence()
Sent by the client to indicate a presence or status update.
public
updatePresence([Activity|null $activity = null ][, bool $idle = false ][, string $status = 'online' ][, bool $afk = false ]) : void
Parameters
- $activity : Activity|null = null
-
The current client activity, or null. Note: Both name and state must be set to use custom, and the only valid fields are
name,state,typeandurl. - $idle : bool = false
-
Whether the client is idle.
- $status : string = 'online'
-
The current status of the client. Must be one of the following: online, dnd, idle, invisible, offline
- $afk : bool = false
-
Whether the client is AFK.
Tags
updateVoiceState()
Sent when a client wants to join, move, or disconnect from a voice channel.
public
updateVoiceState(Guild|string $guild_id[, Channel|string|null $channel_id = null ][, bool $self_mute = false ][, bool $self_deaf = false ]) : void
Parameters
- $guild_id : Guild|string
-
ID of the guild.
- $channel_id : Channel|string|null = null
-
ID of the voice channel to join, or null to disconnect.
- $self_mute : bool = false
-
Whether the client is muted.
- $self_deaf : bool = false
-
Whether the client is deafened.
Tags
buildParams()
Builds the gateway connection parameters and resolves the deferred with the gateway URL and session information.
protected
buildParams(Deferred $deferred, string $gateway[, SessionStartLimit $session = null ]) : void
Parameters
- $deferred : Deferred
-
The deferred object to resolve with the gateway and session data.
- $gateway : string
-
The base gateway URL to connect to.
- $session : SessionStartLimit = null
-
Optional session information. If null, a default session is used.
checkForChunks()
Checks for any large guilds that need to be chunked.
protected
checkForChunks() : void
handleDispatch()
Handles dispatch events received by the WebSocket.
protected
handleDispatch(object $data) : void
Parameters
- $data : object
-
Packet data.
handleGuildMembersChunk()
Handles `GUILD_MEMBERS_CHUNK` packets.
protected
handleGuildMembersChunk(Payload $data) : void
Parameters
- $data : Payload
-
Packet data.
Tags
handleHeartbeat()
Handles heartbeat packets received by the client.
protected
handleHeartbeat(object $data) : void
Parameters
- $data : object
-
Packet data.
handleHeartbeatAck()
Handles heartbeat ACK packets received by the client.
protected
handleHeartbeatAck(object $data) : void
Parameters
- $data : object
-
Packet data.
handleHello()
Handles HELLO packets received by the websocket.
protected
handleHello(object $data) : void
Parameters
- $data : object
-
Packet data.
handleInvalidSession()
Handles invalid session packets received by the client.
protected
handleInvalidSession(object $data) : void
Parameters
- $data : object
-
Packet data.
handleReady()
Handles `READY` packets.
protected
handleReady(Payload $data) : false|void
Parameters
- $data : Payload
-
Packet data.
Tags
Return values
false|voidhandleReconnect()
Handles reconnect packets received by the client.
protected
handleReconnect(object $data) : void
Parameters
- $data : object
-
Packet data.
handleResume()
Handles `RESUME` packets.
protected
handleResume(Payload $data) : void
Parameters
- $data : Payload
-
Packet data.
handleVoiceServerUpdate()
Handles `VOICE_SERVER_UPDATE` packets.
protected
handleVoiceServerUpdate(Payload $data) : void
Parameters
- $data : Payload
-
Packet data.
handleVoiceStateUpdate()
Handles `VOICE_STATE_UPDATE` packets.
protected
handleVoiceStateUpdate(Payload $data) : void
Parameters
- $data : Payload
-
Packet data.
processWsMessage()
Process WebSocket message payloads.
protected
processWsMessage(string $data) : void
Parameters
- $data : string
-
Message payload.
ready()
Emits init if it has not been emitted already.
protected
ready() : false|void
Return values
false|voidresolveOptions()
Resolves the options.
protected
resolveOptions([array<string|int, mixed> $options = [] ]) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed> = []
-
Array of options.
Tags
Return values
array<string|int, mixed> —Options.
send()
Sends a packet to the Discord gateway.
protected
send(Payload|array<string|int, mixed> $data[, bool $force = false ]) : void
Parameters
- $data : Payload|array<string|int, mixed>
-
Packet data.
- $force : bool = false
sendChunks()
Sends chunks of guild member requests.
protected
sendChunks([array<string|int, mixed> &$chunks = [] ]) : void
Parameters
- $chunks : array<string|int, mixed> = []
setGateway()
Retrieves and sets the gateway URL for the client.
protected
setGateway([string|null $gateway = null ]) : PromiseInterface
Parameters
- $gateway : string|null = null
-
Gateway URL to set.
Return values
PromiseInterfacesetupChunking()
Sets guild member chunking up.
protected
setupChunking() : false|void
Return values
false|voidsetupHeartbeat()
Sets the heartbeat timer up.
protected
setupHeartbeat(int $interval) : void
Parameters
- $interval : int
-
The heartbeat interval in milliseconds.
voiceServerUpdate()
protected
voiceServerUpdate(VoiceServerUpdate $vs, Channel $channel, array<string|int, mixed> &$data, Deferred &$deferred, LoggerInterface|null $logger) : mixed
Parameters
- $vs : VoiceServerUpdate
- $channel : Channel
- $data : array<string|int, mixed>
- $deferred : Deferred
- $logger : LoggerInterface|null
voiceStateUpdate()
protected
voiceStateUpdate(mixed $vs, mixed $channel, mixed &$data) : mixed
Parameters
- $vs : mixed
- $channel : mixed
- $data : mixed