Skip to content

Commit 9e8a853

Browse files
author
code-raisan
committed
Pre-commit
0 parents  commit 9e8a853

File tree

11 files changed

+300
-0
lines changed

11 files changed

+300
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Composer
2+
vendor

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "ablaze/php-discord-webhook",
3+
"description": "Discord Webhook sending library for PHP",
4+
"type": "library",
5+
"homepage": "https://github.com/Ablaze-MIRAI/php-discord-webhook",
6+
"keywords": [
7+
"discord",
8+
"webhook"
9+
],
10+
"license": "MIT",
11+
"autoload": {
12+
"psr-4": {
13+
"Ablaze\\PhpDiscordWebhook\\": "src/"
14+
}
15+
},
16+
"authors": [
17+
{
18+
"name": "code-raisan",
19+
"email": "[email protected]"
20+
}
21+
],
22+
"require": {}
23+
}

composer.lock

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Sample code
2+
3+
### `embed_send_sample.php`
4+
5+
Here is a sample of a basic message sent
6+
7+
### `embed_send_sample.php`
8+
9+
Here is a sample of sending Embed message

sample/embed_send_sample.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
require_once __DIR__."/../vendor/autoload.php";
4+
5+
use Ablaze\PhpDiscordWebhook\Webhook;
6+
use Ablaze\PhpDiscordWebhook\Embed;
7+
8+
$embed = new Embed("Title", "Description");
9+
$embed->setColor("#FFFFFF");
10+
$embed->setAuthor("Author Name", "https://example.com", "https://example.img");
11+
$embed->setFooter("Footer", "https://example.img");
12+
$embed->setImage("https://example.img");
13+
$embed->setThumbnail("https://example.img");
14+
$embed->addField("Field 1", "value");
15+
$embed->addField("Field 2", "value(inline)", true);
16+
$embed->addField("Field 3", "value(inline)", true);
17+
18+
$message = new Webhook("User Name", "https://github.com/qiita.png", "Message Content");
19+
$message->addEmbed($embed);
20+
$response = $message->send("https://discord.com/api/webhooks/****/****");
21+
22+
if($message->ok()){
23+
echo "success";
24+
}else{
25+
echo "error";
26+
27+
// Request info
28+
var_dump($response);
29+
}

sample/image/icon.png

9.9 KB
Loading

sample/image/image.png

47.5 KB
Loading

sample/image/thumbnail.png

8.57 KB
Loading

sample/message_send_sample.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
require_once __DIR__."/../vendor/autoload.php";
4+
5+
use Ablaze\PhpDiscordWebhook\Webhook;
6+
7+
$message = new Webhook("User Name", "https://github.com/qiita.png", "Message Content");
8+
$response = $message->send("https://discord.com/api/webhooks/****/****");
9+
10+
if($message->ok()){
11+
echo "success";
12+
}else{
13+
echo "error";
14+
15+
// Request info
16+
var_dump($response);
17+
}

src/Embed.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
namespace Ablaze\PhpDiscordWebhook;
4+
5+
class Embed {
6+
/**
7+
* Initialize class and base content
8+
*
9+
* @param string $title Embed title
10+
* @param string $description Embed description
11+
* @return void
12+
*/
13+
public function __construct(string $title=null, string $description=null)
14+
{
15+
$this->embed_body = [];
16+
$this->embed_body["fields"] = [];
17+
if($title) $this->embed_body["title"] = $title;
18+
if($description) $this->embed_body["description"] = $description;
19+
}
20+
21+
/**
22+
* Set url
23+
*
24+
* @param string $title Embed url
25+
* @return void
26+
*/
27+
public function setURL(string $URL)
28+
{
29+
$this->embed_body["url"] = $URL;
30+
}
31+
32+
/**
33+
* Set timestamp
34+
*
35+
* @param DateTime $timestamp Embed timestamp
36+
* @return void
37+
*/
38+
public function setTimestamp(\DateTime $timestamp)
39+
{
40+
$this->embed_body["timestamp"] = $timestamp->format(\DateTime::ISO8601);
41+
}
42+
43+
/**
44+
* Set color
45+
*
46+
* @param string $color Embed color(Hex)
47+
* @return void
48+
*/
49+
public function setColor(string $color)
50+
{
51+
//$hex = str_replace($color, "#", "");
52+
$this->embed_body["color"] = hexdec($color);
53+
}
54+
55+
/**
56+
* Set footer
57+
*
58+
* @param string $text Embed footer text
59+
* @param string $icon_url Embed footer icon url
60+
* @return void
61+
*/
62+
public function setFooter(string $text=null, string $icon_url=null)
63+
{
64+
if($text) $this->embed_body["footer"]["text"] = $text;
65+
if($icon_url) $this->embed_body["footer"]["icon_url"] = $icon_url;
66+
}
67+
68+
/**
69+
* Set image
70+
*
71+
* @param string $URL Embed main image url
72+
* @return void
73+
*/
74+
public function setImage(string $URL)
75+
{
76+
$this->embed_body["image"]["url"] = $URL;
77+
}
78+
79+
/**
80+
* Set thumbnail
81+
*
82+
* @param string $URL Embed thumbanil image url
83+
* @return void
84+
*/
85+
public function setThumbnail(string $URL)
86+
{
87+
$this->embed_body["thumbnail"]["url"] = $URL;
88+
}
89+
90+
/**
91+
* Set author
92+
*
93+
* @param string $name Embed author name
94+
* @param string $URL Embed author url
95+
* @param string $icon_url Embed author icon url
96+
* @return void
97+
*/
98+
public function setAuthor(string $name=null, string $URL=null, string $icon_url=null)
99+
{
100+
if($name) $this->embed_body["author"]["name"] = $name;
101+
if($URL) $this->embed_body["author"]["url"] = $URL;
102+
if($icon_url) $this->embed_body["author"]["icon_url"] = $icon_url;
103+
}
104+
105+
/**
106+
* Add field
107+
*
108+
* @param string $name Embed field name
109+
* @param string $URL Embed field value
110+
* @param string $icon_url Embed field inline
111+
* @return void
112+
*/
113+
public function addField(string $name=null, string $value=null, bool $inline=false)
114+
{
115+
$field = ["inline" => $inline];
116+
if($name) $field["name"] = $name;
117+
if($value) $field["value"] = $value;
118+
array_push($this->embed_body["fields"], $field);
119+
}
120+
};

0 commit comments

Comments
 (0)