Skip to content

Commit

Permalink
more qol
Browse files Browse the repository at this point in the history
  • Loading branch information
n1d3v committed Jun 22, 2024
1 parent b3a3292 commit ed2023a
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 27 deletions.
80 changes: 71 additions & 9 deletions Naticord/Forms/DM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Newtonsoft.Json;
using System.Drawing;
using System.Net;
using System.IO;

namespace Naticord
{
Expand Down Expand Up @@ -264,7 +265,7 @@ private void ToggleHtmlTag(StringBuilder html, Stack<string> waitingToClose, str
}
}

private void SendMessage()
private async Task SendMessage()
{
string message = messageBox.Text.Trim();
if (!string.IsNullOrEmpty(message))
Expand All @@ -275,27 +276,88 @@ private void SendMessage()
{
content = message
};
string jsonPostData = JsonConvert.SerializeObject(postData);
string jsonPostData = Newtonsoft.Json.JsonConvert.SerializeObject(postData);

using (var client = new WebClient())
using (var client = new HttpClient())
{
byte[] byteArray = Encoding.UTF8.GetBytes(jsonPostData);
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.Headers[HttpRequestHeader.Authorization] = AccessToken;
byte[] responseArray = client.UploadData($"{DiscordApiBaseUrl}channels/{ChatID}/messages", "POST", byteArray);
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(AccessToken);
HttpContent content = new StringContent(jsonPostData, Encoding.UTF8, "application/json");

string response = Encoding.UTF8.GetString(responseArray);
HttpResponseMessage response = await client.PostAsync($"{DiscordApiBaseUrl}channels/{ChatID}/messages", content);
response.EnsureSuccessStatusCode();

if (Clipboard.ContainsImage())
{
Image image = Clipboard.GetImage();
byte[] imageBytes = ImageToBytes(image);
await UploadImage(imageBytes);
}
}

messageBox.Clear();
}
catch (WebException ex)
catch (Exception ex)
{
ShowErrorMessage("Failed to send message", ex);
}
}
}

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.V))
{
if (Clipboard.ContainsImage())
{
try
{
Image clipboardImage = Clipboard.GetImage();
byte[] imageBytes = ImageToBytes(clipboardImage);

Task.Run(() => UploadImage(imageBytes)).ConfigureAwait(false);
}
catch (Exception ex)
{
ShowErrorMessage("Failed to upload image", ex);
}
}
}

return base.ProcessCmdKey(ref msg, keyData);
}

private async Task UploadImage(byte[] imageBytes)
{
try
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(AccessToken);

MultipartFormDataContent formData = new MultipartFormDataContent();
formData.Add(new ByteArrayContent(imageBytes), "file", "image.png");

HttpResponseMessage response = await client.PostAsync($"{DiscordApiBaseUrl}channels/{ChatID}/messages", formData);
response.EnsureSuccessStatusCode();

string responseContent = await response.Content.ReadAsStringAsync();
}
}
catch (Exception ex)
{
ShowErrorMessage("Failed to upload image", ex);
}
}

private byte[] ImageToBytes(Image image)
{
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms.ToArray();
}
}

public async Task<dynamic> GetApiResponse(string endpoint)
{
using (var client = new HttpClient { BaseAddress = new Uri(DiscordApiBaseUrl) })
Expand Down
80 changes: 71 additions & 9 deletions Naticord/Forms/Group.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Newtonsoft.Json;
using System.Drawing;
using System.Net;
using System.IO;

namespace Naticord
{
Expand Down Expand Up @@ -245,7 +246,7 @@ private void ToggleHtmlTag(StringBuilder html, Stack<string> waitingToClose, str
}
}

private void SendMessage()
private async Task SendMessage()
{
string message = messageBox.Text.Trim();
if (!string.IsNullOrEmpty(message))
Expand All @@ -256,27 +257,88 @@ private void SendMessage()
{
content = message
};
string jsonPostData = JsonConvert.SerializeObject(postData);
string jsonPostData = Newtonsoft.Json.JsonConvert.SerializeObject(postData);

using (var client = new WebClient())
using (var client = new HttpClient())
{
byte[] byteArray = Encoding.UTF8.GetBytes(jsonPostData);
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.Headers[HttpRequestHeader.Authorization] = AccessToken;
byte[] responseArray = client.UploadData($"{DiscordApiBaseUrl}channels/{ChatID}/messages", "POST", byteArray);
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(AccessToken);
HttpContent content = new StringContent(jsonPostData, Encoding.UTF8, "application/json");

string response = Encoding.UTF8.GetString(responseArray);
HttpResponseMessage response = await client.PostAsync($"{DiscordApiBaseUrl}channels/{ChatID}/messages", content);
response.EnsureSuccessStatusCode();

if (Clipboard.ContainsImage())
{
Image image = Clipboard.GetImage();
byte[] imageBytes = ImageToBytes(image);
await UploadImage(imageBytes);
}
}

messageBox.Clear();
}
catch (WebException ex)
catch (Exception ex)
{
ShowErrorMessage("Failed to send message", ex);
}
}
}

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.V))
{
if (Clipboard.ContainsImage())
{
try
{
Image clipboardImage = Clipboard.GetImage();
byte[] imageBytes = ImageToBytes(clipboardImage);

Task.Run(() => UploadImage(imageBytes)).ConfigureAwait(false);
}
catch (Exception ex)
{
ShowErrorMessage("Failed to upload image", ex);
}
}
}

return base.ProcessCmdKey(ref msg, keyData);
}

private async Task UploadImage(byte[] imageBytes)
{
try
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(AccessToken);

MultipartFormDataContent formData = new MultipartFormDataContent();
formData.Add(new ByteArrayContent(imageBytes), "file", "image.png");

HttpResponseMessage response = await client.PostAsync($"{DiscordApiBaseUrl}channels/{ChatID}/messages", formData);
response.EnsureSuccessStatusCode();

string responseContent = await response.Content.ReadAsStringAsync();
}
}
catch (Exception ex)
{
ShowErrorMessage("Failed to upload image", ex);
}
}

private byte[] ImageToBytes(Image image)
{
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms.ToArray();
}
}

public async Task<dynamic> GetApiResponse(string endpoint)
{
using (var client = new HttpClient { BaseAddress = new Uri(DiscordApiBaseUrl) })
Expand Down
81 changes: 72 additions & 9 deletions Naticord/Forms/Server.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
Expand Down Expand Up @@ -414,7 +416,7 @@ private void messageBox_KeyDown(object sender, KeyEventArgs e)
}
}

private void SendMessage()
private async Task SendMessage()
{
string message = messageBox.Text.Trim();
if (!string.IsNullOrEmpty(message))
Expand All @@ -425,27 +427,88 @@ private void SendMessage()
{
content = message
};
string jsonPostData = JsonConvert.SerializeObject(postData);
string jsonPostData = Newtonsoft.Json.JsonConvert.SerializeObject(postData);

using (var client = new WebClient())
using (var client = new HttpClient())
{
byte[] byteArray = Encoding.UTF8.GetBytes(jsonPostData);
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.Headers[HttpRequestHeader.Authorization] = AccessToken;
byte[] responseArray = client.UploadData($"{DiscordApiBaseUrl}channels/{ChatID}/messages", "POST", byteArray);
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(AccessToken);
HttpContent content = new StringContent(jsonPostData, Encoding.UTF8, "application/json");

string response = Encoding.UTF8.GetString(responseArray);
HttpResponseMessage response = await client.PostAsync($"{DiscordApiBaseUrl}channels/{ChatID}/messages", content);
response.EnsureSuccessStatusCode();

if (Clipboard.ContainsImage())
{
Image image = Clipboard.GetImage();
byte[] imageBytes = ImageToBytes(image);
await UploadImage(imageBytes);
}
}

messageBox.Clear();
}
catch (WebException ex)
catch (Exception ex)
{
ShowErrorMessage("Failed to send message", ex);
}
}
}

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.V))
{
if (Clipboard.ContainsImage())
{
try
{
Image clipboardImage = Clipboard.GetImage();
byte[] imageBytes = ImageToBytes(clipboardImage);

Task.Run(() => UploadImage(imageBytes)).ConfigureAwait(false);
}
catch (Exception ex)
{
ShowErrorMessage("Failed to upload image", ex);
}
}
}

return base.ProcessCmdKey(ref msg, keyData);
}

private async Task UploadImage(byte[] imageBytes)
{
try
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(AccessToken);

MultipartFormDataContent formData = new MultipartFormDataContent();
formData.Add(new ByteArrayContent(imageBytes), "file", "image.png");

HttpResponseMessage response = await client.PostAsync($"{DiscordApiBaseUrl}channels/{ChatID}/messages", formData);
response.EnsureSuccessStatusCode();

string responseContent = await response.Content.ReadAsStringAsync();
}
}
catch (Exception ex)
{
ShowErrorMessage("Failed to upload image", ex);
}
}

private byte[] ImageToBytes(Image image)
{
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms.ToArray();
}
}

private void ShowErrorMessage(string message, Exception ex)
{
MessageBox.Show($"{message}: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Expand Down

0 comments on commit ed2023a

Please sign in to comment.