Files
Discord-Client/Connections/MsgRequests.cs
T

186 lines
6.0 KiB
C#
Raw Normal View History

2023-03-11 08:45:22 -05:00
using System.Diagnostics;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
namespace Discord_Client_Custom.Connections
{
internal class MsgRequests
{
2023-03-11 09:53:18 -05:00
private static int msgFetchLimit = 50;
2023-03-11 08:45:22 -05:00
private static string dmMePath = "https://discord.com/api/users/@me/channels";
2023-03-11 09:53:18 -05:00
private static string dmGetMsgsBasepath = "https://discord.com/api/channels/{{id}}/messages?limit=" + msgFetchLimit.ToString();
2023-03-11 08:45:22 -05:00
public static string userToken = System.Environment.GetEnvironmentVariable("userToken");
public MsgRequests(string url, string reqType)
{
//userToken = System.Environment.GetEnvironmentVariable("userToken");
}
public async static Task<JsonNode> sendMessage(string content, string ep)
{
try
{
var client = new HttpClient();
var toSend = new Dictionary<string, string>
{
{ "content", content },
};
var request = new HttpRequestMessage()
{
RequestUri = new Uri(ep),
Method = HttpMethod.Post,
Content = new FormUrlEncodedContent(toSend)
};
request.Headers.Clear();
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//CHANGE THIS TO CONFIG LATER
request.Headers.Add("Authorization", userToken);
request.Headers.Add("User-Agent", ".NET Foundation Repository Reporter");
var taskResponse = await client.SendAsync(request);
var responseContent = await taskResponse.Content.ReadAsStringAsync();
JsonNode responseJSON = JsonNode.Parse(responseContent);
if (taskResponse.StatusCode != System.Net.HttpStatusCode.OK)
{
Debug.Write(responseJSON["message"]);
return null;
}
return responseJSON;
} catch (Exception e)
{
Debug.WriteLine(e);
return null;
}
}
2023-03-11 15:54:43 -05:00
public static async Task<JsonNode> sendTyping(string url)
{
try
{
var client = new HttpClient();
var request = new HttpRequestMessage()
{
RequestUri = new Uri(url),
Method = HttpMethod.Post,
};
request.Headers.Clear();
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//CHANGE THIS TO CONFIG LATER
request.Headers.Add("Authorization", userToken);
request.Headers.Add("User-Agent", ".NET Foundation Repository Reporter");
var taskResponse = await client.SendAsync(request);
var responseContent = await taskResponse.Content.ReadAsStringAsync();
//Typing
if (responseContent.Length == 0) return null;
JsonNode responseJSON = JsonNode.Parse(responseContent);
if (taskResponse.StatusCode != System.Net.HttpStatusCode.OK)
{
Debug.Write(responseJSON["message"]);
return null;
}
return responseJSON;
}
catch (Exception e)
{
Debug.WriteLine(e);
return null;
}
}
2023-03-11 08:45:22 -05:00
public static async Task<JsonNode> getChannels()
{
var client = new HttpClient();
var request = new HttpRequestMessage()
{
RequestUri = new Uri(dmMePath),
Method = HttpMethod.Get,
};
request.Headers.Clear();
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//CHANGE THIS TO CONFIG LATER
request.Headers.Add("Authorization", userToken);
request.Headers.Add("User-Agent", ".NET Foundation Repository Reporter");
var taskResponse = await client.SendAsync(request);
var responseContent = await taskResponse.Content.ReadAsStringAsync();
if (responseContent == null) { return null; }
JsonNode responseJSON = JsonNode.Parse(responseContent);
if (taskResponse.StatusCode != System.Net.HttpStatusCode.OK)
{
Debug.Write(responseJSON["message"]);
return null;
}
return responseJSON;
}
//Returns the
public static async Task<JsonNode> getMessages(string cid, string? lastId = null)
{
string newUrl = dmGetMsgsBasepath.Replace("{{id}}", cid);
if (lastId != null)
{
newUrl += "&before=" + lastId;
}
var client = new HttpClient();
var request = new HttpRequestMessage()
{
RequestUri = new Uri(newUrl),
Method = HttpMethod.Get,
};
request.Headers.Clear();
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Add("Authorization", userToken);
request.Headers.Add("User-Agent", ".NET Foundation Repository Reporter");
var taskResponse = await client.SendAsync(request);
var responseContent = await taskResponse.Content.ReadAsStringAsync();
if (responseContent == null) { return null; }
JsonNode responseJSON = JsonNode.Parse(responseContent);
if (taskResponse.StatusCode != System.Net.HttpStatusCode.OK)
{
Debug.Write(responseJSON["message"]);
return null;
}
return responseJSON;
}
}
}