pogdoggy_
help with c#
im trying to write this script that sends a message to a webhook onclick, but it isnt working help would be appreciated
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Text;
using System.Net.Http;
using System;
using System.Threading.Tasks;
namespace UltrakillProject3
{
public class DiscordWebhook : MonoBehaviour
{
public Button button; //buttoncomp ref
public string webhookUrl = ""; // url
void Start()
{
// buton?
if (button == null)
{
Debug.LogError("Button not assigned!");
return;
}
// listen onclikc
button.onClick.AddListener(SendMessageToDiscord);
}
async void SendMessageToDiscord()
{
await PostToDiscord();
}
async Task PostToDiscord()
{
// make sure webhk is there
if (string.IsNullOrEmpty(webhookUrl))
{
Debug.LogError("Webhook URL is not set!");
return;
}
// json payload
var jsonContent = new Dictionary<string, string>
{
{ "content", "Button pressed!" }
};
var jsonPayload = JsonUtility.ToJson(jsonContent);
Debug.Log($"Sending JSON payload to Discord: {jsonPayload}");
var payload = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
// http post request
using (var client = new HttpClient())
{
try
{
var response = await client.PostAsync(webhookUrl, payload);
var responseContent = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
Debug.Log("Message sent to Discord!");
}
else
{
Debug.LogError($"Failed to send message to Discord. Status Code: {response.StatusCode}, Reason: {response.ReasonPhrase}, Content: {responseContent}");
}
}
catch (Exception e)
{
Debug.LogError($"Error sending message to Discord: {e.Message}");
}
}
}
}
}
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Text;
using System.Net.Http;
using System;
using System.Threading.Tasks;
namespace UltrakillProject3
{
public class DiscordWebhook : MonoBehaviour
{
public Button button; //buttoncomp ref
public string webhookUrl = ""; // url
void Start()
{
// buton?
if (button == null)
{
Debug.LogError("Button not assigned!");
return;
}
// listen onclikc
button.onClick.AddListener(SendMessageToDiscord);
}
async void SendMessageToDiscord()
{
await PostToDiscord();
}
async Task PostToDiscord()
{
// make sure webhk is there
if (string.IsNullOrEmpty(webhookUrl))
{
Debug.LogError("Webhook URL is not set!");
return;
}
// json payload
var jsonContent = new Dictionary<string, string>
{
{ "content", "Button pressed!" }
};
var jsonPayload = JsonUtility.ToJson(jsonContent);
Debug.Log($"Sending JSON payload to Discord: {jsonPayload}");
var payload = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
// http post request
using (var client = new HttpClient())
{
try
{
var response = await client.PostAsync(webhookUrl, payload);
var responseContent = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
Debug.Log("Message sent to Discord!");
}
else
{
Debug.LogError($"Failed to send message to Discord. Status Code: {response.StatusCode}, Reason: {response.ReasonPhrase}, Content: {responseContent}");
}
}
catch (Exception e)
{
Debug.LogError($"Error sending message to Discord: {e.Message}");
}
}
}
}
}
33 replies