From 6a9e4978aa888a28e0d907422eae2e2a48e96c30 Mon Sep 17 00:00:00 2001 From: Florian Stinglmayr Date: Sat, 17 Aug 2024 18:38:00 +0200 Subject: [PATCH] add possibility to post logs to discord webhooks --- EliteBGS/DiscordPoster.cs | 32 +++++++ EliteBGS/LoadEntriesWindow.xaml.cs | 2 +- EliteBGS/MainWindow.xaml | 57 ++++++++++++- EliteBGS/MainWindow.xaml.cs | 127 +++++++++++++++++++++++++++ EliteBGS/Util/AppConfig.cs | 132 +++++++++++++++-------------- EliteBGS/Util/Config.cs | 1 + EliteBGS/Util/DiscordWebhook.cs | 13 +++ 7 files changed, 299 insertions(+), 65 deletions(-) create mode 100644 EliteBGS/DiscordPoster.cs create mode 100644 EliteBGS/Util/DiscordWebhook.cs diff --git a/EliteBGS/DiscordPoster.cs b/EliteBGS/DiscordPoster.cs new file mode 100644 index 0000000..6cc59af --- /dev/null +++ b/EliteBGS/DiscordPoster.cs @@ -0,0 +1,32 @@ +using EliteBGS.Util; +using System; +using System.Text; +using System.Net.Http; +using System.Text.Json.Nodes; + +namespace EliteBGS; + +public class DiscordPoster { + public static readonly int DiscordLimit = 2000; + + public static void PostToDiscord(DiscordWebhook webhook, string log) { + JsonObject obj = new(); + obj.Add("content", log); + obj.Add("username", "EDBGS"); + + using (var client = new HttpClient()) { + var content = new StringContent(obj.ToString(), Encoding.UTF8, "application/json"); + var response = client.PostAsync(webhook.Webhook, content); + if (response == null) { + throw new Exception("failed to post content to Discord webhook"); + } + response.Wait(); + var resp = response.Result; + if (!resp.IsSuccessStatusCode) { + throw new Exception(string.Format( + "failed to post content to webhook {0}: {1} / {2}", + webhook.Name, resp.StatusCode, resp.Content.ToString())); + } + } + } +} diff --git a/EliteBGS/LoadEntriesWindow.xaml.cs b/EliteBGS/LoadEntriesWindow.xaml.cs index 12727f3..94a086a 100644 --- a/EliteBGS/LoadEntriesWindow.xaml.cs +++ b/EliteBGS/LoadEntriesWindow.xaml.cs @@ -60,7 +60,7 @@ public partial class LoadEntriesWindow : Window { dialog.DefaultExt = ".log"; dialog.Filter = "Log files (*.log)|*.log|All files (*.*)|*"; - var location = config.Global.DefaultJournalLocation; + var location = AppConfig.DefaultJournalLocation; if (Directory.Exists(location)) { dialog.InitialDirectory = location; } diff --git a/EliteBGS/MainWindow.xaml b/EliteBGS/MainWindow.xaml index 5a14c7d..92284f6 100644 --- a/EliteBGS/MainWindow.xaml +++ b/EliteBGS/MainWindow.xaml @@ -5,9 +5,13 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:EliteBGS" - xmlns:Util="clr-namespace:EliteBGS.Util" d:DataContext="{d:DesignInstance Type=Util:AppConfig}" x:Name="window" x:Class="EliteBGS.MainWindow" + xmlns:Util="clr-namespace:EliteBGS.Util" + d:DataContext="{d:DesignInstance Type=Util:AppConfig}" + x:Name="window" + x:Class="EliteBGS.MainWindow" mc:Ignorable="d" - Title="Elite: Dangerous BGS Helper" Height="520" Width="950" Icon="Salus.ico" Closing="window_Closing"> + Title="Elite: Dangerous BGS Helper" Height="620" Width="950" Icon="Salus.ico" Closing="window_Closing" + > + + + + + + + + + + + + + + + + + + + + @@ -244,6 +268,35 @@ + + + + + + + + + + + + + +