PHP code example of laravel-notification-channels / discord

1. Go to this page and download the library: Download laravel-notification-channels/discord library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

laravel-notification-channels / discord example snippets


// config/app.php
'providers' => [
    // ...
    NotificationChannels\Discord\DiscordServiceProvider::class,
],

    // config/services.php
    'discord' => [
        'token' => 'YOUR_API_TOKEN',
    ],
    

class Guild extends Eloquent
{
    use Notifiable;

    public function routeNotificationForDiscord()
    {
        return $this->discord_channel;
    }
}

> use NotificationChannels\Discord\Discord;
>
> class UserDiscordSettingsController
> {
>     public function store(Request $request)
>     {
>         $userId = $request->input('discord_user_id');
>         $channelId = app(Discord::class)->getPrivateChannel($userId);
>
>         Auth::user()->update([
>             'discord_user_id' => $userId,
>             'discord_private_channel_id' => $channelId,
>         ]);
>     }
> }
> 

// ...
use NotificationChannels\Discord\DiscordChannel;
use NotificationChannels\Discord\DiscordMessage;

class GameChallengeNotification extends Notification
{
    public $challenger;

    public $game;

    public function __construct(Guild $challenger, Game $game)
    {
        $this->challenger = $challenger;
        $this->game = $game;
    }

    public function via($notifiable)
    {
        return [DiscordChannel::class];
    }

    public function toDiscord($notifiable)
    {
        return DiscordMessage::create("You have been challenged to a game of *{$this->game->name}* by **{$this->challenger->name}**!");
    }
}
shell
    php artisan discord:setup