PHP code example of aedon / discord-bot

1. Go to this page and download the library: Download aedon/discord-bot library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aedon / discord-bot example snippets




n = '<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();

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

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;
    }
}