Archived
0
0
Fork 0

fix: crashing if user doesn't have access to channel

This commit is contained in:
Daryl Ronningen 2021-12-17 19:39:16 -07:00
parent 57734b62dd
commit 1d940b0f7d
Signed by: Daryl Ronningen
GPG key ID: FD23F0C934A5EC6B

View file

@ -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<List<GetMessagesResponse>>(restSharpReq);
var response = await restSharpClient.GetAsync<List<GetMessagesResponse>>(restSharpReq);
var messages = new List<string>();
var messages = new List<string>();
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<int, TreeNode>();
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<int, TreeNode>();
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<string>());
}
};