From 2bf8d9018ddef0a85262af024d2c416634bc4263 Mon Sep 17 00:00:00 2001 From: Florian Stinglmayr Date: Wed, 18 Sep 2024 21:10:41 +0200 Subject: [PATCH] further refine splitting of logs --- EliteBGS/DiscordLogGenerator.cs | 10 +++++++- EliteBGS/MainWindow.xaml.cs | 42 +++++++++++++++++++++------------ 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/EliteBGS/DiscordLogGenerator.cs b/EliteBGS/DiscordLogGenerator.cs index 6764622..d941366 100644 --- a/EliteBGS/DiscordLogGenerator.cs +++ b/EliteBGS/DiscordLogGenerator.cs @@ -228,7 +228,7 @@ public class DiscordLogGenerator { int curchunk = 0; string botheader = BotHeader().Trim() + "\n"; - // Leave room for botheder and some leeway + // Leave room for botheader and some leeway int maxlength = (2000 - botheader.Length - 10); // Then try to collate chunks @@ -249,6 +249,14 @@ public class DiscordLogGenerator { chunks[i] = chunks[i].Insert(0, botheader); } + // Now finally check if any one chunk is bigger than max length + for (int i = 0; i < chunks.Count; i++) { + if (chunks[i].Length > maxcount) { + throw new ApplicationException( + string.Format("a chunk turned out bigger than {0}", maxcount)); + } + } + return chunks.ToArray(); } } diff --git a/EliteBGS/MainWindow.xaml.cs b/EliteBGS/MainWindow.xaml.cs index e00c02f..b0a224b 100644 --- a/EliteBGS/MainWindow.xaml.cs +++ b/EliteBGS/MainWindow.xaml.cs @@ -653,22 +653,36 @@ public partial class MainWindow : MetroWindow { } } - private void PostToDiscordWebhook(DiscordWebhook hook) { + private void PostToDiscordWebhook(IEnumerable hooks) { if (string.IsNullOrEmpty(DiscordLog.Text)) { return; } - try { - DiscordLogGenerator discord = LogType.SelectedItem as DiscordLogGenerator; - var chunks = discord.SplitLog(DiscordLog.Text); - foreach (var chunk in chunks) { - DiscordPoster.PostToDiscord(hook, chunk); + DiscordLogGenerator discord = LogType.SelectedItem as DiscordLogGenerator; + string[] chunks; + + try { + chunks = discord.SplitLog(DiscordLog.Text); + } catch (Exception) { + MessageBox.Show( + "The log could not be split into discord appropriate length.\n" + + "This happens with the bigger logs formats if you do lots of things in one system.\n" + + "Try posting the log in the OneLine format.", + "Sorry!", MessageBoxButton.OK, MessageBoxImage.Error + ); + return; + } + + foreach (var hook in hooks) { + try { + foreach (var chunk in chunks) { + DiscordPoster.PostToDiscord(hook, chunk); + } + Log(string.Format("successfully posted to discord webhook `{0}`", hook.Name)); + } catch (Exception ex) { + Log(string.Format("failed to post to discord webhook `{0}`: {1}", + hook.Name, ex.Message)); } - Log(string.Format("successfully posted to discord webhook {0}", - hook.Name)); - } catch (Exception ex) { - Log(string.Format("failed to post to discord webhook {0}: {1}", - hook.Name, ex.Message)); } } @@ -683,12 +697,10 @@ public partial class MainWindow : MetroWindow { if (hook == null) { return; } - PostToDiscordWebhook(hook); + PostToDiscordWebhook(new DiscordWebhook[] { hook }); } private void PostToAll_Click(object sender, RoutedEventArgs e) { - foreach (DiscordWebhook hook in Config.Global.Webhooks) { - PostToDiscordWebhook(hook); - } + PostToDiscordWebhook(Config.Global.Webhooks); } }