Skip to content
SQKo edited this page Oct 22, 2023 · 31 revisions

Discord is the main object of your Discord BOT Application

use Discord\Discord;

Creates a Discord Bot client instance.

function __construct([array<string|int, mixed> $options = [] ]) : mixed

Return

  • Discord the Bot client object

Example

$discord = new Discord([
    'token' => 'bot-token',
]);

Closes the Discord Bot client.

function close([bool $closeLoop = true ]) : void

Example

// Regular close
$discord->close();
// Graceful close (invalidate session & turn Bot offline)
$discord->close(false);
$loop = $discord->getLoop();
$loop->futureTick([$loop, 'stop']);

Note: If you are on development and doing regular restarts, it is preferably to use the first example, as the second example will need you to re-authenticate the session to Discord and may get you rate limit quicker if you frequently do that.

Gets a channel from the Bot cache by Snowflake ID.

function getChannel(string|int $channel_id) : Channel|null

Return

Example

$channel = $discord->getChannel('192180797392814081');

Gets the factory being used by the Bot client to create Part or Repository.

function getFactory() : Factory

Return

  • Factory the Factory object

Example

$factory = $discord->getFactory();

Gets the Discord HTTP client being used by the Bot client.

function getHttpClient() : Http

Return

  • Http the DiscordPHP Http object

Example

$httpClient = $discord->getHttpClient();

$httpClient->get(Endpoint::STICKERS); // GET discord nitro stickers https://discord.com/developers/docs/resources/sticker#list-nitro-sticker-packs

Gets the PSR-3 logger being used by the Bot client.

function getLogger() : LoggerInterface

Return

  • LoggerInterface the Logger interface

Example

$logger = $discord->getLogger();

$logger->info('This information goes to log!');

Gets the ReactPHP event loop loop being used by the Bot client.

function getLoop() : LoopInterface

Return

  • LoopInterface Loop interface

Example

$loop = $discord->getLoop();

Gets the bot voice client from a guild ID. Returns null if there is no voice client.

function getVoiceClient(string $guild_id) : VoiceClient|null

Return

  • VoiceClient the bot Voice Client
  • null the bot has no Voice Client

Example

$voiceClient = $discord->getVoiceClient('115233111977099271');

Joins a voice channel.

function joinVoiceChannel(Channel $channel[, bool $mute = false ][, bool $deaf = true ][, LoggerInterface|null $logger = null ][, bool $check = true ]) : PromiseInterface

Return

  • PromiseInterface the promise when Bot has joined the voice channel

Example

$channel = $discord->getChannel('192180797392814081'); // Get the voice channel by ID

$discord->joinVoiceChannel($channel)->then(function (VoiceClient $vc) {
    echo "Joined voice channel.\r\n";
    $vc->playFile('myaudio.mp3');
}, function ($e) {
    echo "There was an error joining the voice channel: {$e->getMessage()}\r\n"; 
})->done();

Run the Bot client, starts the ReactPHP event loop.

function run() : void

Example

// Should be placed at bottom of the script
$discord->run();

Updates the Bot client's presence and status.

function updatePresence([Activity|null $activity = null ][, bool $idle = false ][, string $status = 'online' ][, bool $afk = false ]) : void

See Activity

API Documentation: https://discord.com/developers/docs/topics/gateway#update-presence

Example

$activity = new Activity($discord, [
    'name' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
    'type' => Activity::TYPE_WATCHING
]);

$discord->updatePresence($activity);

Events

To handle various events from Discord, you can use $discord->on(string EVENTNAME, callable CALLBACK);

ready

Unlike "READY", this event is emitted once when the Discord BOT is ready (e.g. after start-up tasks like loadAllMembers is done).

Gateway event listeners should be registered inside this event.

Example

$discord->on('ready', function (Discord $discord) {
    echo "Bot is ready.", PHP_EOL;

    // place any $discord->on(Event::x, function () {}) here
});

reconnected

This event is called when the Bot client's web socket has successfully reconnected to Discord server.

Example

$discord->on('reconnected', function (Discord $discord) {
    echo "Bot is reconnected.", PHP_EOL;
});

closed

This event is called when $discord->close() is called.

Example

$discord->on('closed', function (Discord $discord) {
    // do something, e.g. clean up
});

raw

This is only for advanced users.

This event is called when any enabled events from gateway is dispatched from the Bot client's web socket containing the raw data, before the respective Part is formed.

Example

$discord->on('raw', function ($data, Discord $discord) {
    // $data contains the payload after json_decode()
});

heartbeat

This is only for advanced users.

This event is called frequently when the heartbeat packet is being sent. You should not run a heavy code here as it might prevent the heartbeat being sent and would interrupt connection to Discord.

Example

$discord->on('heartbeat', function (int $sequence, Discord $discord) {
    // $sequence is the heart beat sequence since connected
});

heartbeat-ack

This is only for advanced users.

This event is called frequently when the heartbeat packet is acknowledged. Around 60 seconds periodically, useful if you want to run regular tasks.

Example

$discord->on('heartbeat-ack', function ($time, Discord $discord) {
    // $time contains the duration between previous heartbeat
});

error

This is only for advanced users.

This event is called the Bot client's web socket error occurred. The web socket connection is immediately closed so you cannot interrupt this event.

Example

$discord->on('error', function ($error, Discord $discord) {
    // do something e.g. log the error or do clean up
});
Clone this wiki locally