From 1d940b0f7d14dcef5b3d91f9145062e679b70a0d Mon Sep 17 00:00:00 2001 From: Daryl Ronningen Date: Fri, 17 Dec 2021 19:39:16 -0700 Subject: [PATCH] fix: crashing if user doesn't have access to channel --- Program.cs | 102 ++++++++++++++++++++++++++++------------------------- 1 file changed, 53 insertions(+), 49 deletions(-) diff --git a/Program.cs b/Program.cs index d0edfa7..bff7e72 100644 --- a/Program.cs +++ b/Program.cs @@ -186,62 +186,66 @@ namespace chord return; } - var restSharpClient = new RestClient("https://discord.com/api/v9"); - var restSharpReq = new RestRequest($"channels/{currentSelectedChannel}/messages"); - restSharpClient.AddDefaultHeader("Authorization", settings.Token); + try { + var restSharpClient = new RestClient("https://discord.com/api/v9"); + var restSharpReq = new RestRequest($"channels/{currentSelectedChannel}/messages"); + restSharpClient.AddDefaultHeader("Authorization", settings.Token); - var response = await restSharpClient.GetAsync>(restSharpReq); + var response = await restSharpClient.GetAsync>(restSharpReq); - var messages = new List(); + var messages = new List(); - foreach (var msg in response) - { - var msgNewLines = msg.content.Split("\n").ToList(); - - var firstMsg = msgNewLines[0]; - - msgNewLines.RemoveAt(0); - msgNewLines.Reverse(); - - messages.AddRange(msgNewLines.Select(message => $"{message}")); - - messages.Add($"{msg.author.username}#{msg.author.discriminator} | {firstMsg}"); - - if (msg.embeds.Count != 0) - messages.Add($"{msg.author.username}#{msg.author.discriminator} | [Unable to display embed]"); - - if (msg.attachments.Count == 0) continue; - messages.AddRange(msg.attachments - .Select(attachment => $"{msg.author.username}#{msg.author.discriminator} | {attachment.url}") - .Select(dummy => dummy)); - } - - messages.Reverse(); - - await chatBoxList.SetSourceAsync(messages); - currentChannelMessages = messages; - - chatBoxList.ScrollDown(currentChannelMessages.Count - chatBoxList.Bounds.Height); - chatBoxList.SelectedItem = currentChannelMessages.Count - 1; - - var rolesDict = new SortedDictionary(); - - foreach (var roles in client.GetGuild(currentSelectedGuild).Roles) - { - var roleName = Regex.Replace(roles.Name, @"[^\u0000-\u007F]+", string.Empty); - var node = new TreeNode(roleName); - - foreach (var users in roles.Members) + foreach (var msg in response) { - node.Children.Add(new TreeNode($"{users.Username}#{users.Discriminator} ({users.Nickname})")); + var msgNewLines = msg.content.Split("\n").ToList(); + + var firstMsg = msgNewLines[0]; + + msgNewLines.RemoveAt(0); + msgNewLines.Reverse(); + + messages.AddRange(msgNewLines.Select(message => $"{message}")); + + messages.Add($"{msg.author.username}#{msg.author.discriminator} | {firstMsg}"); + + if (msg.embeds.Count != 0) + messages.Add($"{msg.author.username}#{msg.author.discriminator} | [Unable to display embed]"); + + if (msg.attachments.Count == 0) continue; + messages.AddRange(msg.attachments + .Select(attachment => $"{msg.author.username}#{msg.author.discriminator} | {attachment.url}") + .Select(dummy => dummy)); } - rolesDict.Add(roles.Position, node); - } + messages.Reverse(); - foreach (var role in rolesDict) - { - userListTree.AddObject(role.Value); + await chatBoxList.SetSourceAsync(messages); + currentChannelMessages = messages; + + chatBoxList.ScrollDown(currentChannelMessages.Count - chatBoxList.Bounds.Height); + chatBoxList.SelectedItem = currentChannelMessages.Count - 1; + + var rolesDict = new SortedDictionary(); + + foreach (var roles in client.GetGuild(currentSelectedGuild).Roles) + { + var roleName = Regex.Replace(roles.Name, @"[^\u0000-\u007F]+", string.Empty); + var node = new TreeNode(roleName); + + foreach (var users in roles.Members) + { + node.Children.Add(new TreeNode($"{users.Username}#{users.Discriminator} ({users.Nickname})")); + } + + rolesDict.Add(roles.Position, node); + } + + foreach (var role in rolesDict) + { + userListTree.AddObject(role.Value); + } + } catch { + await chatBoxList.SetSourceAsync(new List()); } };