Archived
0
0
Fork 0

fix: rare crashing due to non-ascii chars in channel/role names

This commit is contained in:
Daryl Ronningen 2021-12-17 19:35:21 -07:00
parent dc416c085c
commit 57734b62dd
Signed by: Daryl Ronningen
GPG key ID: FD23F0C934A5EC6B

View file

@ -1,5 +1,6 @@
using System.Diagnostics; using System.Diagnostics;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using Discord; using Discord;
using Discord.WebSocket; using Discord.WebSocket;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@ -28,6 +29,7 @@ namespace chord
private async void Start() private async void Start()
{ {
try {
#if DEBUG #if DEBUG
var config = new ConfigurationBuilder() var config = new ConfigurationBuilder()
.AddJsonFile("config.json") .AddJsonFile("config.json")
@ -139,6 +141,8 @@ namespace chord
serverListList.OpenSelectedItem += args => serverListList.OpenSelectedItem += args =>
{ {
channelListTree.ClearObjects();
currentSelectedGuild = guilds.Keys.ToList()[args.Item]; currentSelectedGuild = guilds.Keys.ToList()[args.Item];
var channelNames = guilds.GetValueOrDefault(currentSelectedGuild)! var channelNames = guilds.GetValueOrDefault(currentSelectedGuild)!
@ -148,13 +152,15 @@ namespace chord
foreach (var categories in client.GetGuild(currentSelectedGuild).CategoryChannels) foreach (var categories in client.GetGuild(currentSelectedGuild).CategoryChannels)
{ {
var node = new TreeNode(categories.Name); var categoryName = Regex.Replace(categories.Name, @"[^\u0000-\u007F]+", string.Empty);
var node = new TreeNode(categoryName);
foreach(var channel in categories.Channels) foreach(var channel in categories.Channels)
{ {
if (channel.GetType().Name == "SocketTextChannel") if (channel.GetType().Name == "SocketTextChannel")
{ {
var channelNode = new TreeNode(channel.Name); var channelName = Regex.Replace(categories.Name, @"[^\u0000-\u007F]+", string.Empty);
var channelNode = new TreeNode(channelName);
channelNode.Tag = new { Id = channel.Id }; channelNode.Tag = new { Id = channel.Id };
node.Children.Add(channelNode); node.Children.Add(channelNode);
@ -172,6 +178,8 @@ namespace chord
channelListTree.SelectionChanged += async (arg1, arg2) => channelListTree.SelectionChanged += async (arg1, arg2) =>
{ {
userListTree.ClearObjects();
try { try {
currentSelectedChannel = (ulong)arg2.NewValue.Tag.GetType().GetProperty("Id").GetValue(arg2.NewValue.Tag); currentSelectedChannel = (ulong)arg2.NewValue.Tag.GetType().GetProperty("Id").GetValue(arg2.NewValue.Tag);
} catch { } catch {
@ -220,7 +228,8 @@ namespace chord
foreach (var roles in client.GetGuild(currentSelectedGuild).Roles) foreach (var roles in client.GetGuild(currentSelectedGuild).Roles)
{ {
var node = new TreeNode(roles.Name); var roleName = Regex.Replace(roles.Name, @"[^\u0000-\u007F]+", string.Empty);
var node = new TreeNode(roleName);
foreach (var users in roles.Members) foreach (var users in roles.Members)
{ {
@ -331,6 +340,9 @@ namespace chord
Application.Run(); Application.Run();
Application.Shutdown(); Application.Shutdown();
} catch (System.Exception err) {
Console.WriteLine(err);
}
} }
private Window buildChatBox(Window serverList) private Window buildChatBox(Window serverList)