ilyahoilik / discord-notification-channel
Laravel 用于 Discord 的通知驱动程序。
Requires
- php: ^8.0
- ext-json: *
- guzzlehttp/guzzle: ^6.3 || ^7.0
- illuminate/console: ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
- illuminate/notifications: ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
- illuminate/queue: ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
- illuminate/support: ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
- textalk/websocket: ^1.2
Requires (Dev)
- mockery/mockery: ^1.3.3
- orchestra/testbench: ^6.0 || ^7.0 || ^8.0 || ^9.0
- phpunit/phpunit: ^8.5 || ^9.0 || ^10.5
This package is auto-updated.
Last update: 2024-09-26 13:26:44 UTC
README
此包使得使用 Laravel 通过 Discord bot API 发送通知变得简单。
内容
安装
您可以通过 composer 安装此包
composer require laravel-notification-channels/discord
接下来,您必须加载服务提供者
// config/app.php 'providers' => [ // ... NotificationChannels\Discord\DiscordServiceProvider::class, ],
设置 Discord 机器人
-
点击 Discord 应用程序中的
创建一个 Bot 用户
按钮。 -
将您的机器人 API 令牌粘贴到
services.php
配置文件中的App Bot User
下// config/services.php 'discord' => [ 'token' => 'YOUR_API_TOKEN', ],
-
将机器人添加到您的服务器,并通过运行 artisan 命令来识别它
php artisan discord:setup
用法
在每个您希望通过 Discord 通知的模型中,您必须添加一个通道 ID 属性,并通过 routeNotificationForDiscord
方法访问它
class Guild extends Eloquent { use Notifiable; public function routeNotificationForDiscord() { return $this->discord_channel; } }
注意:Discord 将直接消息视为常规通道。如果您希望允许用户接收来自机器人的直接消息,您需要为该用户创建一个私密通道。
以下是一个示例工作流程
- 您的
users
表有两个 Discord 列:discord_user_id
和discord_private_channel_id
- 当用户更新他们的 Discord 用户 ID (
discord_user_id
) 时,生成并保存私密通道 ID (discord_private_channel_id
)- 在
User
模型的routeNotificationForDiscord
方法中返回用户的discord_private_channel_id
您可以使用
NotificationChannels\Discord\Discord
类中的getPrivateChannel
方法生成直接消息通道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, ]); } }请注意,
getPrivateChannel
方法仅接受 Discord 的 snowflake IDs。Discord 没有提供通过名称和标签查找用户 ID 的 API 路由,复制和粘贴用户 ID 的过程可能会让一些用户感到困惑。因此,建议用户通过使用 Discord 登录或将其链接到现有账户,将 Discord 账户与其在应用程序中的账户连接起来。
现在您可以使用 via
方法告诉 Laravel 向 Discord 通道发送通知
// ... 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}**!"); } }
可用的消息方法
body(string)
:设置消息内容。(支持基本 Markdown)embed(array)
:设置嵌入式内容。(查看嵌入式结构)components(array)
:设置组件内容。(查看组件结构)
变更日志
请参阅 CHANGELOG 了解最近更改的详细信息。
测试
$ composer test
安全
如果您发现任何与安全相关的问题,请通过电子邮件发送到 cs475x@icloud.com,而不是使用问题跟踪器。
贡献
有关详细信息,请参阅 CONTRIBUTING。
鸣谢
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 LICENSE。