fabrizio/laravel-restcord

1.0.2 2022-06-12 23:41 UTC

This package is auto-updated.

Last update: 2024-09-13 05:19:36 UTC


README

该仓库是基于 more-cores/laravel-restcord 的分支,已更新以兼容 PHP 8 和 Laravel 的最新版本。

Restcord 的小型流畅包装。

README 内容

功能

  • 将 Restcord 与 Laravel Socialite 集成,因此当前 OAuth 用户用于 API 调用(当使用 sessionHasDiscordToken 中间件时)
  • 通过 OAuth 处理 Webhooks 的创建(无需机器人)
  • 通过 OAuth 处理将机器人添加到公会(无需 WebSocket 连接)
  • 获取成员与公会关系的详细信息(角色、权限等)

安装

  1. 安装包
composer require more-cores/laravel-restcord:2.*
  1. 定义环境变量 DISCORD_BOT_TOKEN

  2. 对于需要使用当前 OAuth 用户凭据与 Discord API 交互的路由,添加中间件 sessionHasDiscordToken。这是必需的,因为在 ServiceProvider 中不可用会话信息。

  3. 对于 Laravel <= 5.4,在 config/app.php 中注册 service provider

'providers' => [
    ...
    LaravelRestcord\ServiceProvider::class,
]

环境变量

  • DISCORD_BOT_KEY
  • DISCORD_BOT_SECRET
  • DISCORD_BOT_TOKEN
  • DISCORD_KEY
  • DISCORD_SECRET

机器人密钥/密钥将用于添加机器人或创建 Webhook 的回调端点,以及当应用程序在控制台运行时,例如队列工作者和 cron。

用法

此文档假定您的用户正在通过 Discord 驱动程序 通过 Laravel Socialite 登录。

在文档中看到 $discord 时,假定它是一个 LaravelRestcord\Discord\Discord::class 的实例,该实例可从 Laravel 的 IOC 容器中获取。

公会

获取当前用户可访问的公会列表

$discord->guilds() // Guild[]

获取用户与公会关系的详细信息

$guild->userCan(Permission::KICK_MEMBERS); // bool - uses permissions of the currently oauth'd user

$member = $guild->getMemberById($discordUserId); // \LaravelRestcord\Discord\Member
$member->roles(); // \LaravelRestcord\Discord\Role[]
$member->joinedAt(); // Carbon

将机器人添加到公会

此实现使用高级机器人授权流程将机器人添加到公会。您应该在应用的设置中启用需要OAuth2代码授权选项。

use LaravelRestcord\Discord\HandlesBotAddedToGuild;
use Illuminate\Http\RedirectResponse;

class BotAddedToDiscordGuild
{
    use HandlesBotAddedToGuild;
    
    public function botAdded(Guild $guild) : RedirectResponse
    {
        // do something with the guild information the bot was added to
        
        return redirect('/to/this/page');
    }
}

接下来,在您的AppServiceProvider中添加一个绑定,以便在用户返回您的网站时,包知道将公会信息传递给哪个类。

 $this->app->bind(HandlesBotAddedToGuild::class, BotAddedToDiscordGuild::class);

现在您可以引导用户访问Discord的网站,以便他们可以选择添加机器人的公会。

    public function show(Guild $guild)
    {
        // Reference https://discordapi.com/permissions.html to determine
        // the permissions your bot needs
    
        $guild->sendUserToDiscordToAddBot($permissions);
    }

此包处理路由需求,但您需要将回调URL列入白名单才能使其工作。将http://MY-SITE.com/discord/bot-added添加到您的应用的跳转URI中。

当机器人被添加到公会时,您的处理程序将被触发。

一旦建立了WebSocket连接,您就可以通过此机器人发送消息。这只需要做一次,因此通常使用以下代码片段来实现:

"use strict";
var TOKEN="PUT YOUR TOKEN HERE";
fetch("https://discord.com/api/v7/gateway").then(function(a){return a.json()}).then(function(a){var b=new WebSocket(a.url+"/?encoding=json&v=6");b.onerror=function(a){return console.error(a)},b.onmessage=function(a){try{var c=JSON.parse(a.data);0===c.op&&"READY"===c.t&&(b.close(),console.log("Successful authentication! You may now close this window!")),10===c.op&&b.send(JSON.stringify({op:2,d:{token:TOKEN,properties:{$browser:"b1nzy is a meme"},large_threshold:50}}))}catch(a){console.error(a)}}});

创建Webhooks

因为我们使用OAuth创建Webhooks,所以用户将被引导到Discord的网站以选择公会/频道。此包处理此请求/响应生命周期的解释,所以您需要做的只是构建一个处理程序。

use LaravelRestcord\Discord\HandlesDiscordWebhooksBeingCreated;
use Illuminate\Http\RedirectResponse;

class Subscribe
{
    use HandlesDiscordWebhooksBeingCreated;
    
    public function webhookCreated(Webhook $webhook) : RedirectResponse
    {
        // $webhook->token();
        // Here you should save the token for use later when activating the webhook
        
        return redirect('/to/this/page');
    }
}

接下来,在您的AppServiceProvider中添加一个绑定,以便在用户返回您的网站时,包知道将Webhook数据传递给哪个类。

 $this->app->bind(HandlesDiscordWebhooksBeingCreated::class, DiscordChannelAdded::class);

现在您可以引导用户访问Discord的网站以创建Webhook。

    public function show(Guild $guild)
    {
        // redirects the user to Discord's interface for selecting
        // a guild and channel for the webhook
        $guild->sendUserToDiscordToCreateWebhook();
    }

此包处理路由需求,但您需要将回调URL列入白名单才能使其工作。将http://MY-SITE.com/discord/create-webhook添加到您的应用的跳转URI中。

当Webhook被创建时,您的处理程序将被触发。