Skip to content
SQKo edited this page Sep 7, 2021 · 31 revisions

Discord is the main object of your Discord BOT Application

Creates a Discord client instance.

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

Example

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

Return

  • Discord the bot instance

Closes the Discord client.

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

Example

$discord->close();

Allows access to the part/repository factory.

public factory(string $class[, mixed $data = [] ][, bool $created = false ]) : Part|AbstractRepository

The use of factory with part is possible with the new Part($discord, $data, $created);

Example

// Preferred way
$channel = new Channel($discord, ['name' = 'newchannel']);
// Is equivalent to:
$channel = $discord->factory(Channel::class, ['name' => 'newchannel']);

Return

  • Part the bot Part Factory
  • Repository the bot Repository Factory

Gets a channel.

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

Example

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

Return

Gets the factory.

public getFactory() : Factory

Example

$factory = $discord->getFactory();

Return

  • Factory the bot Factory object

Gets the HTTP client.

public getHttpClient() : Http

Example

$httpClient = $discord->getHttpClient();

Return

  • Http the bot Http object

Gets the logger being used.

public getLogger() : LoggerInterface

Example

$logger = $discord->getLogger();

Return

  • LoggerInterface the Logger interface

Gets the loop being used by the bot client.

public getLoop() : LoopInterface

Example

$loop = $discord->getLoop();

Return

  • LoopInterface the bot Loop interface

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

public getVoiceClient(string $guild_id) : VoiceClient|null

Example

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

Return

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

Joins a voice channel.

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

Example

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

Return

  • PromiseInterface the promise when bot has joined the voice channel

Starts the ReactPHP event loop.

public run() : void

Example

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

Updates the clients presence.

public 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(EVENTNAME, CALLBACK);

This event is called when the Discord BOT is ready (connected online).

public const READY = 'READY' 

Gateway events should be registered inside the ready event, which is emitted once when the bot first starts and has connected to the gateway.

Example

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

Clone this wiki locally