Skip to content

Commit

Permalink
Change 'ready' to 'init' (#1057) build docs
Browse files Browse the repository at this point in the history
* Update docs
* Update examples
* Update README.md
* change emittedReady to emittedInit
* proper deprecation of ready
* check if ready listeners still present, if thats the case, send logger info and emit.
  • Loading branch information
key2peace authored Feb 11, 2023
1 parent 51c7d0b commit edfa57d
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ $discord = new Discord([
// | Intents::MESSAGE_CONTENT, // Note: MESSAGE_CONTENT is privileged, see https://dis.gd/mcfaq
]);

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

// Listen for messages.
Expand Down
4 changes: 2 additions & 2 deletions docs/src/pages/api/02_basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ The following options should only be used by large bots that require sharding. I

<hr>

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

```php
$discord->on('ready', function (Discord $discord) {
$discord->on('init', function (Discord $discord) {
```

To register an event we use the `$discord->on(...)` function, which registers a handler.
Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/api/03_events/00_events.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Most events also requires the respective Intents enabled (as well privileged one
To listen on gateway events, use the event emitter callback and `Event` name constants.
Some events are internally handled by the library and may not be registered a listener:

- `Event::READY` (not to be confused with `'ready'`)
- `Event::READY`
- `Event::RESUMED`
- `Event::GUILD_MEMBERS_CHUNK`

Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/api/03_events/04_guilds.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Requires the `Intents::GUILDS` intent.

Called with a `Guild` object in one of the following situations:

1. When the Bot is first starting and the guilds are becoming available. (unless the listener is put inside after 'ready' event)
1. When the Bot is first starting and the guilds are becoming available. (unless the listener is put inside after 'init' event)
2. When a guild was unavailable and is now available due to an outage.
3. When the Bot joins a new guild.

Expand Down
2 changes: 1 addition & 1 deletion examples/ping.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
]);

// When the Bot is ready
$discord->on('ready', function (Discord $discord) {
$discord->on('init', function (Discord $discord) {

// Listen for messages
$discord->on('message', function (Message $message, Discord $discord) {
Expand Down
4 changes: 2 additions & 2 deletions guide/basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ The following options should only be used by large bots that require sharding. I

----

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

To register an event we use the ``$discord->on(...)`` function, which registers a handler. A list of events is available `here <https://github.com/discord-php/DiscordPHP/blob/master/src/Discord/WebSockets/Event.php#L30-L75>`_. They are described in more detail in further sections of the documentation. All events take a callback which is called when the event is triggered, and the callback is called with an object representing the content of the event and an instance of the ``Discord`` client.

.. code:: php
$discord->on('ready', function (Discord $discord) {
$discord->on('init', function (Discord $discord) {
$discord->on(Event::MESSAGE_CREATE, function (Message $message, Discord $discord) {
// ... handle message sent
Expand Down
2 changes: 1 addition & 1 deletion guide/events/guilds.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Guild Create

Called with a ``Guild`` object in one of the following situations:

1. When the Bot is first starting and the guilds are becoming available. (unless the listener is put inside after ‘ready’ event)
1. When the Bot is first starting and the guilds are becoming available. (unless the listener is put inside after ‘init’ event)
2. When a guild was unavailable and is now available due to an outage.
3. When the Bot joins a new guild.

Expand Down
2 changes: 1 addition & 1 deletion guide/events/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ All gateway events are enabled by default and can be individually disabled using

To listen on gateway events, use the event emitter callback and ``Event`` name constants. Some events are internally handled by the library and may not be registered a listener:

- ``Event::READY`` (not to be confused with ``'ready'``)
- ``Event::READY``
- ``Event::RESUMED``
- ``Event::GUILD_MEMBERS_CHUNK``

Expand Down
20 changes: 13 additions & 7 deletions src/Discord/Discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@ class Discord
protected $heartbeatTime;

/**
* Whether `ready` has been emitted.
* Whether `init` has been emitted.
*
* @var bool Emitted.
*/
protected $emittedReady = false;
protected $emittedInit = false;

/**
* The gateway URL that the WebSocket client will connect to.
Expand Down Expand Up @@ -379,7 +379,7 @@ public function __construct(array $options = [])
}

$function = function () use (&$function) {
$this->emittedReady = true;
$this->emittedInit = true;
$this->removeListener('ready', $function);
};

Expand Down Expand Up @@ -804,7 +804,7 @@ protected function handleDispatch(object $data): void
Event::GUILD_DELETE,
];

if (! $this->emittedReady && (! in_array($data->t, $parse))) {
if (! $this->emittedInit && (! in_array($data->t, $parse))) {
$this->unparsedPackets[] = function () use (&$handler, &$deferred, &$data) {
/** @var ExtendedPromiseInterface */
$promise = coroutine([$handler, 'handle'], $data->d);
Expand Down Expand Up @@ -1121,17 +1121,23 @@ protected function send(array $data, bool $force = false): void
}

/**
* Emits ready if it has not been emitted already.
* Emits init if it has not been emitted already.
* @return false|void
*/
protected function ready()
{
if ($this->emittedReady) {
if ($this->emittedInit) {
return false;
}

$this->logger->info('client is ready');
$this->emit('ready', [$this]);
$this->emit('init', [$this]);

/* deprecated */
if (isset($this->listeners['ready'])) {
$this->logger->info('The \'ready\' event is deprecated and will be removed in a future version of DiscordPHP. Please use \'init\' instead.');
$this->emit('ready', [$this]);
}

foreach ($this->unparsedPackets as $parser) {
$parser();
Expand Down
2 changes: 1 addition & 1 deletion src/Discord/DiscordCommandClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function __construct(array $options = [])

parent::__construct($discordOptions);

$this->on('ready', function () {
$this->on('init', function () {
$this->commandClientOptions['prefix'] = str_replace('@mention', (string) $this->user, $this->commandClientOptions['prefix']);
$this->commandClientOptions['name'] = str_replace('<UsernamePlaceholder>', $this->username, $this->commandClientOptions['name']);

Expand Down

0 comments on commit edfa57d

Please sign in to comment.