Skip to content

Commit

Permalink
fixed group chats
Browse files Browse the repository at this point in the history
  • Loading branch information
n1d3v committed Jun 18, 2024
1 parent cdf550c commit 76b2f71
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 79 deletions.
35 changes: 9 additions & 26 deletions Naticord/Forms/Group.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 21 additions & 9 deletions Naticord/Forms/Group.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ public Group(long chatid, string token, string userpfp)
AccessToken = token;
ChatID = chatid;
userPFP = userpfp;
LoadGroupName();
LoadMessages();
LoadGroupMembers();
websocketClient = new WebSocketClientGroup(AccessToken, this);
}

private void Group_Load(object sender, EventArgs e)
{
chatBox.DocumentText = "";
}


private void SetProfilePictureShape(PictureBox pictureBox)
{
var path = new System.Drawing.Drawing2D.GraphicsPath();
Expand Down Expand Up @@ -283,26 +289,32 @@ public async Task<dynamic> GetApiResponse(string endpoint)
}
}

private async void LoadGroupMembers()
// this can be easily replaced by something more simple but it works sooooo
private async void LoadGroupName()
{
try
{
dynamic members = await GetApiResponse($"channels/{ChatID}/members");

groupMembersList.Items.Clear();
dynamic channels = await GetApiResponse("users/@me/channels");
string groupName = "";

foreach (var member in members)
foreach (var channel in channels)
{
string memberName = member.global_name ?? member.username;
groupMembersList.Items.Add(memberName);
if (channel.type == 3 && (long)channel.id == ChatID)
{
groupName = channel.name ?? "Group Chat"; // group chat is for "we couldnt find out the name go fuck yourself"
break;
}
}

usernameLabel.Text = groupName;
}
catch (Exception ex)
{
ShowErrorMessage("Failed to retrieve group members", ex);
ShowErrorMessage("Failed to retrieve group name", ex);
}
}


private void ShowErrorMessage(string message, Exception ex)
{
MessageBox.Show($"{message}\n\nError: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Expand Down
107 changes: 65 additions & 42 deletions Naticord/Forms/Naticord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public partial class Naticord : Form
private string AccessToken;
private Login signin;
private string userPFP;
private Dictionary<string, long> groupChatIDs = new Dictionary<string, long>();

private List<ListViewItem> allFriends;
private List<ListViewItem> allServers;
Expand Down Expand Up @@ -227,6 +228,7 @@ private void SetUserInfo()
}
}

// absolute garbo, dont touch this unless necessary (it will combust into pieces). if it compiles thats great and if it works thats even greater.
private void PopulateFriendsTab()
{
try
Expand Down Expand Up @@ -288,6 +290,8 @@ private void PopulateFriendsTab()
{
namesOrName = (string)channel.name;
channelType = "Group Message";

SaveGroupChatID(namesOrName, channelId);
}
else if (channel.recipients != null && channel.recipients.Count > 0)
{
Expand All @@ -299,13 +303,22 @@ private void PopulateFriendsTab()
}
namesOrName = string.Join(", ", names);
channelType = "Group Message";

SaveGroupChatID(namesOrName, channelId);
}

namesOrName += $" - {channel.recipients.Count} members";

if (!groupChatIDs.ContainsKey(namesOrName))
{
groupChatIDs[namesOrName] = channelId;
}
break;
}

allFriends.Add(new ListViewItem(namesOrName));
ListViewItem item = new ListViewItem(namesOrName);
item.Tag = channelType;
allFriends.Add(item);
channelIds.Add(channelId);
}

Expand All @@ -318,6 +331,10 @@ private void PopulateFriendsTab()
}
}

private void SaveGroupChatID(string groupName, long chatID)
{
groupChatIDs[groupName] = chatID;
}

private long GetChatID(string name)
{
Expand All @@ -326,26 +343,23 @@ private long GetChatID(string name)
dynamic channels = GetApiResponse("users/@me/channels");
foreach (var channel in channels)
{
if (channel.type == 1 && channel.type == 3 && channel.recipients[0].global_name != null)
{
if ((string)channel.recipients[0].global_name == name)
{
return (long)channel.id;
}
}
else if (channel.type == 1 && channel.type == 3 && channel.recipients[0].username != null)
if (channel.type == 1) // i fucked up this line so bad it once started to load every single group chat i had lmfao great coding skillz 1337
{
if ((string)channel.recipients[0].username == name)
foreach (var recipient in channel.recipients)
{
return (long)channel.id;
string recipientName = recipient.global_name ?? recipient.username;
if (recipientName == name)
{
return (long)channel.id;
}
}
}
}
return -1;
}
catch (WebException ex)
{
ShowErrorMessage("Failed to retrieve friends", ex);
ShowErrorMessage("Failed to retrieve chat ID", ex);
return -1;
}
}
Expand All @@ -357,16 +371,10 @@ private long GetFriendID(string name)
dynamic friends = GetApiResponse("users/@me/relationships");
foreach (var friend in friends)
{
if (friend.type == 1 && friend.user.global_name != null)
{
if ((string)friend.user.global_name == name)
{
return (long)friend.id;
}
}
else if (friend.type == 1 && friend.user.username != null)
if (friend.type == 1)
{
if ((string)friend.user.username == name)
string friendName = friend.user.global_name ?? friend.user.username;
if (friendName == name)
{
return (long)friend.id;
}
Expand All @@ -376,28 +384,20 @@ private long GetFriendID(string name)
}
catch (WebException ex)
{
ShowErrorMessage("Failed to retrieve friends", ex);
ShowErrorMessage("Failed to retrieve friend ID", ex);
return -1;
}
}

private long GetGroupID(string name)
{
try
if (groupChatIDs.ContainsKey(name))
{
dynamic channels = GetApiResponse("users/@me/channels");
foreach (var channel in channels)
{
if (channel.type == 3 && channel.name == name)
{
return (long)channel.id;
}
}
return -1;
return groupChatIDs[name];
}
catch (WebException ex)
else
{
ShowErrorMessage("Failed to retrieve group list", ex);
// Handle case when ID is not found
return -1;
}
}
Expand Down Expand Up @@ -466,21 +466,44 @@ protected override void OnFormClosing(FormClosingEventArgs e)
signin.Close();
}

private void friendsList_DoubleClick(object sender, EventArgs ex)
private void friendsList_DoubleClick(object sender, EventArgs e)
{
if (friendsList.SelectedItems.Count > 0)
{
string selectedFriend = friendsList.SelectedItems[0].Text;
long chatID = GetChatID(selectedFriend);
long friendID = GetFriendID(selectedFriend);
if (chatID >= 0)
string selectedChannel = friendsList.SelectedItems[0].Text;
string channelType = friendsList.SelectedItems[0].Tag as string; // Retrieve channel type from Tag

if (channelType == "Direct Message")
{
DM dm = new DM(chatID, friendID, AccessToken, userPFP);
dm.Show();
long chatID = GetChatID(selectedChannel);
if (chatID >= 0)
{
DM dm = new DM(chatID, GetFriendID(selectedChannel), AccessToken, userPFP);
dm.Show();
Console.WriteLine($"Direct Message Chat ID: {chatID}");
}
else
{
MessageBox.Show("Unable to open Direct Message chat. Please try again later.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else if (channelType == "Group Message")
{
long groupID = GetGroupID(selectedChannel);
if (groupID >= 0)
{
Group groupChat = new Group(groupID, AccessToken, userPFP);
groupChat.Show();
Console.WriteLine($"Group Chat ID: {groupID}");
}
else
{
MessageBox.Show("Unable to open Group Message chat. Please try again later.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Unable to open this DM. If you are trying to access a group chat, they will not work for now.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show("Unknown channel type.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions Naticord/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
<Setting Name="proxy" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="groupids" Type="System.Collections.Specialized.StringCollection" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>
4 changes: 2 additions & 2 deletions Naticord/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
<userSettings>
<Naticord.Properties.Settings>
<setting name="token" serializeAs="String">
<value/>
<value />
</setting>
<setting name="proxy" serializeAs="String">
<value/>
<value />
</setting>
</Naticord.Properties.Settings>
</userSettings>
Expand Down

0 comments on commit 76b2f71

Please sign in to comment.