diff options
| author | Natasha Moongrave <natasha@256phi.eu> | 2026-04-01 14:23:25 +0200 |
|---|---|---|
| committer | Natasha Moongrave <natasha@256phi.eu> | 2026-04-01 14:23:25 +0200 |
| commit | 276bbac65676a74d73fe7052537a1fb1fe554025 (patch) | |
| tree | 0e5ce19952e7ad3e0fc5d718bc48c7f0f984c77a | |
| parent | 3aa9e12bf1efc1da4f183b455e423037134f94b7 (diff) | |
Updated commissions and guestbook to be able to hook into a discord bot and send me notifications when a commissions is submited
| -rw-r--r-- | src/Pages/commissions/commissions.php | 59 | ||||
| -rw-r--r-- | src/Pages/guestbook/guestbook.php | 58 |
2 files changed, 117 insertions, 0 deletions
diff --git a/src/Pages/commissions/commissions.php b/src/Pages/commissions/commissions.php index 215db6d..579845d 100644 --- a/src/Pages/commissions/commissions.php +++ b/src/Pages/commissions/commissions.php @@ -1,3 +1,58 @@ +<?php +// Load config from outside web root +$config = file_exists('/var/www/config.php') ? require '/var/www/config.php' : []; + +// Send Discord DM notification +function sendDiscordDM($config, $title, $message) { + if (empty($config['discord_bot_token']) || empty($config['discord_user_id'])) { + return false; + } + + $token = $config['discord_bot_token']; + $userId = $config['discord_user_id']; + + // Create/get DM channel + $ch = curl_init('https://discord.com/api/v10/users/@me/channels'); + curl_setopt_array($ch, [ + CURLOPT_HTTPHEADER => [ + 'Authorization: Bot ' . $token, + 'Content-Type: application/json', + ], + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => json_encode(['recipient_id' => $userId]), + CURLOPT_RETURNTRANSFER => true, + ]); + $response = json_decode(curl_exec($ch), true); + curl_close($ch); + + if (empty($response['id'])) { + return false; + } + + // Send message to DM channel + $ch = curl_init('https://discord.com/api/v10/channels/' . $response['id'] . '/messages'); + curl_setopt_array($ch, [ + CURLOPT_HTTPHEADER => [ + 'Authorization: Bot ' . $token, + 'Content-Type: application/json', + ], + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => json_encode([ + 'embeds' => [[ + 'title' => $title, + 'description' => $message, + 'color' => 0xff00ff, + 'timestamp' => date('c'), + ]] + ]), + CURLOPT_RETURNTRANSFER => true, + ]); + curl_exec($ch); + curl_close($ch); + + return true; +} +?> <!DOCTYPE html> <html lang="en"> <head> @@ -386,6 +441,10 @@ if (file_put_contents($submissionsFile, json_encode($submissions, JSON_PRETTY_PRINT))) { $success = true; + + // Send Discord notification + $dmMessage = "**From:** $name\n**Email:** $email\n**Budget:** " . ($budget ?: 'Not specified') . "\n\n**Description:**\n$description"; + sendDiscordDM($config, '🎨 New Commission Request', $dmMessage); } else { $error = 'Failed to save request. Please email me directly.'; } diff --git a/src/Pages/guestbook/guestbook.php b/src/Pages/guestbook/guestbook.php index 4aea11d..208a272 100644 --- a/src/Pages/guestbook/guestbook.php +++ b/src/Pages/guestbook/guestbook.php @@ -4,6 +4,60 @@ ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); +// Load config from outside web root +$config = file_exists('/var/www/config.php') ? require '/var/www/config.php' : []; + +// Send Discord DM notification +function sendDiscordDM($config, $title, $message) { + if (empty($config['discord_bot_token']) || empty($config['discord_user_id'])) { + return false; + } + + $token = $config['discord_bot_token']; + $userId = $config['discord_user_id']; + + // Create/get DM channel + $ch = curl_init('https://discord.com/api/v10/users/@me/channels'); + curl_setopt_array($ch, [ + CURLOPT_HTTPHEADER => [ + 'Authorization: Bot ' . $token, + 'Content-Type: application/json', + ], + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => json_encode(['recipient_id' => $userId]), + CURLOPT_RETURNTRANSFER => true, + ]); + $response = json_decode(curl_exec($ch), true); + curl_close($ch); + + if (empty($response['id'])) { + return false; + } + + // Send message to DM channel + $ch = curl_init('https://discord.com/api/v10/channels/' . $response['id'] . '/messages'); + curl_setopt_array($ch, [ + CURLOPT_HTTPHEADER => [ + 'Authorization: Bot ' . $token, + 'Content-Type: application/json', + ], + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => json_encode([ + 'embeds' => [[ + 'title' => $title, + 'description' => $message, + 'color' => 0x00ffff, + 'timestamp' => date('c'), + ]] + ]), + CURLOPT_RETURNTRANSFER => true, + ]); + curl_exec($ch); + curl_close($ch); + + return true; +} + $dataFile = __DIR__ . '/guestbook.json'; // Make sure the JSON file exists @@ -47,6 +101,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Save back to JSON file if (file_put_contents($dataFile, json_encode($data, JSON_PRETTY_PRINT)) === false) { echo "<p style='color:red'>Error: Could not save guestbook entry. Check file permissions.</p>"; + } else { + // Send Discord notification + $dmMessage = "**From:** {$entry['name']}\n\n{$entry['message']}"; + sendDiscordDM($config, '📝 New Guestbook Entry', $dmMessage); } // Redirect to avoid form resubmission |
