diff --git a/Assets/Editor/SceneNotifier.cs b/Assets/Editor/SceneNotifier.cs index 1b7682b1..7aa08e65 100644 --- a/Assets/Editor/SceneNotifier.cs +++ b/Assets/Editor/SceneNotifier.cs @@ -1,46 +1,46 @@ +using System.Text; using UnityEditor; using UnityEditor.SceneManagement; using UnityEngine; -using System.Net.Http; -using System.Text; -using System.Threading.Tasks; +using UnityEngine.Networking; [InitializeOnLoad] -public class SceneNotifier +public static class DiscordSceneNotifier { - private static readonly string WebhookUrl = "http://n8n.homelab/webhook/unity-scene"; - private static readonly string UserName = System.Environment.UserName; + private const string WebhookUrl = "https://discord.com/api/webhooks/1486718530765328424/42mP_zYNTEwwBbc_lcuasPbpZppXoC4U14-Pde1YatdxatT5s9vml8lgQXTwAmm0PNe5"; - private static readonly HttpClient Http = new HttpClient(); - - static SceneNotifier() + static DiscordSceneNotifier() { EditorSceneManager.sceneOpened += OnSceneOpened; - EditorSceneManager.sceneClosed += OnSceneClosed; } - private static void OnSceneOpened(UnityEngine.SceneManagement.Scene scene, OpenSceneMode mode) + private static void OnSceneOpened(UnityEngine.SceneManagement.Scene scene, UnityEditor.SceneManagement.OpenSceneMode mode) { - _ = SendNotification(scene.name, "opened"); + string userName = System.Environment.UserName; + string message = $"{userName} opened scene: {scene.name}"; + SendDiscordMessage(message); } - private static void OnSceneClosed(UnityEngine.SceneManagement.Scene scene) + private static async void SendDiscordMessage(string content) { - _ = SendNotification(scene.name, "closed"); + string json = JsonUtility.ToJson(new DiscordMessage { content = content }); + + using var request = new UnityWebRequest(WebhookUrl, "POST"); + byte[] bodyRaw = Encoding.UTF8.GetBytes(json); + + request.uploadHandler = new UploadHandlerRaw(bodyRaw); + request.downloadHandler = new DownloadHandlerBuffer(); + request.SetRequestHeader("Content-Type", "application/json"); + + await request.SendWebRequest(); + + if (request.result != UnityWebRequest.Result.Success) + Debug.LogWarning("Discord webhook failed: " + request.error); } - private static async Task SendNotification(string sceneName, string action) + [System.Serializable] + private class DiscordMessage { - string json = $"{{\"scene\":\"{sceneName}\",\"user\":\"{UserName}\",\"action\":\"{action}\"}}"; - try - { - var content = new StringContent(json, Encoding.UTF8, "application/json"); - var response = await Http.PostAsync(WebhookUrl, content); - Debug.Log($"[SceneNotifier] Sent: {json} — Status: {response.StatusCode}"); - } - catch (System.Exception e) - { - Debug.LogWarning($"[SceneNotifier] Failed to notify: {e.Message}"); - } + public string content; } -} +} \ No newline at end of file