Przeglądaj źródła

Discord Webhook

Owen Diffey 4 lat temu
rodzic
commit
46a777ad46
2 zmienionych plików z 59 dodań i 0 usunięć
  1. 2 0
      config.php.example
  2. 57 0
      index.php

+ 2 - 0
config.php.example

@@ -8,4 +8,6 @@ define(mysqlDatabase, "");
 define(allowedUsers, array());
 define(discordClientId, );
 define(discordClientSecret, "");
+define(discordWebhook, "");
+
 ?>

+ 57 - 0
index.php

@@ -274,6 +274,25 @@ function getDiscordUserGuilds($access_token)
     }
 }
 
+function sendDcMessage($webook, $message)
+{
+    $ch = curl_init();
+    curl_setopt_array($ch, [
+        CURLOPT_URL => $webhook,
+        CURLOPT_POST => true,
+        CURLOPT_POSTFIELDS => $message,
+        CURLOPT_HTTPHEADER => [
+            "Content-Type: application/json"
+        ]
+    ]);
+    $response = curl_exec($ch);
+    curl_close($ch);
+    if ($response == 1) {
+        return $response;
+    }
+    return "Error: Issue sending message";
+}
+
 if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (isset($_POST["delete"])) {
     print_r($_POST["delete"]);
@@ -325,6 +344,44 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
     $birthDate = $conn->escapeString($birthDate);
     $submitTime = $conn->escapeString(date("Y-m-d\TH:i:s"));
     if ($conn->query("INSERT INTO empyreanrealm (nickname, discord, username, birthdate, submittime) VALUES (?,?,?,?,?)", $nickname, $discordUsername, $username, $birthDate, $submitTime)->affectedRows() === 1) {
+      $message = json_encode([
+          "username" => "Bedrock Applications",
+          "avatar_url" => siteUrl."/source/logo.png",
+          "embeds" => [
+          [
+              "title" => "New Bedrock Application",
+              "type" => "rich",
+              "description" => "$nickname has applied to the bedrock server.",
+              "timestamp" => date("Y-m-d\TH:i:s"),
+              "url" => siteUrl."/applications",
+              "color" => hexdec("55C9F6"),
+              "fields" => [
+                  [
+                  "name" => "Nickname",
+                  "value" => "$nickname",
+                  "inline" => true
+                  ],
+                  [
+                    "name" => "Bedrock Username",
+                    "value" => "$username",
+                    "inline" => true
+                  ],
+                  [
+                    "name" => "Discord Username",
+                    "value" => "$discordUsername",
+                    "inline" => true
+                  ],
+                  [
+                    "name" => "Birth Date",
+                    "value" => "$birthDate",
+                    "inline" => true
+                  ]
+              ]
+          ]
+      ]
+      
+      ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
+      sendDcMessage(discordWebhook, $message);
       $success = true;
     } else {
       $errors[] = "Error submitting form.";