-
-
Notifications
You must be signed in to change notification settings - Fork 249
Discord
Discord is the main object of your Discord BOT Application
Creates a Discord client instance.
function __construct([array<string|int, mixed> $options = [] ]) : mixedReturn
- Discord the bot instance
Example
$discord = new Discord([
'token' => 'bot-token',
]);Closes the Discord client.
function close([bool $closeLoop = true ]) : voidExample
$discord->close();Allows access to the part/repository factory.
function factory(string $class[, mixed $data = [] ][, bool $created = false ]) : Part|AbstractRepositoryThe use of factory with part is possible with the new Part($discord, $data, $created);
Return
- Part the bot Part Factory
- Repository the bot Repository Factory
Example
// Preferred way
$channel = new Channel($discord, ['name' => 'newchannel']);
// Is equivalent to:
$channel = $discord->factory(Channel::class, ['name' => 'newchannel']);Gets a channel.
function getChannel(string|int $channel_id) : Channel|nullReturn
- Channel the channel or private channel part
Example
$channel = $discord->getChannel('012345678910111213');Gets the factory.
function getFactory() : FactoryReturn
- Factory the bot Factory object
Example
$factory = $discord->getFactory();Gets the HTTP client.
function getHttpClient() : HttpReturn
- Http the bot Http object
Example
$httpClient = $discord->getHttpClient();Gets the PSR-3 logger being used.
function getLogger() : LoggerInterfaceReturn
- LoggerInterface the Logger interface
Example
$logger = $discord->getLogger();Gets the ReactPHP event loop loop being used by the bot client.
function getLoop() : LoopInterfaceReturn
- LoopInterface the bot 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|nullReturn
- VoiceClient the bot Voice Client
-
nullthe bot has no Voice Client
Example
$voiceClient = $discord->getVoiceClient('012345678910111213');Joins a voice channel.
function joinVoiceChannel(Channel $channel[, bool $mute = false ][, bool $deaf = true ][, LoggerInterface|null $logger = null ][, bool $check = true ]) : PromiseInterfaceReturn
- PromiseInterface the promise when bot has joined the voice channel
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";
});Starts the ReactPHP event loop.
function run() : voidExample
// Should be placed at bottom of the script
$discord->run();Updates the clients presence.
function updatePresence([Activity|null $activity = null ][, bool $idle = false ][, string $status = 'online' ][, bool $afk = false ]) : voidSee 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);To handle various events from Discord, you can use $discord->on(EVENTNAME, CALLBACK);
This event is called when Discord Ready gateway is dispatched (connected online).
const READY = 'READY' Example
$discord->on(Event::READY, function ($data, $discord) {
echo "Bot is connected.", PHP_EOL;
});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) {
echo "Bot is ready.", PHP_EOL;
});This event is called when the web socket has successfully reconnected.
Example
$discord->on('reconnected', function ($discord) {
echo "Bot is reconnected.", PHP_EOL;
});This event is called after $discord->close().
Example
$discord->on('closed', function ($discord) {
// do something, e.g. clean up
});This is only for advanced users.
This event is called when any enabled events from gateway is dispatched from the web socket containing the raw data, before the respective Repository and Part is formed.
Example
$discord->on('raw', function ($data, $discord) {
// $data is json_decode'd payload
});This is only for advanced users.
This event is called the web socket error occurred. The web socket connection is immediately closed so you cannot interrupt this event.
Example
$discord->on('error', function ($error, $discord) {
// do something e.g. log the error or do clean up
});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