Laravel Discord 通知驱动程序。

v1.0.0 2017-08-15 02:29 UTC

This package is not auto-updated.

Last update: 2024-09-20 20:05:56 UTC


README

Latest Version on Packagist Software License Build Status StyleCI SensioLabsInsight Quality Score Code Coverage Total Downloads

本软件包通过 Laravel 5.3 的 Discord bot API 轻松发送通知。

内容

安装

您可以通过 composer 安装此软件包

composer require laravel-notification-channels/discord

接下来,您必须加载服务提供者

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

设置您的 Discord 机器人

  1. 创建一个 Discord 应用程序。

  2. 在您的 Discord 应用程序中点击 创建一个 Bot 用户 按钮。

  3. 将您的机器人 API 令牌(在 App Bot User 下找到)粘贴到您的 services.php 配置文件中

    // config/services.php
    'discord' => [
        'token' => 'YOUR_API_TOKEN',
    ],
  4. 将机器人添加到您的服务器,并通过运行 artisan 命令来识别它

    php artisan discord:setup

使用方法

在每个您希望通过 Discord 通知的模型中,您必须添加一个通道 ID 属性,并通过 routeNotificationForDiscord 方法访问

class Guild extends Eloquent
{
    use Notifiable;

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

注意:Discord 将直接消息视为普通通道。如果您希望允许用户接收来自机器人的直接消息,您需要为该用户创建一个私有通道。以下是一个示例工作流程:

  1. 您的 users 表有两个 discord 列: discord_userdiscord_channel
  2. 当用户更新他们的 Discord 用户 ID(discord_user)时,生成并保存通道 ID(discord_channel
  3. 在用户模型的 routeNotificationForDiscord 方法中返回用户的 discord_channel

您可以使用 NotificationChannels\Discord\Discord 中的 getPrivateChannel 方法生成直接消息通道

use NotificationChannels\Discord\Discord;
// ...

class UserDiscordSettingsController
{
    public function store(Request $request)
    {
        $user = $request->input('discord_user');
        $channel = app(Discord::class)->getPrivateChannel($user);
        
        Auth::user()->update([
            'discord_user' => $user,
            'discord_channel' => $channel,
        ]);
    }
}

现在,您可以在 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}**!");
    }
}

可用的消息方法

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

测试

$ composer test

安全

如果您发现任何安全问题,请通过电子邮件 cs475x@icloud.com 而不是使用问题跟踪器。

贡献

有关详细信息,请参阅 CONTRIBUTING

致谢

许可协议

MIT 许可协议(MIT)。有关更多信息,请参阅 LICENSE