|
1 |
| -# new-channels |
2 |
| -Submit pull requests here for new notification channels |
| 1 | +# Discord notification channel for Laravel 5.3 [WIP] |
| 2 | + |
| 3 | +[](https://packagist.org/packages/laravel-notification-channels/discord) |
| 4 | +[](LICENSE.md) |
| 5 | +[](https://travis-ci.org/laravel-notification-channels/discord) |
| 6 | +[](https://styleci.io/repos/:style_ci_id) |
| 7 | +[](https://insight.sensiolabs.com/projects/:sensio_labs_id) |
| 8 | +[](https://scrutinizer-ci.com/g/laravel-notification-channels/discord) |
| 9 | +[](https://scrutinizer-ci.com/g/laravel-notification-channels/discord/?branch=master) |
| 10 | +[](https://packagist.org/packages/laravel-notification-channels/discord) |
| 11 | + |
| 12 | +This package makes it easy to send notifications using the [Discord bot API](https://discordapp.com/developers/docs/intro) with Laravel 5.3. |
| 13 | + |
| 14 | +## Contents |
| 15 | + |
| 16 | +- [Installation](#installation) |
| 17 | + - [Setting up your Discord bot](#setting-up-your-discord-bot) |
| 18 | +- [Usage](#usage) |
| 19 | + - [Available Message methods](#available-message-methods) |
| 20 | +- [Changelog](#changelog) |
| 21 | +- [Testing](#testing) |
| 22 | +- [Security](#security) |
| 23 | +- [Contributing](#contributing) |
| 24 | +- [Credits](#credits) |
| 25 | +- [License](#license) |
| 26 | + |
| 27 | + |
| 28 | +## Installation |
| 29 | + |
| 30 | +You can install the package via composer: |
| 31 | + |
| 32 | +`composer require laravel-notification-channels/discord` |
| 33 | + |
| 34 | +Next, you must load the service provider: |
| 35 | + |
| 36 | +```php |
| 37 | +// config/app.php |
| 38 | +'providers' => [ |
| 39 | + // ... |
| 40 | + NotificationChannels\Discord\DiscordServiceProvider::class, |
| 41 | +] |
| 42 | +``` |
| 43 | + |
| 44 | +### Setting up your Discord bot |
| 45 | + |
| 46 | +1. [Create a Discord application.](https://discordapp.com/developers/applications/me/create) |
| 47 | +2. Click the `Create a Bot User` button on your Discord application. |
| 48 | +3. Paste your bot's API token, found under `App Bot User`, in your `services.php` config file: |
| 49 | + |
| 50 | + ```php |
| 51 | + // config/services.php |
| 52 | + 'discord' => [ |
| 53 | + 'token' => 'YOUR_API_TOKEN', |
| 54 | + ], |
| 55 | + ``` |
| 56 | + |
| 57 | +4. Add the bot to your server and identify it by running the artisan command: |
| 58 | + |
| 59 | + ```shell |
| 60 | + php artisan discord:setup |
| 61 | + ``` |
| 62 | + |
| 63 | +## Usage |
| 64 | + |
| 65 | +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: |
| 66 | + |
| 67 | +```php |
| 68 | +class Guild extends Eloquent |
| 69 | +{ |
| 70 | + use Notifiable; |
| 71 | + |
| 72 | + public function routeNotificationForPushover() |
| 73 | + { |
| 74 | + return $this->discord_channel; |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +> **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. |
| 80 | +> An example workflow may look like the following: |
| 81 | +> |
| 82 | +> 1. Your `users` table has two discord columns: `discord_user` and `discord_channel` |
| 83 | +> 2. When a user updates their Discord user ID (`discord_user`), generate and save a channel ID (`discord_channel`) |
| 84 | +> 3. Return the user's `discord_channel` in the `routeNotificationForDiscord` method on the User model |
| 85 | +> |
| 86 | +> You can generate direct message channels by using the `getPrivateChannel` method in `NotificationChannels\Discord\Discord`: |
| 87 | +> |
| 88 | +> ```php |
| 89 | +> use NotificationChannels\Discord\Discord; |
| 90 | +> // ... |
| 91 | +> |
| 92 | +> class UserDiscordSettingsController |
| 93 | +> { |
| 94 | +> public function store(Request $request) |
| 95 | +> { |
| 96 | +> $user = $request->input('discord_user'); |
| 97 | +> $channel = app(Discord::class)->getPrivateChannel($user); |
| 98 | +> |
| 99 | +> Auth::user()->update([ |
| 100 | +> 'discord_user' => $user, |
| 101 | +> 'discord_channel' => $channel, |
| 102 | +> ]); |
| 103 | +> } |
| 104 | +> } |
| 105 | +> ``` |
| 106 | +
|
| 107 | +You may now tell Laravel to send notifications to Discord channels in the `via` method: |
| 108 | +
|
| 109 | +```php |
| 110 | +class GameChallengeNotification extends Notification |
| 111 | +{ |
| 112 | + public $channelger; |
| 113 | +
|
| 114 | + public $game; |
| 115 | +
|
| 116 | + public function __construct(Guild $challenger, Game $game) |
| 117 | + { |
| 118 | + $this->challenger = $challenger; |
| 119 | + $this->game = $game; |
| 120 | + } |
| 121 | + public function via($notifiable) |
| 122 | + { |
| 123 | + return [DiscordChannel::class]; |
| 124 | + } |
| 125 | +
|
| 126 | + public function toDiscord($notifiable) |
| 127 | + { |
| 128 | + return (new DiscordMessage) |
| 129 | + ->body("You have been challenged to a game of *{$this->game->name}* by **{$this->channelger->name}**!"); |
| 130 | + } |
| 131 | +} |
| 132 | +``` |
| 133 | +
|
| 134 | +### Available Message methods |
| 135 | + |
| 136 | +* `body(string)`: Set the content of the message. ([Supports basic markdown](https://support.discordapp.com/hc/en-us/articles/210298617-Markdown-Text-101-Chat-Formatting-Bold-Italic-Underline-)) |
| 137 | + |
| 138 | +## Changelog |
| 139 | + |
| 140 | +Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. |
| 141 | + |
| 142 | +## Testing |
| 143 | + |
| 144 | +``` bash |
| 145 | +$ composer test |
| 146 | +``` |
| 147 | + |
| 148 | +## Security |
| 149 | + |
| 150 | +If you discover any security related issues, please email [email protected] instead of using the issue tracker. |
| 151 | + |
| 152 | +## Contributing |
| 153 | + |
| 154 | +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 155 | + |
| 156 | +## Credits |
| 157 | + |
| 158 | +- [Cody Scott](https://github.com/cs475x) |
| 159 | +- [All Contributors](../../contributors) |
| 160 | + |
| 161 | +## License |
| 162 | + |
| 163 | +The MIT License (MIT). Please see [LICENSE](LICENSE.md) for more information. |
0 commit comments