C
C#3mo ago
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}");
}
}
}
}
}
23 Replies
Angius
Angius3mo ago
Define "doesn't work"
pogdoggy_
pogdoggy_3mo ago
im getting 400 bad request and the detailed errors in console arent working
pogdoggy_
pogdoggy_3mo ago
No description
pogdoggy_
pogdoggy_3mo ago
also getting this
No description
Angius
Angius3mo ago
Use the debugger to see what actual data you're sending
pogdoggy_
pogdoggy_3mo ago
how do i do that?
Angius
Angius3mo ago
Then try sending it with, say, Bruno or something $debug
MODiX
MODiX3mo ago
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
pogdoggy_
pogdoggy_3mo ago
alr thanks
Angius
Angius3mo ago
Might be a little more involved for Unity, not 100% sure
pogdoggy_
pogdoggy_3mo ago
No description
Angius
Angius3mo ago
Yeah, you're gonna have to look up how to use the debugger with Unity
pogdoggy_
pogdoggy_3mo ago
alrighty! since this is a mapping thing i cant really put unity into play mode to debug it
Angius
Angius3mo ago
Skip that step, then, and try sending the same message with Bruno or whatever other client Or even with a simple C# console app See if that works
pogdoggy_
pogdoggy_3mo ago
ive sent it with an external tool] the webhook works fine ill try with bruno though'
Angius
Angius3mo ago
Nah, what you use doesn't really matter
pogdoggy_
pogdoggy_3mo ago
ah okay if the webhook works fine could it be my json payload
Angius
Angius3mo ago
Could be, yeah
pogdoggy_
pogdoggy_3mo ago
im sure discord uses application/json
Angius
Angius3mo ago
That much is a given, yeah
pogdoggy_
pogdoggy_3mo ago
looking at my code can you see anything that could cause it?
Angius
Angius3mo ago
new StringContent seems sus, could be creating a text payload There's a JsonContent class
pogdoggy_
pogdoggy_3mo ago
hm alr ill change it i think ima take a break for now and come back to it