Initial code commit

This commit is contained in:
ION606
2023-03-11 08:45:22 -05:00
parent 157789299b
commit 848cd840b6
20 changed files with 1824 additions and 0 deletions
+202
View File
@@ -0,0 +1,202 @@
using System;
using System.Diagnostics;
using System.Security.Policy;
using System.Text.Json.Nodes;
namespace Discord_Client_Custom.Channels
{
internal class ChannelMsgGroup
{
internal class message
{
internal class Author
{
private string id;
private string username;
private string discriminator;
private string avatar;
public string getId() { return id; }
public string getTag() { return username + discriminator; }
public string getAvatar() { return avatar; }
public Author(JsonNode inp)
{
id = inp["id"].ToString();
username = inp["username"].ToString();
discriminator = inp["discriminator"].ToString();
//Check for deleted user
if ((string)inp["avatar"] != "")
{
avatar = inp["avatar"].ToString();
}
}
public string toString()
{
return "[\n\tid: " + id + "\n\ttag: " + username + "#" + discriminator + "\n\tavatar: " + getAvatar() + "\n]";
}
}
private string id;
private string content;
private Author msgAuthor;
private string url;
private DateTime timestamp;
public message(JsonNode inp)
{
id = (string)inp["id"];
url = "https://discord.com/channels/@me/" + inp["channel_id"] + "/" + inp["id"];
content = (string)inp["content"];
msgAuthor = new Author(inp["author"]);
timestamp = DateTime.Parse((string)inp["timestamp"]);
if (id == null || content == null || msgAuthor == null)
{
Debug.WriteLine("id, content, or author is null");
}
}
public string getId() { return id; }
public string getUrl() { return url; }
public DateTime getTimestamp() { return timestamp; }
public string toString()
{
return "id: " + id + "\ncontent: " + content + "\nauthor: " + msgAuthor.toString();
}
public string getContent() { return content; }
}
private ContextMenuStrip createContextMenu(bool isIcon, string iconUrl)
{
//see profile, see icon
//delete message, copy link, edit message
ContextMenuStrip menu = new ContextMenuStrip();
menu.Text = "right-click";
if (isIcon)
{
var item1 = new ToolStripMenuItem("see profile");
var item2 = new ToolStripMenuItem("see icon");
item2.Click += new EventHandler((sender, args) =>
{
//Horrid resolution, try just opening the web page
var icon = (PictureBox)((ContextMenuStrip)((ToolStripMenuItem)sender).Owner).SourceControl;
Process.Start(new ProcessStartInfo
{
FileName = iconUrl.Remove(iconUrl.IndexOf("?size=")),
UseShellExecute = true
});
});
menu.Items.Add(item1);
menu.Items.Add(item2);
}
else
{
var item1 = new ToolStripMenuItem("delete message");
var item2 = new ToolStripMenuItem("edit message");
var item3 = new ToolStripMenuItem("copy link");
item3.Click += new EventHandler(async (sender, args) =>
{
Clipboard.SetText(iconUrl);
var parent = (Label)((ContextMenuStrip)((ToolStripMenuItem)sender).Owner).SourceControl;
var oldCol = parent.ForeColor;
parent.ForeColor = Color.Blue;
ManualResetEvent resetEvent = new ManualResetEvent(false);
await Task.Delay(2000);
parent.ForeColor = oldCol;
});
menu.Items.Add(item1);
menu.Items.Add(item2);
menu.Items.Add(item3);
}
return menu;
}
private message[] msgs;
private Label[] msglabels;
public ChannelMsgGroup(JsonNode inpMsgs, TableLayoutPanel dmFlowContent, Image uIcon, int rowNumber)
{
//msgs = new message[inpMsgs.Length];
//msglabels = new Label[inpMsgs.Length];
//dmFlowContent.SuspendLayout();
//Add the user icon
var uBtn = new PictureBox
{
Image = uIcon,
Tag = uIcon.Tag,
};
uBtn.ContextMenuStrip = createContextMenu(true, (string)uIcon.Tag);
uBtn.Size = uIcon.Size;
uBtn.Padding = new Padding(0, 0, 0, 50);
dmFlowContent.Controls.Add(uBtn, 0, rowNumber);
var msg = new message(inpMsgs);
Label txt = new Label();
txt.Text = msg.getContent();
txt.Tag = msg.getId();
txt.MaximumSize = new Size(dmFlowContent.Width - 70, 100000000);
txt.AutoSize = true;
txt.Visible = true;
txt.ContextMenuStrip = createContextMenu(false, msg.getUrl());
dmFlowContent.Controls.Add(txt, 1, rowNumber);
/* FOR MULTIPLE MESSAGES
for (int i = 0; i < msgs.Length; i++)
{
//if ((string)inpMsgs[i]["content"] == "") continue;
msgs[i] = new message(inpMsgs[i]);
Label txt = new Label();
txt.Text = msgs[i].getContent();
txt.Tag = msgs[i].getId();
txt.MaximumSize = new Size(dmFlowContent.Width - 70, 100000000);
txt.AutoSize = true;
txt.Visible = true;
txt.ContextMenuStrip = createContextMenu(false, msgs[i].getUrl());
dmFlowContent.Controls.Add(txt, 1, rowNumber + i - 1);
//dmFlowContent.RowStyles.Add(new RowStyle(SizeType.Absolute));
//dmFlowContent.RowStyles[rowNumber + i].Height = txt.Height;
//dmFlowContent.SetFlowBreak(txt, true);
}*/
//dmFlowContent.ResumeLayout();
}
public Label[] getLabels()
{
return msglabels;
}
}
}
+254
View File
@@ -0,0 +1,254 @@
using Discord_Client_Custom.client_internals;
using Discord_Client_Custom.Connections;
using System.Collections;
using System.Diagnostics;
using System.Net;
using System.Runtime.Serialization;
using System.Text.Json.Nodes;
using static Discord_Client_Custom.client_internals.Client;
namespace Discord_Client_Custom.Channels
{
public class ChannelObj
{
public static async Task<Image> getIconStream(string imageUrl)
{
Image image = null;
try
{
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, imageUrl);
var response = await client.SendAsync(request);
Stream stream = await response.Content.ReadAsStreamAsync();
image = Image.FromStream(stream);
}
catch (Exception ex)
{
Debug.WriteLine("=======================================================");
Debug.Write(ex.StackTrace);
Debug.WriteLine(ex.Message);
Debug.WriteLine("Using URL: " + imageUrl);
Debug.WriteLine("=======================================================");
return null;
}
return image;
}
private static List<user> users = new List<user>();
private static List<ChannelMsgGroup> groupedMsgs = new List<ChannelMsgGroup>();
private static string cid;
private static int ctype = 1;
private static string? cname;
private static string? cicon;
private static string? cownerId;
private string lastSent;
public ChannelObj() { }
//For displaying information
public ChannelObj(JsonNode contents)
{
cid = contents["id"].ToString();
ctype = (int)contents["type"];
if (ctype == 3)
{
cname = contents["name"].ToString();
cicon = contents["icon"].ToString();
cownerId = contents["owner_id"].ToString();
}
//Add the users to the DM
Array recipients = contents["recipients"].AsArray().ToArray();
for (int i = 0; i < recipients.Length; i++)
{
JsonNode uObj = (JsonNode)recipients.GetValue(i);
users.Add(new user(uObj));
//users.Append(new user(uObj));
}
//Console.WriteLine(recipients.GetValue(0).ToString() + " NUM: " + recipients.Length.ToString());
}
//Creating Messages
internal ChannelObj(JsonNode contents, string cid2, TableLayoutPanel dmFlowContent, Image uicon, userMain uMain)
{
dmFlowContent.Controls.Clear();
//Begin message section
Image uMainIcon = uMain.getAvatar();
string uMainId = uMain.getId();
//There's gonna be a faster way
var arr = contents.AsArray().ToArray().Reverse().ToArray();
string id_current = (string)arr.Last()["id"];
int startInd = 0;
//Make sure the first column is consitant
dmFlowContent.ColumnStyles.Clear();
dmFlowContent.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, uicon.Width));
dmFlowContent.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
//dmFlowContent.ColumnStyles[1].Width = 500;
int i;
for (i = 0; i < arr.Length; i++)
{
if ((string)arr[i]["content"] == "") continue;
string authorName = (string)arr[i]["author"]["id"];
//Removed for spacing reasons
//if (authorName != id_current)
//{
Debug.WriteLine(arr[i]["content"]);
if (authorName == uMainId)
{
groupedMsgs.Add(new ChannelMsgGroup(arr[i], dmFlowContent, uMainIcon, i));
}
else
{
groupedMsgs.Add(new ChannelMsgGroup(arr[i], dmFlowContent, uicon, i));
}
startInd = i;
id_current = authorName;
//}
}
/*
if (i != startInd)
{
if ((string)arr[startInd]["author"]["id"] == uMainId)
{
groupedMsgs[groupedMsgs.Count - 1] = new ChannelMsgGroup(arr[startInd..i], dmFlowContent, uMainIcon, i);
}
else
{
groupedMsgs[groupedMsgs.Count - 1] = new ChannelMsgGroup(arr[startInd..i], dmFlowContent, uicon, i);
}
}*/
lastSent = id_current;
//Add the text box
var txtbx = new RichTextBox();
txtbx.AutoWordSelection = true;
txtbx.Size = new Size(dmFlowContent.Width - 50, 50);
txtbx.KeyDown += async (object o, KeyEventArgs k) =>
{
if (k.KeyCode == Keys.Enter)
{
string ep;
if (ctype == 1)
{
ep = "https://discord.com/api/channels/" + cid2 + "/messages";
} else
{
//Deal with group message stuff here.....
Debug.WriteLine("Message Sending has not been implemented for group DMs (yet)");
return;
}
var response = await MsgRequests.sendMessage(txtbx.Text, ep);
JsonNode[] arr = new JsonNode[1];
//arr[0] = response;
//Add the message to chat in the app
groupedMsgs.Add(new ChannelMsgGroup(response, dmFlowContent, uMainIcon, i + 1));
txtbx.Clear();
i++;
dmFlowContent.Controls.Remove(txtbx);
dmFlowContent.Controls.Add(txtbx, 1, i + 1);
dmFlowContent.SetColumnSpan(txtbx, 2);
} else
{
//Do typing intent stuff here
//Connection
}
};
dmFlowContent.Controls.Add(txtbx, 0, i+1);
dmFlowContent.SetColumnSpan(txtbx, 2);
}
public string getName()
{
if (cname != null)
{
return cname;
}
return users[0].getUserName();
}
public string getId()
{
return cid;
}
public async Task<Image> getIcon()
{
string iconUrl;
if (ctype == 1)
{
iconUrl = "https://cdn.discordapp.com/avatars/" + users[0].getId() + "/";
string uAvatar = users[0].getAvatar();
//null user
if (uAvatar != null)
{
iconUrl += uAvatar;
}
else
{
iconUrl = "https://discord.com/assets/1f0bfc0865d324c2587920a7d80c609b";
}
}
else
{
iconUrl = "https://cdn.discordapp.com/channel-icons/" + cid + "/" + cicon;
}
iconUrl += ".png?size=32";
var imgRaw = await getIconStream(iconUrl);
imgRaw.Tag = iconUrl;
return imgRaw;
//string rootPath = @"C:\DownloadedImageFromUrl";
//string fileName = System.IO.Path.Combine(rootPath, "test.gif");
//image.Save(fileName);
}
public string toString()
{
string totalString = "cid: " + cid;
if (cname != null) { totalString += ", cname: " + cname; }
totalString += "\nusers: [";
for (int i = 0; i < users.Count; i++)
{
totalString += users[i].toString();
}
return totalString + "]\n";
}
}
}