aedon/discord-bot

A PHP bot implementation for Discord.

0.9.1 2022-08-03 06:52 UTC

This package is auto-updated.

Last update: 2024-04-19 21:55:59 UTC


README

GitHub release Build Status GitHub license

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