aedon / discord-bot
A PHP bot implementation for Discord.
Requires
- php: >=7.4.0|^8.0
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- aedon/php-expectations: ^0.9.6
- psr/log: ^1.1.2
- ratchet/pawl: ^0.3.5
Requires (Dev)
- mikey179/vfsstream: ^1.6.8
- phpspec/prophecy-phpunit: ^2.0.1
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2025-03-20 00:01:18 UTC
README
A Discord bot implementation written in PHP. Use this library to create your own bot.
- Integrates the Discord websocket API (receiving events from Discord)
- Adds basic support for the Discord REST API (sending commands to Discord)
- Supports API v6
Missing features
- Does not resume lost connections
- No support for voice channels
Requirements
PHP 7.4+ or PHP 8.0+
Required extensions:
- ext-curl
- ext-json
- ext-mbstring
Getting your bot up and running
You have created a Discord bot and it's now in your server but offline all day... Let's start!
Bring your bot to life
Create a PHP file and run it from the command line.
<?php require('vendor/autoload.php'); $token = '<insert-your-bot-token>'; $loop = \React\EventLoop\Factory::create(); $bot = new \Aedon\DiscordBot\Service\DiscordBot($token); $bot->initialize($loop, new \Ratchet\Client\Connector($loop)); $loop->run();
Even though your bot won't do much for now it should actually be online and it should receive heartbeat events in the console.
Subscribe to events
The bot receives events from Discord now. You can subscribe to a specific event or all events. In this example you will listen to MESSAGE_CREATE events.
class MySubscriber implements \Aedon\Discordbot\Event\EventSubscriberInterface { public function process(\Aedon\Discordbot\Event\EventInterface $event): void { print_r($event->getData('d')); } } // Create the bot $bot->subscribe(new MySubscriber(), 'MESSAGE_CREATE'); // Run the loop
Let the bot do something in Discord
The bot library has very basic support for the Discord REST API that is used to e.g. create a message in Discord. For more information about the Discord REST API check out the documentation.
Add the RestApiSubscriberInterface to your subscriber and it will get access to the rest api object.
class MySubscriber implements \Aedon\Discordbot\Event\EventSubscriberInterface, \Aedon\DiscordBot\Rest\RestApiSubscriberInterface { private \Aedon\DiscordBot\Rest\RestApiInterface $restApi; public function process(\Aedon\Discordbot\Event\EventInterface $event): void { if ($event->getName() == 'MESSAGE_CREATE') { $data = $event->getData('d'); if (isset($data['content']) && $data['content'] == '/roll') { if (isset($data['channel_id']) && !empty($data['channel_id'])) { $this->restApi->post('/channels/' . $data['channel_id'] . '/messages', [ 'content' => 'Roll Result (1-6): ' . (int)mt_rand(1, 6), ]); } } } } public function setRestApi(\Aedon\DiscordBot\Rest\RestApiInterface $restApi): void { $this->restApi = $restApi; } }
The bot will listen for MESSAGE_CREATE events and when someone writes "/roll" it will answer back to the user with "Roll Result (1-6): X".
Support
Join Discord: https://discord.gg/NEfRerY
Aedon Discord Bot created by Michael "Striker" Berger