-
-
Notifications
You must be signed in to change notification settings - Fork 248
Home
This wiki is currently a work in progress. Content may be missing or inaccurate. Feel free to join our Discord server to get additional help.
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. Read up on the usage of promises here, albeit in a JavaScript context, but the concepts carry over to PHP. DiscordPHP uses the 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.
})->otherwise(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()ornew Part($discord, ...)to create a Part locally -
fetch()to fetch a Part from Discord (GETrequest to Discord API) -
save()to save a Part into Discord (POSTrequest to Discord API) -
delete()to delete a Part locally & on Discord if exists (DELETErequest to Discord API)
Additionally you may:
-
fresh()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:
- a
Membersends a "hello"Message - BOT received
Event::MESSAGE_CREATEthencreate()the$messagePart- Additionally if
saveMessagesoption is enabled, cache locally into$channel->messagesRepository
- Additionally if
- If BOT has the appropriate permission, for instance
delete($message), BOT will send aDELETEHTTP request - Once
done(), the$messageisunset()from$channel->messagesRepository
$discord->on(Event::MESSAGE_CREATE, function (Message $message, Discord $discord) { // 2
if ($message->content == 'hello') { // 1
$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