n445/easydiscord

Easy Discord

安装: 45

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

公开问题: 0

类型:symfony-bundle

1.6.6 2020-06-03 20:04 UTC

This package is auto-updated.

Last update: 2024-09-29 05:36:20 UTC


README

步骤 1: 下载包

composer require n445/easydiscord

步骤 2: 启用包

然后,通过将其添加到项目中的 app/AppKernel.php 文件中注册的包列表中,启用该包

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...
            new N445\EasyDiscord\N445EasyDiscordBundle(),
        ];

        // ...
    }

    // ...
}

步骤 3: 使用

仅发送嵌入消息

<?php

use N445\EasyDiscord\Helper\Colors;
use N445\EasyDiscord\Model\Author;
use N445\EasyDiscord\Model\Embed;
use N445\EasyDiscord\Model\Field;
use N445\EasyDiscord\Model\Footer;
use N445\EasyDiscord\Model\Image;
use N445\EasyDiscord\Model\Message;
use N445\EasyDiscord\Model\Thumbnail;
use N445\EasyDiscord\Service\DiscordSender;

// Init one embed
$embed = (new Embed())
    ->setTitle('My embed from EasyDiscord')
    ->setDescription('My wonderful message')
    ->setUrl('https://github.com/N445') // optional
    ->setColor(Colors::GREEN) // optional
;

// Add author optional
$author = (new Author())
    ->setName('N445')
    ->setUrl('https://github.com/N445')
    ->setIconUrl('https://avatars0.githubusercontent.com/u/25900291?s=460&v=4')
;
$embed->setAuthor($author);

// Add image optional
$image = (new Image())
    ->setUrl('https://images.unsplash.com/photo-1517976384346-3136801d605d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1300&q=80');
$embed->setImage($image);

// Add thumbnail optional
$thumbnail = (new Thumbnail())
    ->setUrl('https://images.unsplash.com/photo-1517976384346-3136801d605d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1300&q=80');
$embed->setThumbnail($thumbnail);

// Add footer optional
$footer = (new Footer())
    ->setText('Footer !');
$embed->setFooter($footer);

// Add fields optional
$field1 = (new Field())
    ->setName('field 1')
    ->setValue('value field 1')
    ->setInline(true)
;

$field2 = (new Field())
    ->setName('field 2')
    ->setValue('value field 2')
    ->setInline(true)
;

$field3 = (new Field())
    ->setName('field 3')
    ->setValue('value field 3')
;

$embed->addField($field1);
$embed->addField($field2);
$embed->addField($field3);
//or
$embed->setFields([
    $field1,
    $field2,
    $field3,
]);

// Init your message
$message = (new Message())
    ->setUsername('My super bot')
    ->addEmbed($embed)
;


// You can add multiple embed
// Init another embed
$embed2 = (new Embed())
    ->setTitle('other title')
    ->setDescription('My wonderful message 2')
;
$message->addEmbed($embed2);

$id = 'MY_WEBHOOK_ID';
$token = 'MY_WEBHOOK_TOKEN';

(new DiscordSender($id, $token))
    ->send($message);

文档

https://discord.com/developers/docs/resources/webhook#execute-webhook https://discord.com/developers/docs/resources/channel#embed-object