Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
add more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
EnterVPL committed Sep 8, 2023
1 parent 0125060 commit cba876d
Show file tree
Hide file tree
Showing 18 changed files with 218 additions and 5 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ Discord webhooks is a professional client for Discord's webhook API.

### Requirements

- PHP >= 8.1
- [PHP](https://www.php.net/) >= 8.1
- [Composer](https://getcomposer.org/)

### Install via composer (not available yet)

Expand Down Expand Up @@ -45,8 +46,6 @@ $payload->setUsername('Example Webhook Bot') // Change discord bot webhook usern
$webhook = new WebhookClient();
// Send message
$webhook->send($url, $payload);


```

## License
Expand Down
4 changes: 3 additions & 1 deletion docs/examples/MessageBuilderExample.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Simple Message Example
# Message Builder Example

```php
<?php
Expand Down Expand Up @@ -42,3 +42,5 @@ $payload->setMessage($message);
$webhook = new WebhookClient();
$webhook->send($url, $payload);
```

You can also use [Message Builder with embed](./MessageBuilderWithEmbedExample.md) e.g. to field or description
46 changes: 46 additions & 0 deletions docs/examples/MessageBuilderWithEmbedExample.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Message Builder with Embed Example

```php
<?php
use EnterV\DiscordWebhooks\Builder\TextFormattingCombine;
use EnterV\DiscordWebhooks\Builder\TextMessageBuilder;
use EnterV\DiscordWebhooks\Payload;
use EnterV\DiscordWebhooks\WebhookClient;

$url = 'DISCORD_WEBHOOK_URL'; // Put your discord webhook url

// Builder with on auto-newline mode (to disable auto-newline use new TextMessageBuilder(false))
$messageBuilder = new TextMessageBuilder();
$text = $messageBuilder->addText('How are you?') // some text without format
->addBold("It's time for new webhook tool!") // **Text with bold**
/**
* Text with multiline codeblock:
* ```php
* // It's can using codeblock
* ```
*/
->addMultilineCodeBlock("// It's can using codeblock", 'php')
->addQuoteBlock('And some quote!') // > Text with quote block
->addCombineTextFormatting(
"It's awesome!!!111oneone",
(new TextFormattingCombine())
->withBold()
->withItalic()
->withUnderline()
) // ***__Text with: Bold, Italic and Underline__***
->addText('Are you reade for this??')
->build() // build message (it was returned string)
;

// Create embed
$embed = new Embed();
$embed->addField('Field title', $text); // add field

// Create Payload
$payload = new Payload();
$payload->addEmbed($embed) // Add embed

// Send message
$webhook = new WebhookClient();
$webhook->send($url, $payload);
```
18 changes: 18 additions & 0 deletions docs/examples/OnlyMessage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Simple example

```php
<?php
use EnterV\DiscordWebhooks\Payload;
use EnterV\DiscordWebhooks\WebhookClient;

$url = 'DISCORD_WEBHOOK_URL'; // Put your discord webhook url

// Create Payload
$payload = new Payload();
$payload->setMessage('This is a message'); // Some text. If You need use the Message Builder

// New discord webhook client
$webhook = new WebhookClient();
// Send message
$webhook->send($url, $payload);
```
5 changes: 5 additions & 0 deletions src/Builder/Interface/GetTextFormattingCombineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace EnterV\DiscordWebhooks\Builder\Interface;

/**
* All public methods that can be used to check whether a given format has been used.
*
* @see https://github.com/EnterVPL/discord-webhooks/blob/master/docs/TextFormattingCombine.md
*/
interface GetTextFormattingCombineInterface
{
public function isBold(): bool;
Expand Down
8 changes: 8 additions & 0 deletions src/Builder/Interface/MessageBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@

namespace EnterV\DiscordWebhooks\Builder\Interface;

/**
* Everything you need to build a formatted message.
*
* @see https://github.com/EnterVPL/discord-webhooks/blob/master/docs/TextMessageBuilder.md
*/
interface MessageBuilderInterface extends SetMessageBuilderInterface
{
/**
* Returns a formatted string consistent with Discord message formatting.
*/
public function build(): string;
}
11 changes: 11 additions & 0 deletions src/Builder/Interface/SetMessageBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@

namespace EnterV\DiscordWebhooks\Builder\Interface;

/**
* All methods that add text, following Discord's formatting.
*
* @see https://github.com/EnterVPL/discord-webhooks/blob/master/docs/TextMessageBuilder.md
*/
interface SetMessageBuilderInterface
{
/**
* Adds text to the message, without formatting.
*/
public function addText(string $text): static;

/**
* Adds a new line.
*/
public function addNewLine(): static;

/**
Expand Down
5 changes: 5 additions & 0 deletions src/Builder/Interface/SetTextFormattingCombineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace EnterV\DiscordWebhooks\Builder\Interface;

/**
* All public methods that can be used to set a format combination. Use only those that are needed.
*
* @see https://github.com/EnterVPL/discord-webhooks/blob/master/docs/TextFormattingCombine.md
*/
interface SetTextFormattingCombineInterface
{
public function withBold(): static;
Expand Down
5 changes: 5 additions & 0 deletions src/Builder/Interface/TextFormattingCombineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace EnterV\DiscordWebhooks\Builder\Interface;

/**
* Everything you need to be able to create text with a combination of formats.
*
* @see https://github.com/EnterVPL/discord-webhooks/blob/master/docs/TextFormattingCombine.md
*/
interface TextFormattingCombineInterface extends SetTextFormattingCombineInterface, GetTextFormattingCombineInterface
{
}
9 changes: 9 additions & 0 deletions src/Helper/ColorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@

use EnterV\DiscordWebhooks\Exception\Color\ColorException;

/**
* Helps with the Color class.
*/
class ColorHelper
{
public const SHORT_RGB_REGEX = '/^#?([A-Fa-f0-9]{3})$/';
public const RGB_REGEX = '/^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/';

/**
* Checks whether the RGB code specified in HEX is correct.
*/
public static function validRgb(string $rgb): bool
{
if (preg_match(static::RGB_REGEX, $rgb)) {
Expand All @@ -21,6 +27,9 @@ public static function validRgb(string $rgb): bool
}

/**
* Converts short RGB format to regular format.
* If you provide a regular format, then it will be returned.
*
* @throws ColorException
*/
public static function convertShortRGBToFull(string $rgb): string
Expand Down
9 changes: 9 additions & 0 deletions src/Interface/Color/ColorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@

namespace EnterV\DiscordWebhooks\Interface\Color;

/**
* @see https://github.com/EnterVPL/discord-webhooks/blob/master/docs/Color.md
*/
interface ColorInterface
{
/**
* Set the color with using RGB HEX format (eg. FAFAFA or short code FDA).
*/
public function setRgb(string $rgb): void;

/**
* Convert the color to decimal format.
*/
public function toInt(): int|float;
}
5 changes: 5 additions & 0 deletions src/Interface/Embed/EmbedInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace EnterV\DiscordWebhooks\Interface\Embed;

/**
* A full-fledged interface for the Embed class.
*
* @see https://github.com/EnterVPL/discord-webhooks/blob/master/docs/Embed.md
*/
interface EmbedInterface extends SetEmbedInterface, GetEmbedInterface
{
}
7 changes: 7 additions & 0 deletions src/Interface/Embed/GetEmbedInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@

namespace EnterV\DiscordWebhooks\Interface\Embed;

/**
* Data that Embed should return.
*
* @see https://github.com/EnterVPL/discord-webhooks/blob/master/docs/Embed.md
*/
interface GetEmbedInterface
{
/**
* Convert all embed properties to array foramt.
*
* @return array<string, null|array<mixed>|float|int|string>
*/
public function toArray(): array;
Expand Down
36 changes: 36 additions & 0 deletions src/Interface/Embed/SetEmbedInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,61 @@

use EnterV\DiscordWebhooks\Interface\Color\ColorInterface;

/**
* Interface for public setters for the Embed class.
*
* @see https://github.com/EnterVPL/discord-webhooks/blob/master/docs/Embed.md
*/
interface SetEmbedInterface
{
/**
* Sets the title of the embedded content.
*/
public function setTitle(string $title): static;

/**
* Sets the url link to the title of the embedded content.
*/
public function setTitleUrl(string $url): static;

/**
* Sets the description of the embedded content.
*/
public function setDescription(string $description): static;

/**
* Sets the time at which the embedded content was created.
*/
public function setTimestamp(\DateTimeInterface $timestamp): static;

/**
* Sets the color of the embedded content bar.
*/
public function setColor(ColorInterface $color): static;

/**
* Sets the footer for embedded content.
*/
public function setFooter(string $text, null|string $iconUrl = null): static;

/**
* Sets the image of the embedded content.
*/
public function setImage(string $url): static;

/**
* Sets the thumbnail visible at the top right of embedded content.
*/
public function setThumbnail(string $url): static;

/**
* Sets the author of the embedded content.
*/
public function setAuthor(string $name, string $url, string $iconUrl): static;

/**
* Adds a field to embedded content.
* You can add several different fields. They are displayed in the order they were added.
*/
public function addField(string $name, null|bool|int|float|string $value, bool $inline = false): static;
}
7 changes: 7 additions & 0 deletions src/Interface/Payload/GetPayloadInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@

namespace EnterV\DiscordWebhooks\Interface\Payload;

/**
* Data that Payload should return.
*
* @see https://github.com/EnterVPL/discord-webhooks/blob/master/docs/Payload.md
*/
interface GetPayloadInterface
{
/**
* Convert all properties to array foramt.
*
* @return array<mixed>
*/
public function toArray(): array;
Expand Down
5 changes: 5 additions & 0 deletions src/Interface/Payload/PayloadInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace EnterV\DiscordWebhooks\Interface\Payload;

/**
* A full-fledged interface for the Payload class.
*
* @see https://github.com/EnterVPL/discord-webhooks/blob/master/docs/Payload.md
*/
interface PayloadInterface extends GetPayloadInterface, SetPayloadInterface
{
}
24 changes: 24 additions & 0 deletions src/Interface/Payload/SetPayloadInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,39 @@

use EnterV\DiscordWebhooks\Interface\Embed\GetEmbedInterface;

/**
* Interface for public setters for the Payload class.
*
* @see https://github.com/EnterVPL/discord-webhooks/blob/master/docs/Payload.md
*/
interface SetPayloadInterface
{
/**
* Sets webhook bot name.
*/
public function setUsername(string $username): static;

/**
* Sets webhook bot avatar.
* The image format must be compatible with the API.
* I prefer that the avatar weighs a maximum of 25 kB (just to make it load quickly).
*
* See compatible formats: https://discord.com/developers/docs/reference#image-formatting
*/
public function setAvatarUrl(string $avatarUrl): static;

/**
* The message that is displayed before embed.
*/
public function setMessage(string $message): static;

/**
* Adds embedded content.
*/
public function addEmbed(GetEmbedInterface $embed): static;

/**
* Sets text-to-speech (off by default).
*/
public function setTts(bool $tts): static;
}
14 changes: 13 additions & 1 deletion src/Interface/WebhookClient/WebhookClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@
use EnterV\DiscordWebhooks\Interface\Payload\GetPayloadInterface;
use Psr\Http\Message\ResponseInterface;

/**
* A full-fledged interface for the WebhookClient class.
*
* @see https://github.com/EnterVPL/discord-webhooks/blob/master/docs/WebhookClient.md
*/
interface WebhookClientInterface
{
public function send(string $url, GetPayloadInterface $payload): ResponseInterface;
/**
* Execute Webhook.
*
* @see https://discord.com/developers/docs/resources/webhook#execute-webhook
*
* @param array $options - see https://docs.guzzlephp.org/en/stable/request-options.html
*/
public function send(string $url, GetPayloadInterface $payload, array $options = []): ResponseInterface;

Check failure on line 24 in src/Interface/WebhookClient/WebhookClientInterface.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method EnterV\DiscordWebhooks\Interface\WebhookClient\WebhookClientInterface::send() has parameter $options with no value type specified in iterable type array.
}

0 comments on commit cba876d

Please sign in to comment.