Listening to Commands

In order to listen to commands, we need to listen on the message event. The event listener will receive an instance of Message, which contains all necessary informations. The event gets emitted, once the bot receives a message from a channel he has access to. That means the bot won't receive messages from a channel he doesn't have access to, obviously.

$client->on('message', function (\CharlotteDunois\Yasmin\Models\Message $message) {
    var_dump($message->content);
});

I recommend to install Xdebug for development, as there are circular references in most Yasmin models, so logging them will yield to a really huge output.

Replying to Commands

Logging is all great, but even better is it to make your bot respond to them!

To do that, we need to implement a logic for your bot. For our bot, we use the prefix $ for our commands.

It's recommended to always use a prefix. Prefixless bots can be very annoying.

For the start, we will use an if condition for our commands. It's recommended to use a more advanced command handler. If you don't want to write your own, consider using Livia.

$client->on('message', function (\CharlotteDunois\Yasmin\Models\Message $message) {
    if($message->content === '$ping') {
        $message->channel->send('Pong!');
    }
});

If you did that, restarted your bot and sent a message with the content $ping, and everything goes well, the bot should reply with Pong! .

Congratulations, you've created your first functional bot command! But this isn't the end of it, this is just the start.

results matching ""

    No results matching ""