Larascord Events

Larascord dispatches its own events from the package. There is nothing to publish or register.

EventDispatched whenProperties
DiscordAccountCreatedA Discord account is inserted into larascord_accounts.$account
DiscordAccountUpdatedAn existing Discord account is updated.$account
DiscordAccountLinkedA Discord account is assigned a user_id.$account, $user
DiscordAccountUnlinkedA Discord account is deleted.$account
UserAuthenticatedThe callback completes a login.$account, $user

The events are in the Jakyeru\Larascord\Events namespace. $account is a Jakyeru\Larascord\Models\DiscordAccount and $user is an Illuminate\Contracts\Auth\Authenticatable.

Listening

Create a listener

php artisan make:listener WelcomeNewUser

Type hint the event

Laravel discovers the listener from the type hint of the handle method.

app/Listeners/WelcomeNewUser.php
<?php
 
namespace App\Listeners;
 
use Illuminate\Support\Facades\Log;
use Jakyeru\Larascord\Events\DiscordAccountCreated;
 
class WelcomeNewUser
{
    public function handle(DiscordAccountCreated $event): void
    {
        Log::info('Welcome ' . $event->account->tag);
    }
}

Example: adding the user to a guild

app/Listeners/AddUserToGuild.php
<?php
 
namespace App\Listeners;
 
use Jakyeru\Larascord\Events\UserAuthenticated;
 
class AddUserToGuild
{
    public function handle(UserAuthenticated $event): void
    {
        $event->account->joinGuild('81384788765712384');
    }
}

Example: rejecting a banned user

app/Listeners/BlockBannedUsers.php
<?php
 
namespace App\Listeners;
 
use Illuminate\Support\Facades\Auth;
use Jakyeru\Larascord\Events\UserAuthenticated;
 
class BlockBannedUsers
{
    public function handle(UserAuthenticated $event): void
    {
        if ($event->user->is_banned) {
            Auth::logout();
        }
    }
}