Webhook (Embed)
Voici le code pour envoyer un webhook sous forme d'embed :
private static readonly HttpClient httpClient = new HttpClient();
private async Task SendWebhook(string url, string title, params (string Name, string Value, bool Inline)[] fields)
{
try
{
var embed = new
{
title,
color = 0xff0000,
timestamp = DateTime.UtcNow.ToString("o"),
fields = fields.Select(f => new { name = f.Name, value = f.Value, inline = f.Inline }).ToArray()
};
var payload = new { embeds = new[] { embed } };
string json = JsonConvert.SerializeObject(payload, Formatting.Indented);
using (var request = new HttpRequestMessage(HttpMethod.Post, url))
{
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
request.Headers.Add("User-Agent", "UnityWebhookClient");
HttpResponseMessage response = await httpClient.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
Debug.LogError($"[Webhook Error] Code: {response.StatusCode}, Message: {await response.Content.ReadAsStringAsync()}");
}
}
}
catch (Exception ex)
{
Debug.LogError($"[Webhook Error] Exception : {ex.Message}");
}
}
// Exemple d'utilisation :
public async override void OnPlayerMoney(Player player, double amount, string reason)
{
base.OnPlayerMoney(player, amount, reason);
string title = "💰 Nouvelle Transaction";
var fields = new[]
{
("Heure", DateTime.Now.ToString("HH:mm:ss"), true),
("Joueur", $"{player.FullName}", true),
("ID", $"{player.character.Id}", true),
("Steam ID", $"{player.account.steamId}", true),
("Montant", $"{amount}€", true),
("Raison", reason, false) // "False" désigne si la valeur doit être alignée aux autres
};
await SendWebhook("webhook_url", title, fields);
}
Dernière mise à jour