Getting started

Disboard provides a simple way of sending user notifications through Discord directly to your users. Utilizing Laravel's built-in Notifiable facade and a custom provider for Discord, you can start writing your own notifications and start sending them instantly!

These notifications can be used to inform your users that someone started following their profile, they levelled up, or really anything else! You can start creating your own notification with the php artisan make:notification command.

In this example, we'll make an example notification where a user leaves a comment on another user's profile.

Note
You must make a Bot Application and add it to at least one Discord server before you can send messages to users. Once you do those steps, run php artisan discord:setup to connect your application to the Discord gateway.

Creating the notification

We'll start by creating our new notification class. Run php artisan make:notification UserCommentNotification. A new folder named Notifications will be created in your app folder, along with your new notification class:



namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class UserCommentNotification extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

            

Currently this notification only sends via email, but we just need to add a few things to make it work with Discord. Start by adding the following using statements at the top:


use NotificationChannels\Discord\DiscordChannel;
use NotificationChannels\Discord\DiscordMessage;
            

These classes allow us to use Laravel's built-in Discord notification channels out-of-the-box. Next, modify (or replace) the return in the via($notifiable) function to add the Discord route:


/**
 * Get the notification's delivery channels.
 *
 * @param    mixed  $notifiable
 * @return  array
 */
public function via($notifiable)
{
    return [DiscordChannel::class];
}
            

Last thing we need in the notification class is the message we're sending to Discord. This is provided in a toDiscord() function. Here's an example message we can send to the user:


public function toDiscord ($notifiable)
{
    return DiscordMessage::create("{$this->user->getDisplayName()} left a comment on your profile!");
}
            

This will direct message the user on Discord when the notification is sent.

Here's the final result of the class, with comments omitted:


namespace App\Notifications;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use NotificationChannels\Discord\DiscordChannel;
use NotificationChannels\Discord\DiscordMessage;

class UserCommentNotification extends Notification implements ShouldQueue
{
    use Queueable;
    protected $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function via($notifiable)
    {
        return [DiscordChannel::class];
    }
    
    public function toDiscord($notifiable)
    {
        return DiscordMessage::create("{$this->user->getDisplayName()} left a comment on your profile!");
    }
}
            

Invoking the notification

Finalizing the notification class, you need to specify which model we're calling the notification on. In the case of our example, we will be calling the notification from the User model. Because of this, we need to call the notify() function from an instance of a user.

Note
The User model already has the proper function defined for routing notifications to a channel, but if you want to set this up on another model, you must implement the routeNotificationToDiscord() function yourself!

To send the notification, call the notify() function on a user model from anywhere in your application:


// Add include for notification
use App\Notifications\UserCommentNotification;

// Call it
$user->notify(new UserCommentNotification($commenter));
            

Have fun sending notifications!