├── .editorconfig ├── .github └── workflows │ └── php.yml ├── .styleci.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── Commands └── SetupCommand.php ├── Discord.php ├── DiscordChannel.php ├── DiscordMessage.php ├── DiscordServiceProvider.php └── Exceptions └── CouldNotSendNotification.php /.editorconfig: -------------------------------------------------------------------------------- 1 | ; This file is for unifying the coding style for different editors and IDEs. 2 | ; More information at http://editorconfig.org 3 | 4 | root = true 5 | 6 | [*] 7 | charset = utf-8 8 | indent_size = 4 9 | indent_style = space 10 | end_of_line = lf 11 | insert_final_newline = true 12 | trim_trailing_whitespace = true 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.github/workflows/php.yml: -------------------------------------------------------------------------------- 1 | name: PHP 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | run: 7 | runs-on: ${{ matrix.os }} 8 | strategy: 9 | max-parallel: 15 10 | fail-fast: false 11 | matrix: 12 | os: [ubuntu-latest] 13 | php: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2'] 14 | dependency-version: [prefer-stable] 15 | 16 | name: PHP ${{ matrix.php }} - OS ${{ matrix.os }} - ${{ matrix.dependency-version }} 17 | steps: 18 | - name: Checkout 19 | uses: actions/checkout@master 20 | 21 | - name: Setup PHP 22 | uses: shivammathur/setup-php@master 23 | with: 24 | php-version: ${{ matrix.php }} 25 | extensions: mbstring 26 | coverage: pcov 27 | 28 | - name: Install dependencies 29 | run: composer update --${{ matrix.dependency-version }} --no-interaction --prefer-dist 30 | 31 | - name: Lint composer.json 32 | run: composer validate 33 | 34 | - name: Run Tests 35 | run: vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover 36 | 37 | - name: Upload Coverage to Scrutinizer 38 | run: | 39 | wget https://scrutinizer-ci.com/ocular.phar 40 | php ocular.phar code-coverage:upload --format=php-clover coverage.clover 41 | # Disable on PHP 8 as Ocular isn't supported 42 | if: matrix.php < 8 43 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All Notable changes to `discord` will be documented in this file 4 | 5 | ## 1.0.0 - 2017-08-14 6 | 7 | - initial release 8 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) Cody Scott 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Discord notification channel for Laravel 6.0+ 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/laravel-notification-channels/discord.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/discord) 4 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 5 | [![Build Status](https://img.shields.io/github/workflow/status/laravel-notification-channels/discord/PHP.svg?style=flat-square)](https://github.com/laravel-notification-channels/discord/actions) 6 | [![StyleCI](https://styleci.io/repos/65772492/shield)](https://styleci.io/repos/65772492) 7 | [![Quality Score](https://img.shields.io/scrutinizer/g/laravel-notification-channels/discord.svg?style=flat-square)](https://scrutinizer-ci.com/g/laravel-notification-channels/discord) 8 | [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/laravel-notification-channels/discord/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/laravel-notification-channels/discord/?branch=master) 9 | [![Total Downloads](https://img.shields.io/packagist/dt/laravel-notification-channels/discord.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/discord) 10 | 11 | This package makes it easy to send notifications using the [Discord bot API](https://discord.com/developers/docs/intro) with Laravel. 12 | 13 | ## Contents 14 | 15 | - [Discord notification channel for Laravel 6.0+](#discord-notification-channel-for-laravel-56) 16 | - [Contents](#contents) 17 | - [Installation](#installation) 18 | - [Setting up your Discord bot](#setting-up-your-discord-bot) 19 | - [Usage](#usage) 20 | - [Available Message methods](#available-message-methods) 21 | - [Changelog](#changelog) 22 | - [Testing](#testing) 23 | - [Security](#security) 24 | - [Contributing](#contributing) 25 | - [Credits](#credits) 26 | - [License](#license) 27 | 28 | 29 | ## Installation 30 | 31 | You can install the package via composer: 32 | 33 | ```bash 34 | composer require laravel-notification-channels/discord 35 | ``` 36 | 37 | Next, you must load the service provider: 38 | 39 | ```php 40 | // config/app.php 41 | 'providers' => [ 42 | // ... 43 | NotificationChannels\Discord\DiscordServiceProvider::class, 44 | ], 45 | ``` 46 | 47 | ### Setting up your Discord bot 48 | 49 | 1. [Create a Discord application.](https://discord.com/developers/applications) 50 | 2. Click the `Create a Bot User` button on your Discord application. 51 | 3. Paste your bot's API token, found under `App Bot User`, in your `services.php` config file: 52 | 53 | ```php 54 | // config/services.php 55 | 'discord' => [ 56 | 'token' => 'YOUR_API_TOKEN', 57 | ], 58 | ``` 59 | 60 | 4. Add the bot to your server and identify it by running the artisan command: 61 | 62 | ```shell 63 | php artisan discord:setup 64 | ``` 65 | 66 | ## Usage 67 | 68 | In every model you wish to be notifiable via Discord, you must add a channel ID property to that model accessible through a `routeNotificationForDiscord` method: 69 | 70 | ```php 71 | class Guild extends Eloquent 72 | { 73 | use Notifiable; 74 | 75 | public function routeNotificationForDiscord() 76 | { 77 | return $this->discord_channel; 78 | } 79 | } 80 | ``` 81 | 82 | > **NOTE**: Discord handles direct messages as though they are a regular channel. If you wish to allow users to receive direct messages from your bot, you will need to create a private channel with that user. 83 | > 84 | > An example workflow may look like the following: 85 | > 86 | > 1. Your `users` table has two discord columns: `discord_user_id` and `discord_private_channel_id` 87 | > 2. When a user updates their Discord user ID (`discord_user_id`), generate and save a private channel ID (`discord_private_channel_id`) 88 | > 3. Return the user's `discord_private_channel_id` in the `routeNotificationForDiscord` method on the `User` model 89 | > 90 | > You can generate direct message channels by using the `getPrivateChannel` method in the `NotificationChannels\Discord\Discord` class 91 | > 92 | > ```php 93 | > use NotificationChannels\Discord\Discord; 94 | > 95 | > class UserDiscordSettingsController 96 | > { 97 | > public function store(Request $request) 98 | > { 99 | > $userId = $request->input('discord_user_id'); 100 | > $channelId = app(Discord::class)->getPrivateChannel($userId); 101 | > 102 | > Auth::user()->update([ 103 | > 'discord_user_id' => $userId, 104 | > 'discord_private_channel_id' => $channelId, 105 | > ]); 106 | > } 107 | > } 108 | > ``` 109 | > 110 | > Please take note that the `getPrivateChannel` method only accepts [Discord's snowflake IDs](https://discord.com/developers/docs/reference#snowflakes). There is no API route provided by Discord to lookup a user's ID by their name and tag, and the process for copying and pasting a user ID can be confusing to some users. Because of this, it is recommended to add the option for users to connect their Discord account to their account within your application either by logging in with Discord or linking it to their pre-existing account. 111 | 112 | You may now tell Laravel to send notifications to Discord channels in the `via` method: 113 | 114 | ```php 115 | // ... 116 | use NotificationChannels\Discord\DiscordChannel; 117 | use NotificationChannels\Discord\DiscordMessage; 118 | 119 | class GameChallengeNotification extends Notification 120 | { 121 | public $challenger; 122 | 123 | public $game; 124 | 125 | public function __construct(Guild $challenger, Game $game) 126 | { 127 | $this->challenger = $challenger; 128 | $this->game = $game; 129 | } 130 | 131 | public function via($notifiable) 132 | { 133 | return [DiscordChannel::class]; 134 | } 135 | 136 | public function toDiscord($notifiable) 137 | { 138 | return DiscordMessage::create("You have been challenged to a game of *{$this->game->name}* by **{$this->challenger->name}**!"); 139 | } 140 | } 141 | ``` 142 | 143 | ### Available Message methods 144 | 145 | * `body(string)`: Set the content of the message. ([Supports basic markdown](https://support.discord.com/hc/en-us/articles/210298617-Markdown-Text-101-Chat-Formatting-Bold-Italic-Underline-)) 146 | * `embed(array)`: Set the embedded content. ([View embed structure](https://discord.com/developers/docs/resources/channel#embed-object)) 147 | * `components(array)`: Set the component content. ([View component structure](https://discord.com/developers/docs/interactions/message-components#component-object)) 148 | 149 | 150 | ## Changelog 151 | 152 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 153 | 154 | ## Testing 155 | 156 | ```bash 157 | $ composer test 158 | ``` 159 | 160 | ## Security 161 | 162 | If you discover any security related issues, please email cs475x@icloud.com instead of using the issue tracker. 163 | 164 | ## Contributing 165 | 166 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 167 | 168 | ## Credits 169 | 170 | - [Cody Scott](https://github.com/codyphobe) 171 | - [All Contributors](../../contributors) 172 | 173 | ## License 174 | 175 | The MIT License (MIT). Please see [LICENSE](LICENSE.md) for more information. 176 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "laravel-notification-channels/discord", 3 | "description": "Laravel notification driver for Discord.", 4 | "keywords": [ 5 | "laravel", 6 | "notification", 7 | "driver", 8 | "channel", 9 | "discord" 10 | ], 11 | "homepage": "https://github.com/laravel-notification-channels/discord", 12 | "license": "MIT", 13 | "authors": [ 14 | { 15 | "name": "Cody Scott", 16 | "email": "cs475x@icloud.com", 17 | "role": "Developer" 18 | } 19 | ], 20 | "require": { 21 | "php": "^7.2|^8.0", 22 | "ext-json": "*", 23 | "guzzlehttp/guzzle": "^6.3 || ^7.0", 24 | "illuminate/notifications": "^7.0 || ^8.0 || ^9.0 || ^10.0|^11.0|^12.0", 25 | "illuminate/support": "^7.0 || ^8.0 || ^9.0 || ^10.0|^11.0|^12.0", 26 | "illuminate/queue": "^7.0 || ^8.0 || ^9.0 || ^10.0|^11.0|^12.0", 27 | "illuminate/console": "^7.0 || ^8.0 || ^9.0 || ^10.0|^11.0|^12.0", 28 | "textalk/websocket": "^1.2" 29 | }, 30 | "require-dev": { 31 | "mockery/mockery": "^1.3.3", 32 | "orchestra/testbench": "^6.0 || ^7.0 || ^8.0|^9.0|^10.0", 33 | "phpunit/phpunit": "^8.5 || ^9.0|^10.5|^11.5.3" 34 | }, 35 | "autoload": { 36 | "psr-4": { 37 | "NotificationChannels\\Discord\\": "src" 38 | } 39 | }, 40 | "autoload-dev": { 41 | "psr-4": { 42 | "NotificationChannels\\Discord\\Test\\": "tests" 43 | } 44 | }, 45 | "scripts": { 46 | "test": "vendor/bin/phpunit" 47 | }, 48 | "config": { 49 | "sort-packages": true 50 | }, 51 | "extra": { 52 | "laravel": { 53 | "providers": [ 54 | "NotificationChannels\\Discord\\DiscordServiceProvider" 55 | ] 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Commands/SetupCommand.php: -------------------------------------------------------------------------------- 1 | guzzle = $guzzle; 47 | $this->token = $token; 48 | } 49 | 50 | /** 51 | * Attempt to connect and identify a bot with the Discord websocket gateway. 52 | * 53 | * @return int|void 54 | */ 55 | public function handle() 56 | { 57 | if (! $this->token) { 58 | $this->error('You must paste your Discord token (App Bot User token) into your `services.php` config file.'); 59 | $this->error('View the README for more info: https://github.com/laravel-notification-channels/discord#installation'); 60 | 61 | return -1; 62 | } 63 | 64 | if (! $this->confirm('Is the bot already added to your server?')) { 65 | $clientId = $this->ask('What is your Discord app client ID?'); 66 | 67 | $this->warn('Add the bot to your server by visiting this link: https://discord.com/oauth2/authorize?&client_id='.$clientId.'&scope=bot&permissions=0'); 68 | 69 | if (! $this->confirm('Continue?', true)) { 70 | return -1; 71 | } 72 | } 73 | 74 | $this->warn("Attempting to identify the bot with Discord's websocket gateway..."); 75 | 76 | $this->gateway = $this->getGateway(); 77 | 78 | $this->warn("Connecting to '$this->gateway'..."); 79 | 80 | $client = $this->getSocket($this->gateway); 81 | 82 | // Discord requires all bots to connect via a websocket connection and 83 | // identify at least once before any API requests over HTTP are allowed. 84 | // https://discord.com/developers/docs/topics/gateway#gateway-identify 85 | $client->send(json_encode([ 86 | 'op' => 2, 87 | 'd' => [ 88 | 'token' => $this->token, 89 | 'properties' => [ 90 | '$os' => PHP_OS, 91 | '$browser' => 'laravel-notification-channels-discord', 92 | '$device' => 'laravel-notification-channels-discord', 93 | ], 94 | ], 95 | ])); 96 | 97 | $response = $client->receive(); 98 | $identified = Arr::get(json_decode($response, true), 'op') === 10; 99 | 100 | if (! $identified) { 101 | $this->error("Discord responded with an error while trying to identify the bot: $response"); 102 | 103 | return -1; 104 | } 105 | 106 | $this->info('Your bot has been identified by Discord and can now send API requests!'); 107 | } 108 | 109 | /** 110 | * Get a websocket client for the given gateway. 111 | * 112 | * @param string $gateway 113 | * 114 | * @return \WebSocket\Client 115 | */ 116 | public function getSocket($gateway) 117 | { 118 | return new Client($gateway); 119 | } 120 | 121 | /** 122 | * Get the URL of the gateway that the socket should connect to. 123 | * 124 | * @return string 125 | */ 126 | public function getGateway() 127 | { 128 | $gateway = $this->gateway; 129 | 130 | try { 131 | $response = $this->guzzle->get('https://discord.com/api/gateway'); 132 | 133 | $gateway = Arr::get(json_decode($response->getBody(), true), 'url', $gateway); 134 | } catch (Exception $e) { 135 | $this->warn("Could not get a websocket gateway address, defaulting to '{$gateway}'."); 136 | } 137 | 138 | return $gateway; 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /src/Discord.php: -------------------------------------------------------------------------------- 1 | httpClient = $http; 41 | $this->token = $token; 42 | } 43 | 44 | /** 45 | * Send a message to a Discord channel. 46 | * 47 | * @param string $channel 48 | * @param array $data 49 | * 50 | * @return array 51 | */ 52 | public function send($channel, array $data) 53 | { 54 | return $this->request('POST', 'channels/'.$channel.'/messages', $data); 55 | } 56 | 57 | /** 58 | * Get a private channel with another Discord user from their snowflake ID. 59 | * 60 | * @param string $userId 61 | * 62 | * @return string 63 | */ 64 | public function getPrivateChannel($userId) 65 | { 66 | return $this->request('POST', 'users/@me/channels', ['recipient_id' => $userId])['id']; 67 | } 68 | 69 | /** 70 | * Perform an HTTP request with the Discord API. 71 | * 72 | * @param string $verb 73 | * @param string $endpoint 74 | * @param array $data 75 | * 76 | * @return array 77 | * 78 | * @throws \NotificationChannels\Discord\Exceptions\CouldNotSendNotification 79 | */ 80 | protected function request($verb, $endpoint, array $data) 81 | { 82 | $url = rtrim($this->baseUrl, '/').'/'.ltrim($endpoint, '/'); 83 | 84 | try { 85 | $response = $this->httpClient->request($verb, $url, [ 86 | 'headers' => [ 87 | 'Authorization' => 'Bot '.$this->token, 88 | ], 89 | 'json' => $data, 90 | ]); 91 | } catch (RequestException $exception) { 92 | if ($response = $exception->getResponse()) { 93 | throw CouldNotSendNotification::serviceRespondedWithAnHttpError($response, $response->getStatusCode(), $exception); 94 | } 95 | 96 | throw CouldNotSendNotification::serviceCommunicationError($exception); 97 | } catch (Exception $exception) { 98 | throw CouldNotSendNotification::serviceCommunicationError($exception); 99 | } 100 | 101 | $body = json_decode($response->getBody(), true); 102 | 103 | if (Arr::get($body, 'code', 0) > 0) { 104 | throw CouldNotSendNotification::serviceRespondedWithAnApiError($body, $body['code']); 105 | } 106 | 107 | return $body; 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/DiscordChannel.php: -------------------------------------------------------------------------------- 1 | discord = $discord; 20 | } 21 | 22 | /** 23 | * Send the given notification. 24 | * 25 | * @param mixed $notifiable 26 | * @param \Illuminate\Notifications\Notification $notification 27 | * 28 | * @return array 29 | * 30 | * @throws \NotificationChannels\Discord\Exceptions\CouldNotSendNotification 31 | */ 32 | public function send($notifiable, Notification $notification) 33 | { 34 | if (! $channel = $notifiable->routeNotificationFor('discord', $notification)) { 35 | return; 36 | } 37 | 38 | $message = $notification->toDiscord($notifiable); 39 | 40 | $data = [ 41 | 'content' => $message->body 42 | ]; 43 | 44 | if (count($message->embed) > 0) { 45 | $data['embeds'] = [$message->embed]; 46 | } 47 | 48 | if (count($message->components) > 0) { 49 | $data['components'] = $message->components; 50 | } 51 | 52 | return $this->discord->send($channel, $data); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/DiscordMessage.php: -------------------------------------------------------------------------------- 1 | body = $body; 46 | $this->embed = $embed; 47 | $this->components = $components; 48 | } 49 | 50 | /** 51 | * Set the text content of the message. 52 | * 53 | * @param string $body 54 | * 55 | * @return $this 56 | */ 57 | public function body($body) 58 | { 59 | $this->body = $body; 60 | 61 | return $this; 62 | } 63 | 64 | /** 65 | * Set a single embedded object. 66 | * 67 | * TODO: Refactor to enable multiple embeds. 68 | * See https://discord.com/developers/docs/resources/channel#create-message 69 | * 70 | * @param array $embed 71 | * 72 | * @return $this 73 | */ 74 | public function embed($embed) 75 | { 76 | $this->embed = $embed; 77 | 78 | return $this; 79 | } 80 | 81 | /** 82 | * Set the components object. 83 | * 84 | * @param array $components 85 | * 86 | * @return $this 87 | */ 88 | public function components($components) 89 | { 90 | $this->components = $components; 91 | 92 | return $this; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/DiscordServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->bind('command.discord:setup', SetupCommand::class); 16 | $this->commands('command.discord:setup'); 17 | } 18 | 19 | /** 20 | * Bootstrap the application services. 21 | */ 22 | public function boot() 23 | { 24 | $token = $this->app->make('config')->get('services.discord.token'); 25 | 26 | $this->app->when(Discord::class) 27 | ->needs('$token') 28 | ->give($token); 29 | 30 | $this->app->when(SetupCommand::class) 31 | ->needs('$token') 32 | ->give($token); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Exceptions/CouldNotSendNotification.php: -------------------------------------------------------------------------------- 1 | getStatusCode()}"; 21 | 22 | if ($error = Arr::get(json_decode($response->getBody(), true), 'message')) { 23 | $message .= ": $error"; 24 | } 25 | 26 | return new static($message, $code, $exception); 27 | } 28 | 29 | /** 30 | * @param array $response 31 | * @param int $code 32 | * 33 | * @return static 34 | */ 35 | public static function serviceRespondedWithAnApiError(array $response, $code, $exception = null) 36 | { 37 | return new static("Discord responded with an API error: {$response['code']}: {$response['message']}", $code); 38 | } 39 | 40 | /** 41 | * @param \Exception $exception 42 | * 43 | * @return static 44 | */ 45 | public static function serviceCommunicationError(Exception $exception) 46 | { 47 | return new static("Communication with Discord failed: {$exception->getCode()}: {$exception->getMessage()}", $exception->getCode(), $exception); 48 | } 49 | } 50 | --------------------------------------------------------------------------------