Skip to content

Commit

Permalink
Updated for PHP8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
supercrafter333 committed Aug 24, 2023
1 parent 74f0599 commit 524d836
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 51 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ Now that the embed has been constructed and has a valid content, we will have to
```php
$msg->addEmbed($embed);
```

You can even enable specific mentions, using the following statement: `Message->getAllowedMentions`. The only things you need are discord snowflakes/ids.
```php
$msg->getAllowedMentions()->addUser($userId1, $userId2); // Only the two users corresponding with these two ids will be mentionend
$msg->getAllowedMentions()->addRole($roleId1, $roleId2); // Now also all the people with $roleId1 and $roleId2 will be mentioned
```

But if you want to supress every mention out of that message you can use following method.
```php
$msg->getAllowedMentions()->suppressAll();
```

**That's all for the Basic Usage of the API. To learn more, You can explore it by reading the API's source code yourself (the code is simple and explanatory) or by using your favorite IDE to index it yourself. :3**
# Sample Code used to test this API earlier:
```php
Expand All @@ -73,6 +85,7 @@ $msg = new Message();
$msg->setUsername("USERNAME");
$msg->setAvatarURL("https://cortexpe.xyz/utils/kitsu.png");
$msg->setContent("INSERT TEXT HERE");
$msg->suppressAll();

// Create an embed object with #FF0000 (RED) as the embed's color and "EMBED 1" as the title
$embed = new Embed();
Expand Down
4 changes: 2 additions & 2 deletions src/supercrafter333/DiscordWebhooksX/Embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@

class Embed
{
/** @var array */
protected $data = [];

protected array $data = [];

public function asArray(): array
{
Expand Down
17 changes: 14 additions & 3 deletions src/supercrafter333/DiscordWebhooksX/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
namespace supercrafter333\DiscordWebhooksX;


use CortexPE\DiscordWebhookAPI\AllowedMentions;

class Message implements \JsonSerializable
{
/** @var array */
protected $data = [];

protected array $data = [];

public function __construct(array $embeds = null)
{
Expand Down Expand Up @@ -91,7 +93,16 @@ public function setTextToSpeech(bool $ttsEnabled): self
return $this;
}

public function jsonSerialize()
public function getAllowedMentions(): AllowedMentions
{
if (array_key_exists("allowed_mentions", $this->data)) {
return $this->data["allowed_mentions"];
}

return $this->data["allowed_mentions"] = new AllowedMentions();
}

public function jsonSerialize(): array
{
return $this->data;
}
Expand Down
30 changes: 15 additions & 15 deletions src/supercrafter333/DiscordWebhooksX/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@
use supercrafter333\DiscordWebhooksX\task\DiscordWebhookSendTask;
use pocketmine\Server;

class Webhook {
/** @var string */
protected $url;
class Webhook
{

public function __construct(string $url){
$this->url = $url;
}
public function __construct(protected readonly string $url) {}

public function getURL(): string{
return $this->url;
}
public function getURL(): string
{
return $this->url;
}

public function isValid(): bool{
return filter_var($this->url, FILTER_VALIDATE_URL) !== false;
}
public function isValid(): bool
{
return filter_var($this->url, FILTER_VALIDATE_URL) !== false;
}

public function send(Message $message): void{
Server::getInstance()->getAsyncPool()->submitTask(new DiscordWebhookSendTask($this, $message));
}
public function send(Message $message): void
{
Server::getInstance()->getAsyncPool()->submitTask(new DiscordWebhookSendTask($this, $message));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,29 @@
use pocketmine\scheduler\AsyncTask;
use pocketmine\Server;

class DiscordWebhookSendTask extends AsyncTask {
/** @var Webhook */
protected $webhook;
/** @var Message */
protected $message;

public function __construct(Webhook $webhook, Message $message){
$this->webhook = $webhook;
$this->message = $message;
}

public function onRun():void{
$ch = curl_init($this->webhook->getURL());
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->message));
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
$this->setResult([curl_exec($ch), curl_getinfo($ch, CURLINFO_RESPONSE_CODE)]);
curl_close($ch);
}

public function onCompletion():void{
$response = $this->getResult();
if(!in_array($response[1], [200, 204])){
Server::getInstance()->getLogger()->error("[DiscordWebhooksX] Got error ({$response[1]}): " . $response[0]);
}
}
class DiscordWebhookSendTask extends AsyncTask
{

public function __construct(protected readonly Webhook $webhook, protected readonly Message $message) {}

public function onRun():void
{
$ch = curl_init($this->webhook->getURL());
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->message));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
$this->setResult([curl_exec($ch), curl_getinfo($ch, CURLINFO_RESPONSE_CODE)]);
curl_close($ch);
}

public function onCompletion():void
{
$response = $this->getResult();
if (!in_array($response[1], [200, 204])) {
Server::getInstance()->getLogger()->error("[DiscordWebhooksX] Got error ({$response[1]}): " . $response[0]);
}
}
}
4 changes: 2 additions & 2 deletions virion.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: DiscordWebhooksX
antigen: supercrafter333\DiscordWebhooksX
api: 4.0.0
version: 1.0.0
api: [4.0.0, 5.0.0]
version: 1.2.0
authors: ["CortexPE", "supercrafter333"]

0 comments on commit 524d836

Please sign in to comment.