team-reflex/discord-php
DiscordPHP is a CLI-focused PHP wrapper for Discord’s REST, Gateway, and Voice APIs. Build bots with event-driven ReactPHP support, with community framework integrations like Laracord for Laravel. Docs and class reference available online.
## Getting Started
### Minimal Setup in Laravel
1. **Install via Composer** (preferably with `laracord/laracord` for Laravel integration):
```bash
composer require team-reflex/discord-php laracord/laracord
Configure Laravel Service Provider (if using laracord):
Add to config/app.php under providers:
Laracord\LaracordServiceProvider::class,
Publish the config:
php artisan vendor:publish --provider="Laracord\LaracordServiceProvider"
Update .env with your bot token:
DISCORD_BOT_TOKEN=your_bot_token_here
First Use Case: Basic Bot Command
Create a command handler in app/Console/Commands/DiscordBot.php:
<?php
namespace App\Console\Commands;
use Discord\Discord;
use Discord\Parts\Channel\Message;
use Discord\WebSockets\Intents;
use Laracord\Laracord;
use Illuminate\Console\Command;
class DiscordBot extends Command
{
protected $signature = 'discord:bot';
protected $description = 'Run the Discord bot';
public function handle(Laracord $laracord)
{
$discord = $laracord->makeDiscord([
'token' => config('discord.bot_token'),
'intents' => Intents::getDefaultIntents()
]);
$discord->on('ready', function (Discord $discord) {
$this->info('Bot is ready!');
});
$discord->on('messageCreate', function (Message $message) {
if ($message->content === '!ping') {
$message->channel->sendMessage('Pong!');
}
});
$discord->run();
}
}
Run the bot:
php artisan discord:bot
$discord->on('messageCreate', function (Message $message) {
event(new DiscordMessageReceived($message));
});
app/Listeners/DiscordMessageListener.php:
public function handle(DiscordMessageReceived $event)
{
if ($event->message->content === '!data') {
$event->message->channel->sendMessage(
json_encode(['status' => 'ok', 'data' => User::all()->toArray()])
);
}
}
routes/web.php:
Route::discordCommand('greet', [GreetCommand::class, 'handle']);
app/Http/Controllers/GreetCommand.php:
public function handle(Discord\Parts\Interactions\CommandInteraction $interaction)
{
return $interaction->createResponse('Hello, ' . $interaction->member->username);
}
$discord->on('guildCreate', function ($guild) {
Guild::updateOrCreate(
['discord_id' => $guild->id],
['name' => $guild->name, 'owner_id' => $guild->ownerId]
);
});
discord-php-voice for voice interactions.$discord->on('messageCreate', function (Message $message) {
if ($message->content === '!join') {
$voice = $discord->voice;
$voice->join($message->guild->voiceChannel);
}
});
config/discord.php:
return [
'intents' => [
'default' => Intents::GUILDS | Intents::GUILD_MESSAGES,
'voice' => Intents::GUILDS | Intents::GUILD_VOICE_STATES,
],
];
$discord = $laracord->makeDiscord([
'token' => config('discord.bot_token'),
'intents' => config('discord.intents.default'),
]);
$discord->on('messageCreate', function (Message $message) {
if ($message->content === '!throttled') {
if ($message->author->can('throttled')) {
$message->channel->sendMessage('Action performed!');
} else {
$message->channel->sendMessage('Too many requests. Try again later.');
}
}
});
Discord\Parts\User\User to include throttle logic.Artisan::queueCommand() to run the bot in the background:
Artisan::queueCommand('discord:bot');
ini_set('memory_limit', '-1');
$discord->on('messageCreate', function (Message $message) {
dispatch(new ProcessDiscordMessage($message))->onQueue('discord');
});
MESSAGE_CONTENT) require privileged access.GUILD_MEMBERS intent:
$discord = $laracord->makeDiscord([
'token' => config('discord.bot_token'),
'intents' => Intents::GUILDS | Intents::GUILD_MEMBERS,
]);
$discord->on('messageCreate', function (Message $message) {
\Discord\Promises\promise(function () use ($message) {
return User::where('discord_id', $message->author->id)->first();
})->then(function ($user) use ($message) {
$message->channel->sendMessage("Hello, {$user->name}!");
});
});
caextract.crt from cURL's site and set openssl.cafile in php.ini.retry helper:
try {
retry(3, function () use ($message) {
$message->channel->sendMessage('Hello!');
});
} catch (\Exception $e) {
$message->channel->sendMessage('Failed to send message.');
}
$this->app->bind(Discord::class, function ($app) {
return $app->make(Laracord::class)->makeDiscord([
'token' => config('discord.bot_token'),
'intents' => config('discord.intents.default'),
How can I help you explore Laravel packages today?