more-cores/laravel-restcord

此包已被弃用且不再维护。未建议替代包。

v3.0.3 2020-09-16 12:11 UTC

README

Restcord 的小型流畅包装器。

此项目已废弃,详细信息请见 https://gitlab.com/more-cores/laravel-restcord/-/issues/38

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 中无法访问会话信息。
  1. 对于 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 添加到您的 应用程序的 redirect uris

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

> 一旦建立了 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创建webhook,用户将被引导至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创建时,您的处理程序将被触发。