hesmatt/discord-hooker

There is no license information available for the latest version (v1.0.0) of this package.

A lightweight Discord webhook client library for PHP

v1.0.0 2023-07-15 20:02 UTC

This package is auto-updated.

Last update: 2024-04-15 21:41:47 UTC


README

Discord Hooker is a lighweight Discord webhook client library for PHP (>=7.0)

Installation

You just make your project require it via Composer 👀

composer require hesmatt/discord-hooker

Usage

It's pretty simple to use Discord Hooker, let's start with an example that includes just message.

Basic usage

use HesMatt\DiscordHooker\Client\Webhook;

$webhook = new Webhook('Your webhook _URL_');

$webhook->setMessage('I am a Discord Hooker');

$webhook->send();

The result

We can also set the username and image, you can do that via the Webhook settings in Discord as well, but this way we can 'enforce' a different one that's not set there.

Let's change the code a bit

use HesMatt\DiscordHooker\Client\Webhook;
use HesMatt\DiscordHooker\Dto\Embed\Embed;

$webhook = new Webhook('Your webhook _URL_');

$webhook->setMessage('I am a Discord Hooker');
$webhook->setUsername('Hooker');
$webhook->setAvatar('The url of your image'); //Note that this always has to be an URL, not a file!

$webhook->send();

The result

Using embeds

You are also able to build Embeds and send them, that's probably the thing you'll be using the most, to make your messages look sexy 🌶️

It's not hard to build and add Embed, and the best thing is that you can add as many of them as you want (Or as Discord will let you 😉)

Let's start by building an Embed, an Embed can be something as simple as just a title.

use HesMatt\DiscordHooker\Dto\Embed\Embed;

$embed = new Embed();

$embed->setTitle('I am a test embed');

//The description is optional and you don't have to set it, but I wanted to mention is as well :)
$embed->setDescription('I am  test embed description');

We need to send it now.

use HesMatt\DiscordHooker\Client\Webhook;
use HesMatt\DiscordHooker\Dto\Embed\Embed;

$webhook = new Webhook('Your webhook _URL_');
$embed = new Embed(); //Let's assume we have the different parts we've already built.

//You can use all of the settings for $webhook from earlier, to make the code shorter I won't be typing them again from now on.

$webhook->addEmbed($embed);

$webhook->send();

The result

The embeds have many settings, I'll be listing them here without 'The result' part, to not make it any longer than it is necessary.

Adding a footer to embed

use HesMatt\DiscordHooker\Dto\Embed\Embed;

$embed = new Embed();

$embed->setFooter('The footer text', 'The footer image URL');

//Setting the time part of footer, we can either use
$embed->setTimestamp(new DateTimeImmutable());
//or (To use current time)
$embed->setTimestampNow();

Setting a color

use HesMatt\DiscordHooker\Dto\Embed\Embed;

$embed = new Embed();

$embed->setColor('327424'); //Discord is using an int value of the color.

Setting a thumbnail

use HesMatt\DiscordHooker\Dto\Embed\Embed;

$embed = new Embed();

$embed->setThumbnail('Url of the thumbnail image');

Adding an author

use HesMatt\DiscordHooker\Dto\Embed\Embed;

$embed = new Embed();

$embed->setAuthor('Name of Author','Icon of author','Url of author');

Adding a field

use HesMatt\DiscordHooker\Dto\Embed\Embed;

$embed = new Embed();

$embed->addField('Text','Value',false); //The last parameter is whether you want the field to be inlined or no.

We can of course combine any of these together, note that at least one field is required. So you need either an actual field, or title/description.

Contributing

Contributions are always welcome!

License

MIT