Disforget/Utils.cs

52 lines
1.5 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
namespace Disforget;
public static class Utils
{
static Utils()
{
if (!File.Exists("./config.json"))
{
Console.WriteLine("Cannot find config.json file! Aborting...");
Environment.Exit(-4);
return;
}
Data = JsonSerializer.Deserialize<UserData>(File.ReadAllText("./config.json"))!;
if (Data == null)
{
Console.WriteLine("Invalid config.json file! Aborting...");
Environment.Exit(-5);
return;
}
}
public static readonly UserData Data;
public static void AddHeader(this HttpClient client, string h, string v)
{
if (!client.DefaultRequestHeaders.TryAddWithoutValidation(h, v))
{
Console.Error.WriteLine($"Cannot add {h}: {v}");
Environment.Exit(-3);
}
}
public static HttpClient CreateClient()
{
HttpClient cli = new();
cli.AddHeader("authority", "discord.com");
cli.AddHeader("x-discord-locale", "en-US");
cli.AddHeader("accept-language", "en-US");
cli.AddHeader("authorization", $"{Data.Token}");
cli.AddHeader("user-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) discord/0.0.21 Chrome/91.0.4472.164 Electron/13.6.6 Safari/537.36");
cli.AddHeader("accept", "*/*");
cli.AddHeader("cookie", $"{Data.Cookie}");
return cli;
}
}