Build powerful Discord bots with PHP

A wrapper for Discord's REST, WebSocket and Voice APIs built on ReactPHP. Event-driven, non-blocking, and ready for production.

1K+ stars 340K+ downloads 250+ forks
bot.php
<?php

use Discord\Discord;
use Discord\WebSockets\Event;
use Discord\WebSockets\Intents;

$discord = new Discord([
    'token' => getenv('DISCORD_TOKEN'),
    'intents' => Intents::getDefaultIntents(),
]);

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

    $discord->on(Event::MESSAGE_CREATE, function ($message) {
        if ($message->content === '!ping') {
            $message->reply('Pong!');
        }
    });
});

$discord->run();

Everything you need to build Discord bots

A comprehensive PHP library with full Discord API coverage, built on ReactPHP for high-performance, non-blocking operation.

Real-time Gateway

Full WebSocket support for real-time events with automatic reconnection and heartbeat management.

REST API

Complete REST API coverage for all Discord endpoints with rate limiting and error handling.

Voice Support

Send and receive audio in voice channels with FFmpeg integration for media processing.

Interactive Components

Build rich UIs with buttons, select menus, modals, and slash commands.

Permissions System

Full permissions system with role and channel overwrite support for fine-grained access control.

Event-Driven

Built on ReactPHP for non-blocking, high-performance asynchronous operation.

Collections

Laravel-inspired Collection class for easy and fluent data manipulation.

Type-Safe

PHP 8.1+ with full type hints, enums, and IDE autocompletion support.

Quick Installation

Get up and running in minutes with Composer. DiscordPHP runs as a CLI application.

1 Requirements

  • PHP 8.1.2 or higher (CLI only)
  • ext-json
  • ext-zlib

2 Recommended (Optional)

  • ext-uv (better performance)
  • ext-ev (better performance)
  • ext-event (better performance)
  • ext-mbstring (better performance)

3 Voice Support

  • 64-bit PHP
  • ext-sodium
  • FFmpeg installed

Install via Composer

# Install latest stable release
composer require team-reflex/discord-php

# Or install development version
composer require team-reflex/discord-php dev-master

Include Autoloader

<?php

include __DIR__.'/vendor/autoload.php';

use Discord\Discord;

Code Examples

See how easy it is to build powerful Discord bots with DiscordPHP.

basic-bot.php
<?php

use Discord\Discord;
use Discord\Parts\Channel\Message;
use Discord\WebSockets\Event;
use Discord\WebSockets\Intents;

$discord = new Discord([
    'token' => getenv('DISCORD_TOKEN'),
    'intents' => Intents::getDefaultIntents() | Intents::MESSAGE_CONTENT,
]);

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

    // Listen for messages
    $discord->on(Event::MESSAGE_CREATE, function (Message $message, Discord $discord) {
        // Ignore bot messages
        if ($message->author->bot) return;

        // Respond to !hello
        if ($message->content === '!hello') {
            $message->channel->sendMessage("Hello, {$message->author}!");
        }
    });
});

$discord->run();
slash-commands.php
<?php

use Discord\Discord;
use Discord\Builders\MessageBuilder;
use Discord\Parts\Interactions\Interaction;
use Discord\Parts\Interactions\Command\Command;

$discord->on('ready', function (Discord $discord) {
    // Register a slash command
    $command = new Command($discord, [
        'name' => 'ping',
        'description' => 'Check bot latency',
    ]);

    $discord->application->commands->save($command);

    // Handle interactions
    $discord->listenCommand('ping', function (Interaction $interaction) {
        $interaction->respondWithMessage(
            MessageBuilder::new()->setContent('Pong! Latency: ' . round($interaction->discord->wsLatency() * 1000) . 'ms')
        );
    });
});
components.php
<?php

use Discord\Builders\MessageBuilder;
use Discord\Builders\Components\ActionRow;
use Discord\Builders\Components\Button;
use Discord\Parts\Interactions\Interaction;

// Create a message with buttons
$message = MessageBuilder::new()
    ->setContent('Click a button!')
    ->addComponent(
        ActionRow::new()
            ->addComponent(
                Button::new(Button::STYLE_PRIMARY)
                    ->setLabel('Click me!')
                    ->setListener(function (Interaction $interaction) {
                        $interaction->respondWithMessage(
                            MessageBuilder::new()->setContent('You clicked the button!')
                        );
                    }, $discord)
            )
            ->addComponent(
                Button::new(Button::STYLE_DANGER)
                    ->setLabel('Delete')
                    ->setCustomId('delete_message')
            )
    );

$channel->sendMessage($message);
voice.php
<?php

use Discord\Voice\VoiceClient;
use Discord\Parts\Channel\Channel;

// Join a voice channel
$discord->joinVoiceChannel($voiceChannel)->then(function (VoiceClient $vc) {
    echo "Joined voice channel!", PHP_EOL;

    // Play audio from file
    $vc->playFile('path/to/audio.mp3')->then(function () use ($vc) {
        echo "Finished playing audio", PHP_EOL;

        // Leave the channel
        $vc->close();
    });

    // Or play from URL
    // $vc->playFile('https://example.com/audio.mp3');

    // Control playback
    // $vc->pause();
    // $vc->unpause();
    // $vc->stop();
});

Contributors

Thank you to all the amazing contributors who have helped build DiscordPHP.

Join the Community

Connect with thousands of PHP Discord bot developers. Get help, share your projects, and stay updated on the latest releases.

  • Get Help

    Ask questions and get help from experienced developers.

  • Stay Updated

    Be the first to know about new releases and features.

  • Share Projects

    Showcase your bots and get feedback from the community.

  • Contribute

    Help improve DiscordPHP by contributing code or documentation.

Join Discord Server