Creating the Bot

We are now getting to the more exciting parts - creating the necessary logic for our bot!

Open your preferred editor of choice (I use Atom with PHP Integrator, but you're free to use anything else, except notepad and notepad++). Create a new file in your directory where you installed Yasmin, generally it's suggested to use something like index.php, bot.php or app.php as filename, but you're free to name your name however you want, as long as it has the extension .php.

Login into Discord

Before we can do anything with Yasmin, we need to require composer's autoloader and create a ReactPHP event loop. ReactPHP suggest to use \React\EventLoop\Factory::create() to create an event loop. You are of course free to import namespaces, I won't do this, as I think it's easier to understand if you see the whole namespace.

// Include composer autoloader
require(__DIR__.'/vendor/autoload.php');

// Create ReactPHP event loop
$loop = \React\EventLoop\Factory::create();

Now we required composer's autoloader and created an event loop! But this doesn't do anything yet, the event loop doesn't run yet, and we haven't created our Yasmin client yet.

Now we create a Yasmin client, we pass any client options we wish to use (refer to the docs of Client::__construct) and our event loop.

// Create the client
$client = new \CharlotteDunois\Yasmin\Client(array(), $loop);

Important note: The order of parameters will change in the future.

Now we got our client and we can add event listeners. We need event listeners, because Yasmin is asynchronous (that doesn't mean it's multithreaded), Yasmin emits events whenever something happens. You can listen on any events and run any code.

In my case, I will now listen on the ready event and print something whenever the client logs into the account (or successfully reconnects).

$client->on('ready', function () use ($client) {
    echo 'Successfully logged into '.$client->user->tag.PHP_EOL;
});

A list of available events can be found in the docs, in the ClientEvents interface.

The ready event gets emitted once Yasmin has received and processed all necessary informations by Discord to start and is ready to serve you events.

Add now any event listeners you wish to add, of course you can add event listeners at a later point too.

To login into Discord, we run Client::login, presenting our token. This method returns a promise and resolves, if a connection could successfully be established. This does not mean, the client is ready, it just means our connection and token was accepted by Discord. Of course, this promise can also be rejected.

$client->login('YOUR_TOKEN_GOES_HERE');

After logging, we need to run the event loop. It doesn't run automatically, that's something I don't want to do, because you may want to do something else first.

To run the event loop, a simple

$loop->run();

will be sufficient. Remember, this call will now block anything below from running, until you stop the event loop.

results matching ""

    No results matching ""