From 0d53ce18893d65e314dddb1dee012ee50ad61aa8 Mon Sep 17 00:00:00 2001 From: kotyara <62548070+kotyaralih@users.noreply.github.com> Date: Sat, 30 Apr 2022 21:07:32 +0000 Subject: [PATCH] Discord Logger !!EXPERIMENTAL!! --- src/Player.php | 6 +++++- src/PocketMinecraftServer.php | 39 +++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/Player.php b/src/Player.php index 9dbcb62f..d3a510b5 100755 --- a/src/Player.php +++ b/src/Player.php @@ -305,8 +305,9 @@ class Player{ if($msg === true and $this->username != "" and $this->spawned !== false){ $this->server->api->chat->broadcast($this->username." left the game"); } - $this->spawned = false; + $this->spawned = false; console("[INFO] ".FORMAT_AQUA.$this->username.FORMAT_RESET."[/".$this->ip.":".$this->port."] logged out due to ".$reason); + $this->server->send2Discord($this->username . " left the game: ".$reason); $this->windows = array(); $this->armor = array(); $this->inventory = array(); @@ -1348,6 +1349,8 @@ class Player{ return; } + $this->server->send2Discord($this->username . " joined the game"); + $this->auth = true; if(!$this->data->exists("inventory") or ($this->gamemode & 0x01) === 0x01){ if(($this->gamemode & 0x01) === 0x01){ @@ -2016,6 +2019,7 @@ class Player{ }else{ $data = array("player" => $this, "message" => $message); if($this->server->api->handle("player.chat", $data) !== false){ + $this->server->send2Discord("<" . $this->username . "> " . $message); if(isset($data["message"])){ $this->server->api->chat->send($this, $data["message"]); }else{ diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 1a8021a6..2ddff9fc 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -73,11 +73,26 @@ class PocketMinecraftServer{ } console("[INFO] Loading extra.properties..."); $this->extraprops = new Config(DATA_PATH . "extra.properties", CONFIG_PROPERTIES, array( + "version" => "2", "save-player-data" => true, - "save-console-data" => true + "save-console-data" => true, + "discord-msg" => false, + "discord-webhook-url" => "none", + "discord-bot-name" => "NostalgiaCore Logger" )); - - $dolog = $this->extraprops->get("save-console-data"); + + if($this->extraprops->get("discord-msg") == true){ + if($this->extraprops->get("discord-webhook-url") !== "none"){ + console("[INFO] Discord Logger is enabled."); + } else { + console("[WARNING] Discord Logger is enabled in extra.properties,"); + console("[WARNING] but you didn't put the webhook url, so it won't work."); + } + } elseif($this->extraprops->get("version") == null){ + console("[WARNING] Your extra.properties file is corrupted!"); + console("To fix it - just remove it! Server will generate it automatically."); + } + $dolog = $this->extraprops->get("save-console-data"); } function __construct($name, $gamemode = SURVIVAL, $seed = false, $port = 19132, $serverip = "0.0.0.0"){ @@ -695,5 +710,21 @@ class PocketMinecraftServer{ } } } - + + public function send2Discord($msg){ + if($this->extraprops->get("discord-msg") == true and $this->extraprops->get("discord-webhook-url") !== "none"){ + $url = $this->extraprops->get("discord-webhook-url"); + $name = $this->extraprops->get("discord-bot-name"); + $headers = [ 'Content-Type: application/json; charset=utf-8' ]; + $POST = [ 'username' => $name, 'content' => $msg]; + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($POST)); + $response = curl_exec($ch); + } + } }