implement log splitting for discord posting

This commit is contained in:
Florian Stinglmayr 2024-09-17 19:53:17 +02:00
parent fd3e5f61cb
commit 20adf93d39
5 changed files with 89 additions and 23 deletions

View File

@ -198,4 +198,53 @@ public class DiscordLogGenerator {
return log.ToString().Trim(); return log.ToString().Trim();
} }
public virtual string[] SplitLog(string log, int maxcount = 2000) {
throw new NotImplementedException();
}
protected string[] SplitLogWithHeader(string log, string header, int maxcount = 2000) {
string[] lines = log.Split("\n");
List<string> chunks = new();
string chunk = string.Empty;
bool done = false;
// First split the log into its headers
// skip first bot header line
for (int i = 1; i < lines.Length; i++) {
string line = lines[i];
if (line.StartsWith(header) && !string.IsNullOrEmpty(chunk)) {
chunks.Add(chunk.Trim());
chunk = string.Empty;
}
chunk = chunk + "\n" + line;
}
int curchunk = 0;
string botheader = BotHeader().Trim() + "\n";
// Leave room for botheder and some leeway
int maxlength = (2000 - botheader.Length - 10);
// Then try to collate chunks
for (curchunk = 0; curchunk < chunks.Count; ++curchunk) {
int count = chunks[curchunk].Length;
while (count < maxlength && (curchunk+1) < chunks.Count) {
count += chunks[curchunk + 1].Length + 2;
if (count < maxlength) {
chunks[curchunk] += "\n";
chunks[curchunk] += chunks[curchunk + 1];
chunks.RemoveAt(curchunk + 1);
}
}
}
// Readd bott headers
for (int i = 0; i < chunks.Count; i++) {
chunks[i] = chunks[i].Insert(0, botheader);
}
return chunks.ToArray();
}
} }

View File

@ -1,4 +1,7 @@
namespace EliteBGS; using System.Collections.Generic;
using System.Windows.Documents;
namespace EliteBGS;
public class GenericDiscordLog : DiscordLogGenerator { public class GenericDiscordLog : DiscordLogGenerator {
public override string ToString() { public override string ToString() {
@ -8,4 +11,8 @@ public class GenericDiscordLog : DiscordLogGenerator {
public override string Name { public override string Name {
get { return "Generic"; } get { return "Generic"; }
} }
public override string[] SplitLog(string log, int maxcount = 2000) {
return SplitLogWithHeader(log, "**Date:**", maxcount);
}
} }

View File

@ -653,27 +653,17 @@ public partial class MainWindow : MetroWindow {
} }
} }
private bool CheckDiscordPostMessageLength() {
var content = DiscordLog.Text;
if (string.IsNullOrEmpty(content)) {
return true;
}
if (content.Length >= DiscordPoster.DiscordLimit) {
MessageBox.Show("The log is too long for discord posting (limit of 2000 characters).",
"Error", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
return true;
}
private void PostToDiscordWebhook(DiscordWebhook hook) { private void PostToDiscordWebhook(DiscordWebhook hook) {
if (string.IsNullOrEmpty(DiscordLog.Text)) { if (string.IsNullOrEmpty(DiscordLog.Text)) {
return; return;
} }
try { try {
DiscordPoster.PostToDiscord(hook, DiscordLog.Text); DiscordLogGenerator discord = LogType.SelectedItem as DiscordLogGenerator;
var chunks = discord.SplitLog(DiscordLog.Text);
foreach (var chunk in chunks) {
DiscordPoster.PostToDiscord(hook, chunk);
}
Log(string.Format("successfully posted to discord webhook {0}", Log(string.Format("successfully posted to discord webhook {0}",
hook.Name)); hook.Name));
} catch (Exception ex) { } catch (Exception ex) {
@ -693,16 +683,10 @@ public partial class MainWindow : MetroWindow {
if (hook == null) { if (hook == null) {
return; return;
} }
if (!CheckDiscordPostMessageLength()) {
return;
}
PostToDiscordWebhook(hook); PostToDiscordWebhook(hook);
} }
private void PostToAll_Click(object sender, RoutedEventArgs e) { private void PostToAll_Click(object sender, RoutedEventArgs e) {
if (!CheckDiscordPostMessageLength()) {
return;
}
foreach (DiscordWebhook hook in Config.Global.Webhooks) { foreach (DiscordWebhook hook in Config.Global.Webhooks) {
PostToDiscordWebhook(hook); PostToDiscordWebhook(hook);
} }

View File

@ -91,4 +91,8 @@ public class NonaDiscordLog : DiscordLogGenerator {
public override string Name { public override string Name {
get { return "NovaNavy"; } get { return "NovaNavy"; }
} }
public override string[] SplitLog(string log, int maxcount = 2000) {
return SplitLogWithHeader(log, ":clock2: `Date:`", maxcount);
}
} }

View File

@ -1,6 +1,9 @@
using EliteBGS.LogGenerator; using EliteBGS.LogGenerator;
using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Windows.Documents;
namespace EliteBGS; namespace EliteBGS;
@ -25,6 +28,25 @@ public class OneLineDiscordLog : DiscordLogGenerator {
return ""; return "";
} }
public override string[] SplitLog(string log, int maxcount = 2000) {
string[] lines = log.Split('\n');
List<string> chunks = new();
string chunk = string.Empty;
for (int i = 0; i < lines.Length; i++) {
string line = lines[i];
if ((chunk.Length + line.Length) > maxcount || i == lines.Length - 1) {
chunks.Add(chunk.Trim());
chunk = string.Empty;
chunk = chunk.Insert(0, BotHeader()).Trim();
} else {
chunk = chunk + "\n" + line;
}
}
return chunks.ToArray();
}
public override string GenerateDiscordLog(Report report) { public override string GenerateDiscordLog(Report report) {
StringBuilder log = new StringBuilder(); StringBuilder log = new StringBuilder();