Skip to content

Commit

Permalink
0.2b2
Browse files Browse the repository at this point in the history
  • Loading branch information
n1d3v committed Jun 17, 2024
1 parent 44e91b7 commit cdf550c
Show file tree
Hide file tree
Showing 31 changed files with 2,657 additions and 1,409 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Security.Authentication;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using System.Net;
using WebSocketSharp;
using System.Security.Authentication;

namespace Naticord
{
Expand All @@ -14,13 +15,11 @@ public class WebSocketClientDM
private WebSocket webSocket;
private string accessToken;
private const SslProtocols Tls12 = (SslProtocols)0x00000C00;
bool tryingRandomStuffAtThisPoint = false;
private bool tryingRandomStuffAtThisPoint = false;

public WebSocketClientDM(string accessToken, DM parentDMForm)
{
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
//Console.WriteLine($"Using Access Token: {accessToken}");

this.accessToken = accessToken;
this.parentDMForm = parentDMForm;
InitializeWebSocket();
Expand All @@ -30,7 +29,7 @@ private void InitializeWebSocket()
{
webSocket = new WebSocket($"wss://gateway.discord.gg/?v=9&encoding=json");
webSocket.SslConfiguration.EnabledSslProtocols = Tls12;
webSocket.OnMessage += (sender, e) => HandleWebSocketMessage(e.Data);
webSocket.OnMessage += async (sender, e) => await HandleWebSocketMessage(e.Data);
webSocket.OnError += (sender, e) => HandleWebSocketError(e.Message);
webSocket.OnClose += (sender, e) => HandleWebSocketClose();
webSocket.Connect();
Expand Down Expand Up @@ -71,7 +70,7 @@ private void SendIdentifyPayload()
}
}

private void HandleWebSocketMessage(string data)
private async Task HandleWebSocketMessage(string data)
{
Console.WriteLine($"WebSocket Received: {data}");

Expand All @@ -88,16 +87,16 @@ private void HandleWebSocketMessage(string data)
HandleTypingStartEvent(json["d"]);
break;
case "MESSAGE_CREATE":
HandleMessageCreateEvent(json["d"]);
await HandleMessageCreateEventAsync(json["d"]);
HandleTypingStopEvent(json["d"]);
break;
case "PRESENCE_UPDATE":
HandlePresenceUpdateEvent(json["d"]);
//HandlePresenceUpdateEvent(json["d"]);
break;
}
break;
default:
// handle other op codes when needed ig
// handle other op codes when needed
break;
}
}
Expand All @@ -123,16 +122,13 @@ private void HandleTypingStopEvent(JToken jToken)
string channelId = (string)jToken["channel_id"];
if (long.TryParse(channelId, out long parsedChannelId) && parsedChannelId == parentDMForm.ChatID)
{
string message = "";

parentDMForm.Invoke((MethodInvoker)(() =>
{
parentDMForm.typingStatus.Text = message;
parentDMForm.typingStatus.Text = string.Empty;
}));
}
}


public string GetUsernameById(string userId)
{
try
Expand Down Expand Up @@ -163,17 +159,18 @@ public class Embed
public string Description { get; set; }
}

private void HandleMessageCreateEvent(JToken data)
private async Task HandleMessageCreateEventAsync(JToken data)
{
dynamic eventData = data;
dynamic attachmentData = eventData["attachments"];
dynamic embedData = eventData["embeds"];
string channelId = eventData["channel_id"];
string author = eventData["author"]["global_name"];
if(eventData["author"]["global_name"] == null) author = eventData["author"]["username"];
if (author == null) author = eventData["author"]["username"];
string content = eventData["content"];
List<Attachment> attachmentsFormed = new List<Attachment>();
List<Embed> embedsFormed = new List<Embed>();
var attachmentsFormed = new List<Attachment>();
var embedsFormed = new List<Embed>();

if (attachmentData != null)
{
foreach (var attachment in attachmentData)
Expand Down Expand Up @@ -202,7 +199,8 @@ private void HandleMessageCreateEvent(JToken data)
case 19:
// Reply
bool found = false;
foreach (var message in parentDMForm.GetApiResponse($"channels/{parentDMForm.ChatID.ToString()}/messages"))
var messages = await parentDMForm.GetApiResponse($"channels/{parentDMForm.ChatID}/messages");
foreach (var message in messages)
{
if (message.id == eventData["message_reference"]["message_id"])
{
Expand All @@ -217,41 +215,51 @@ private void HandleMessageCreateEvent(JToken data)
break;

default:
//Normal text or unimplemented
// Normal text or unimplemented
parentDMForm.AddMessage(author, content, "said", attachmentsFormed.ToArray(), embedsFormed.ToArray(), true, true);
break;
}
parentDMForm.Invoke((MethodInvoker)(() => parentDMForm.ScrollToBottom()));
}
}

private void HandlePresenceUpdateEvent(JToken data)
/*private void HandlePresenceUpdateEvent(JToken data)
{
dynamic eventData = data;
string userId = eventData["user"]["id"];
string status = eventData["status"];
}
string username = GetUsernameById(userId);
parentDMForm.Invoke((MethodInvoker)(() =>
{
// nothing for now
}));
}*/

private void HandleWebSocketError(string errorMessage)
{
parentDMForm.Invoke((MethodInvoker)(() =>
{
//Console.WriteLine($"WebSocket Error: {errorMessage}");
// really shitty code on getting the websocket back but works fine, will be patched soon
Console.WriteLine($"WebSocket Error: {errorMessage}");
InitializeWebSocket();
}));
}

private void HandleWebSocketClose()
{
if (!tryingRandomStuffAtThisPoint) try
if (!tryingRandomStuffAtThisPoint)
{
parentDMForm.Invoke((MethodInvoker)(() =>
try
{
//Console.WriteLine("WebSocket connection closed.");
// really shitty code on getting the websocket back but works fine, will be patched soon
InitializeWebSocket();
}));
parentDMForm.Invoke((MethodInvoker)(() =>
{
Console.WriteLine("WebSocket connection closed.");
InitializeWebSocket();
}));
}
catch { }
}
catch { }
}

public void CloseWebSocket()
Expand Down
Loading

0 comments on commit cdf550c

Please sign in to comment.