Archived
0
0
Fork 0

style: fix code indentation in try catch statement

This commit is contained in:
Daryl Ronningen 2021-12-22 15:08:43 -07:00
parent b6a2c1d68f
commit fd6d1830c5
Signed by: Daryl Ronningen
GPG key ID: FD23F0C934A5EC6B

View file

@ -31,319 +31,319 @@ namespace chord
{ {
try { try {
#if DEBUG #if DEBUG
var config = new ConfigurationBuilder() var config = new ConfigurationBuilder()
.AddJsonFile("config.json") .AddJsonFile("config.json")
.Build();
#else
var config = new ConfigurationBuilder()
.AddJsonFile($"{Environment.GetEnvironmentVariable("HOME")}/.config/chord/config.json")
.Build(); .Build();
#else
var config = new ConfigurationBuilder()
.AddJsonFile($"{Environment.GetEnvironmentVariable("HOME")}/.config/chord/config.json")
.Build();
#endif #endif
settings = config.Get<Settings>(); settings = config.Get<Settings>();
var client = new DiscordSocketClient(new DiscordSocketConfig var client = new DiscordSocketClient(new DiscordSocketConfig
{ AlwaysDownloadUsers = true, GatewayIntents = GatewayIntents.All }); { AlwaysDownloadUsers = true, GatewayIntents = GatewayIntents.All });
await client.LoginAsync(TokenType.Bot, settings.Token); await client.LoginAsync(TokenType.Bot, settings.Token);
await client.StartAsync(); await client.StartAsync();
Application.Init(); Application.Init();
window = new Window("chord") window = new Window("chord")
{
X = 0,
Y = 1,
Width = Dim.Fill(),
Height = Dim.Fill(),
ColorScheme =
{ {
Normal = new Attribute(Color.White, Color.Black) X = 0,
} Y = 1,
}; Width = Dim.Fill(),
Height = Dim.Fill(),
client.Ready += () => ColorScheme =
{
guilds = new SortedDictionary<ulong, List<ulong>>();
foreach (var guild in client.Guilds)
{
guilds.Add(guild.Id, new List<ulong>());
foreach (var channel in guild.TextChannels)
{ {
var findGuild = guilds.GetValueOrDefault(guild.Id); Normal = new Attribute(Color.White, Color.Black)
if (channel.Users.ToList().Find(user => user.Id == client.CurrentUser.Id) == null) continue;
if (findGuild == null) continue;
findGuild.Add(channel.Id);
findGuild.Sort();
} }
}
var menuBar = buildMenu();
var serverList = buildServerList();
var channelList = buildChannelList(serverList);
var chatBox = buildChatBox(serverList);
var messageBox = buildMessageBox(serverList, chatBox);
var userList = buildUserList(chatBox);
var serverListList = new ListView
{
X = 0,
Y = 0,
Width = Dim.Fill(),
Height = Dim.Fill()
}; };
var guildNames = guilds.Select(guild => client.GetGuild(guild.Key).Name).ToList(); client.Ready += () =>
serverListList.SetSource(guildNames);
var channelListTree = new TreeView
{ {
X = 0, guilds = new SortedDictionary<ulong, List<ulong>>();
Y = 0,
Width = Dim.Fill(),
Height = Dim.Fill()
};
chatBoxList = new ListView foreach (var guild in client.Guilds)
{
X = 0,
Y = 0,
Width = Dim.Fill(),
Height = Dim.Fill()
};
var messageBoxText = new TextView
{
X = 0,
Y = 0,
Width = Dim.Percent(95),
Height = Dim.Fill()
};
var messageBoxSend = new Button("Send", true)
{
X = Pos.Right(messageBoxText) - 2,
Y = 0
};
var userListTree = new TreeView
{
X = 0,
Y = 0,
Width = Dim.Fill(),
Height = Dim.Fill()
};
serverListList.OpenSelectedItem += args =>
{
channelListTree.ClearObjects();
currentSelectedGuild = guilds.Keys.ToList()[args.Item];
var channelNames = guilds.GetValueOrDefault(currentSelectedGuild)!
.Select(channel => client.GetGuild(currentSelectedGuild).GetTextChannel(channel).Name).ToList();
var categoryDict = new SortedDictionary<ulong, TreeNode>();
foreach (var categories in client.GetGuild(currentSelectedGuild).CategoryChannels)
{ {
var categoryName = Regex.Replace(categories.Name, @"[^\u0000-\u007F]+", string.Empty); guilds.Add(guild.Id, new List<ulong>());
var node = new TreeNode(categoryName);
foreach(var channel in categories.Channels) foreach (var channel in guild.TextChannels)
{ {
if (channel.GetType().Name == "SocketTextChannel") var findGuild = guilds.GetValueOrDefault(guild.Id);
{
var channelName = Regex.Replace(channel.Name, @"[^\u0000-\u007F]+", string.Empty);
var channelNode = new TreeNode(channelName);
channelNode.Tag = new { Id = channel.Id };
node.Children.Add(channelNode); if (channel.Users.ToList().Find(user => user.Id == client.CurrentUser.Id) == null) continue;
} if (findGuild == null) continue;
findGuild.Add(channel.Id);
findGuild.Sort();
} }
categoryDict.Add(categories.Id, node);
} }
foreach (var category in categoryDict) var menuBar = buildMenu();
var serverList = buildServerList();
var channelList = buildChannelList(serverList);
var chatBox = buildChatBox(serverList);
var messageBox = buildMessageBox(serverList, chatBox);
var userList = buildUserList(chatBox);
var serverListList = new ListView
{ {
channelListTree.AddObject(category.Value); X = 0,
} Y = 0,
}; Width = Dim.Fill(),
Height = Dim.Fill()
};
channelListTree.SelectionChanged += async (arg1, arg2) => var guildNames = guilds.Select(guild => client.GetGuild(guild.Key).Name).ToList();
{
userListTree.ClearObjects();
try { serverListList.SetSource(guildNames);
currentSelectedChannel = (ulong)arg2.NewValue.Tag.GetType().GetProperty("Id").GetValue(arg2.NewValue.Tag);
} catch {
return;
}
try { var channelListTree = new TreeView
var restSharpClient = new RestClient("https://discord.com/api/v9"); {
var restSharpReq = new RestRequest($"channels/{currentSelectedChannel}/messages"); X = 0,
restSharpClient.AddDefaultHeader("Authorization", settings.Token); Y = 0,
Width = Dim.Fill(),
Height = Dim.Fill()
};
var response = await restSharpClient.GetAsync<List<GetMessagesResponse>>(restSharpReq); chatBoxList = new ListView
{
X = 0,
Y = 0,
Width = Dim.Fill(),
Height = Dim.Fill()
};
var messages = new List<string>(); var messageBoxText = new TextView
{
X = 0,
Y = 0,
Width = Dim.Percent(95),
Height = Dim.Fill()
};
foreach (var msg in response) var messageBoxSend = new Button("Send", true)
{
X = Pos.Right(messageBoxText) - 2,
Y = 0
};
var userListTree = new TreeView
{
X = 0,
Y = 0,
Width = Dim.Fill(),
Height = Dim.Fill()
};
serverListList.OpenSelectedItem += args =>
{
channelListTree.ClearObjects();
currentSelectedGuild = guilds.Keys.ToList()[args.Item];
var channelNames = guilds.GetValueOrDefault(currentSelectedGuild)!
.Select(channel => client.GetGuild(currentSelectedGuild).GetTextChannel(channel).Name).ToList();
var categoryDict = new SortedDictionary<ulong, TreeNode>();
foreach (var categories in client.GetGuild(currentSelectedGuild).CategoryChannels)
{ {
var msgNewLines = msg.content.Split("\n").ToList(); var categoryName = Regex.Replace(categories.Name, @"[^\u0000-\u007F]+", string.Empty);
var node = new TreeNode(categoryName);
var firstMsg = msgNewLines[0]; foreach(var channel in categories.Channels)
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)
{ {
node.Children.Add(new TreeNode($"{users.Username}#{users.Discriminator} ({users.Nickname})")); if (channel.GetType().Name == "SocketTextChannel")
}
rolesDict.Add(roles.Position, node);
}
foreach (var role in rolesDict)
{
userListTree.AddObject(role.Value);
}
} catch {
await chatBoxList.SetSourceAsync(new List<string>());
}
};
messageBoxSend.Clicked += async () =>
{
var restSharpClient = new RestClient("https://discord.com/api/v9");
var restSharpReq = new RestRequest($"channels/{currentSelectedChannel}/messages");
restSharpClient.AddDefaultHeader("Authorization", settings.Token);
restSharpReq.AddJsonBody(new { content = Encoding.UTF8.GetString(messageBoxText.Text.ToByteArray()) });
await restSharpClient.PostAsync<List<object>>(restSharpReq);
messageBoxText.Text = "";
};
chatBoxList.OpenSelectedItem += args =>
{
if (currentChannelMessages[args.Item].Split(" | ")[1].Contains("http"))
for (var i = 0; i < currentChannelMessages[args.Item].Split(" | ")[1].Split(" ").Count(); i++)
if (currentChannelMessages[args.Item].Split(" | ")[1].Split(" ")[i].Contains("http"))
{
var procStartInfo = new ProcessStartInfo("/bin/sh",
$"-c \"xdg-open {currentChannelMessages[args.Item].Split(" | ")[1].Split(" ")[i]}\"")
{ {
RedirectStandardOutput = true, var channelName = Regex.Replace(channel.Name, @"[^\u0000-\u007F]+", string.Empty);
RedirectStandardError = true, var channelNode = new TreeNode(channelName);
CreateNoWindow = true, channelNode.Tag = new { Id = channel.Id };
UseShellExecute = false
};
var proc = new Process(); node.Children.Add(channelNode);
proc.StartInfo = procStartInfo; }
proc.Start();
} }
};
Application.Top.KeyPress += async args => categoryDict.Add(categories.Id, node);
{ }
switch (ShortcutHelper.GetModifiersKey(args.KeyEvent)) {
case Key.CtrlMask | Key.S: foreach (var category in categoryDict)
serverListList.SetFocus(); {
break; channelListTree.AddObject(category.Value);
case Key.CtrlMask | Key.C: }
channelListTree.SetFocus(); };
break;
case Key.CtrlMask | Key.AltMask | Key.C: channelListTree.SelectionChanged += async (arg1, arg2) =>
chatBoxList.SetFocus(); {
break; userListTree.ClearObjects();
case Key.CtrlMask | Key.U:
userListTree.SetFocus(); try {
break; currentSelectedChannel = (ulong)arg2.NewValue.Tag.GetType().GetProperty("Id").GetValue(arg2.NewValue.Tag);
case Key.CtrlMask | Key.Enter: } catch {
return;
}
try {
var restSharpClient = new RestClient("https://discord.com/api/v9"); var restSharpClient = new RestClient("https://discord.com/api/v9");
var restSharpReq = new RestRequest($"channels/{currentSelectedChannel}/messages"); var restSharpReq = new RestRequest($"channels/{currentSelectedChannel}/messages");
restSharpClient.AddDefaultHeader("Authorization", settings.Token); restSharpClient.AddDefaultHeader("Authorization", settings.Token);
restSharpReq.AddJsonBody(new { content = Encoding.UTF8.GetString(messageBoxText.Text.ToByteArray()) });
await restSharpClient.PostAsync<List<object>>(restSharpReq); var response = await restSharpClient.GetAsync<List<GetMessagesResponse>>(restSharpReq);
messageBoxText.Text = ""; var messages = new List<string>();
break;
} 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)
{
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>());
}
};
messageBoxSend.Clicked += async () =>
{
var restSharpClient = new RestClient("https://discord.com/api/v9");
var restSharpReq = new RestRequest($"channels/{currentSelectedChannel}/messages");
restSharpClient.AddDefaultHeader("Authorization", settings.Token);
restSharpReq.AddJsonBody(new { content = Encoding.UTF8.GetString(messageBoxText.Text.ToByteArray()) });
await restSharpClient.PostAsync<List<object>>(restSharpReq);
messageBoxText.Text = "";
};
chatBoxList.OpenSelectedItem += args =>
{
if (currentChannelMessages[args.Item].Split(" | ")[1].Contains("http"))
for (var i = 0; i < currentChannelMessages[args.Item].Split(" | ")[1].Split(" ").Count(); i++)
if (currentChannelMessages[args.Item].Split(" | ")[1].Split(" ")[i].Contains("http"))
{
var procStartInfo = new ProcessStartInfo("/bin/sh",
$"-c \"xdg-open {currentChannelMessages[args.Item].Split(" | ")[1].Split(" ")[i]}\"")
{
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
UseShellExecute = false
};
var proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
}
};
Application.Top.KeyPress += async args =>
{
switch (ShortcutHelper.GetModifiersKey(args.KeyEvent)) {
case Key.CtrlMask | Key.S:
serverListList.SetFocus();
break;
case Key.CtrlMask | Key.C:
channelListTree.SetFocus();
break;
case Key.CtrlMask | Key.AltMask | Key.C:
chatBoxList.SetFocus();
break;
case Key.CtrlMask | Key.U:
userListTree.SetFocus();
break;
case Key.CtrlMask | Key.Enter:
var restSharpClient = new RestClient("https://discord.com/api/v9");
var restSharpReq = new RestRequest($"channels/{currentSelectedChannel}/messages");
restSharpClient.AddDefaultHeader("Authorization", settings.Token);
restSharpReq.AddJsonBody(new { content = Encoding.UTF8.GetString(messageBoxText.Text.ToByteArray()) });
await restSharpClient.PostAsync<List<object>>(restSharpReq);
messageBoxText.Text = "";
break;
}
};
Application.Top.Add(window, menuBar);
window.Add(serverList, channelList, messageBox, chatBox, userList);
serverList.Add(serverListList);
channelList.Add(channelListTree);
chatBox.Add(chatBoxList);
messageBox.Add(messageBoxText, messageBoxSend);
userList.Add(userListTree);
return Task.CompletedTask;
}; };
Application.Top.Add(window, menuBar); client.MessageReceived += async msg =>
window.Add(serverList, channelList, messageBox, chatBox, userList); {
serverList.Add(serverListList); if (msg.Channel.Id != currentSelectedChannel) return;
channelList.Add(channelListTree);
chatBox.Add(chatBoxList);
messageBox.Add(messageBoxText, messageBoxSend);
userList.Add(userListTree);
return Task.CompletedTask; var restSharpClient = new RestClient("https://discord.com/api/v9");
}; var restSharpReq = new RestRequest($"channels/{currentSelectedChannel}/messages");
restSharpReq.AddQueryParameter("limit", "1");
restSharpClient.AddDefaultHeader("Authorization", settings.Token);
client.MessageReceived += async msg => var response = await restSharpClient.GetAsync<List<GetMessagesResponse>>(restSharpReq);
{
if (msg.Channel.Id != currentSelectedChannel) return;
var restSharpClient = new RestClient("https://discord.com/api/v9"); currentChannelMessages.Add(
var restSharpReq = new RestRequest($"channels/{currentSelectedChannel}/messages"); $"{response.First().author.username}#{response.First().author.discriminator} | {response.First().content}");
restSharpReq.AddQueryParameter("limit", "1"); await chatBoxList.SetSourceAsync(currentChannelMessages);
restSharpClient.AddDefaultHeader("Authorization", settings.Token);
var response = await restSharpClient.GetAsync<List<GetMessagesResponse>>(restSharpReq); chatBoxList.ScrollDown(currentChannelMessages.Count - chatBoxList.Bounds.Height);
chatBoxList.SelectedItem = currentChannelMessages.Count - 1;
};
currentChannelMessages.Add( Application.MainLoop.AddTimeout(TimeSpan.FromMilliseconds(100), caller => true);
$"{response.First().author.username}#{response.First().author.discriminator} | {response.First().content}");
await chatBoxList.SetSourceAsync(currentChannelMessages);
chatBoxList.ScrollDown(currentChannelMessages.Count - chatBoxList.Bounds.Height); Application.Run();
chatBoxList.SelectedItem = currentChannelMessages.Count - 1; Application.Shutdown();
};
Application.MainLoop.AddTimeout(TimeSpan.FromMilliseconds(100), caller => true);
Application.Run();
Application.Shutdown();
} catch (System.Exception err) { } catch (System.Exception err) {
Console.WriteLine(err); Console.WriteLine(err);
} }