-
Notifications
You must be signed in to change notification settings - Fork 237
Home
DiscordPHP is powered by ReactPHP/Event-Loop. It is important to know that exucting $discord->start()
starts the event loop and blocks the execution until the loop is stopped.
$discord = new Discord([
'token' => '...'
'loop' => Loop::get() // set the Event Loop
]);
// Make sure to place event listeners before the run()
$discord->on('ready', function (Discord $discord) {
// ...
});
echo 'Bot started!';
// Must be placed at the bottom
$discord->run(); // This blocks here
// Any code below here will NOT be executed until the loop is stopped.
echo 'Bot stopped!';
Many methods from event loop are useful for the Bot operation such as the addTimer()
and addPeriodicTimer()
$discord->on('ready', function (Discord $discord) {
// get the event loop
$loop = $discord->getLoop();
// start a 5 seconds timer to run the function
$loop->addTimer(5, function ($timer) {
echo 'Printed after 5 seconds!';
});
});
As a stateful library there is mass data that needs to be processed. This requires asynchronous execution to prevent data loss and slow downs. This means that promises are required to be used for most methods that would require contact with the Discord servers. Methods are indicated if they return promises. It is usually found along with the then()
and done()
method chain.
Read up on the usage of promises here, albeit in a JavaScript context, but the concepts carry over to PHP. DiscordPHP is powered by ReactPHP/Promise.
Here is an example of sending a message in a channel, which will return a promise:
echo 'before message'.PHP_EOL;
$channel->sendMessage('my message')->then(
function (Message $message) {
echo 'Message sent!'.PHP_EOL;
// Message was successfully sent, continue with execution.
},
function (\Exception $e) {
echo 'Error sending message: '.$e->getMessage().PHP_EOL;
// Message was not sent and an error occured.
}
);
echo 'after message'.PHP_EOL;
The output from this code would be the following:
before message
after message
Message sent!
Parts represent Discord objects such as messages, guilds and channels.
Repositories act as data containers consisting of parts. An example is a channel repository:
Creating and modifying parts must be done through the respective repository. An example is if you are modifying a channel:
- The channel part is modified.
- The channel part is saved through the repository, which sends an HTTP request to the Discord to update the object remotely, and stores the part in the local repository.
Depending on the repository endpoints, you may use the factory methods:
-
create(Part::class, ...)
or$part = new Part($discord, ...)
to create a Part locally -
fetch('PART ID')
to fetch a Part from Discord (GET
request to Discord API) -
save($part)
to save a Part into Discord (POST
orPATCH
request to Discord API) -
delete($part)
ordelete('PART ID')
to delete a Part locally & from Discord (DELETE
request to Discord API)
Additionally you may:
-
fresh($part)
load or refresh a local Part from the Discord -
freshen()
refresh all Parts from the Discord
An example of a Message
Part life cycle in a Channel
:
- BOT received
Event::MESSAGE_CREATE
then the Part is created with$message = new
Message
($discord, ...)
- Additionally if
storeMessages
option is enabled, cache locally into$channel
->messages
Repository
- Additionally if
- a
Member
sends a "hello"Message
- If BOT is instructed to
$message->delete()
or$channel->messages->delete($message)
, BOT will send aDELETE
HTTP request to Discord API - Once the HTTP request is
done()
, the$message
isunset()
from$channel
->messages
Repository
$discord->on(Event::MESSAGE_CREATE, function (Message $message, Discord $discord) { // 1
if ($message->content == 'hello') { // 2
$message
->delete() // 4
->done(function ($deletedMessage) { // 5
// Message is deleted, $deletedMessage is the data of cached Message
});
}
});
Note: This wiki is currently Work In Progress. Consider reading the docs instead.
- Application Command (Slash based)
Command Client (Message based)
- Activity
- Application
- Guild
- Private Channel
- User
Components
-
ActionRow
- Buttons
- Option (commands)
- SelectMenu
- TextInput
Builders