Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #23: Better Webhook Integration #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions SquadForger/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<appSettings configSource="PrivateData.config">

<appSettings >
<add key="dev_webhook" value="" />
</appSettings>

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
Expand Down
10 changes: 10 additions & 0 deletions SquadForger/Components/BindabalePasswordBox.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<UserControl x:Class="SquadForger.Components.BindabalePasswordBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SquadForger.Components"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<PasswordBox x:Name="passwordBox" Width="230" Height="20" Margin="10" Background="#9e8cbb" PasswordChanged="PasswordBox_OnPasswordChanged" ></PasswordBox>
</UserControl>
27 changes: 27 additions & 0 deletions SquadForger/Components/BindabalePasswordBox.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Windows;
using System.Windows.Controls;

namespace SquadForger.Components
{
public partial class BindabalePasswordBox : UserControl
{
public static readonly DependencyProperty PasswordProperty = DependencyProperty.Register(
"Password", typeof(string), typeof(BindabalePasswordBox), new PropertyMetadata("Discord Webhook ID"));

public string Password
{
get { return (string)GetValue(PasswordProperty); }
set { SetValue(PasswordProperty, value); }
}

public BindabalePasswordBox()
{
InitializeComponent();
}

private void PasswordBox_OnPasswordChanged(object sender, RoutedEventArgs e)
{
Password=passwordBox.Password;
}
}
}
8 changes: 5 additions & 3 deletions SquadForger/SquadForger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Components\BindabalePasswordBox.xaml.cs">
<DependentUpon>BindabalePasswordBox.xaml</DependentUpon>
</Compile>
<Compile Include="Converter\TextToVisibilityConverter.cs" />
<Compile Include="Model\Champions.cs" />
<Compile Include="Model\LeagueVersion.cs" />
Expand All @@ -262,6 +265,7 @@
<Compile Include="View\SquadView.xaml.cs">
<DependentUpon>SquadView.xaml</DependentUpon>
</Compile>
<Page Include="Components\BindabalePasswordBox.xaml" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down Expand Up @@ -301,9 +305,7 @@
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="packages.config" />
<None Include="PrivateData.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>

<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
19 changes: 17 additions & 2 deletions SquadForger/View/DiscordView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SquadForger.View"
xmlns:components = "clr-namespace:SquadForger.Components"
xmlns:vm="clr-namespace:SquadForger.ViewModel"
mc:Ignorable="d"
Background="Transparent"
Expand Down Expand Up @@ -73,6 +74,7 @@
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition MaxHeight="50"/>
<RowDefinition MaxHeight="50"/>
</Grid.RowDefinitions>
<StackPanel>

Expand All @@ -93,13 +95,26 @@
Margin="0,0,10,0"
Background="#e3c57e"
Foreground="black"/>
<Button Command="{Binding PostToDiscordCommand}"
<Button Style="{DynamicResource DiscordViewButtonTemplate}" Command="{Binding PostToDiscordCommand}"
Content="Send to discord"
Height="40"
Width="100"
Background="#e3c57e"
Foreground="black"/>


</StackPanel>

<StackPanel Grid.Row="2" Orientation="Horizontal">

<components:BindabalePasswordBox Password="{Binding WebhookID,UpdateSourceTrigger=PropertyChanged , Mode=TwoWay}"/>
<Button Style="{DynamicResource DiscordViewButtonTemplate}" Command="{Binding UpdateWebhookIDCommand}"
Content="Update Webhook ID"
Height="40"
Width="140"
Background="#e3c57e"
Foreground="black"/>

</StackPanel>

</Grid>
</UserControl>
52 changes: 51 additions & 1 deletion SquadForger/ViewModel/DiscordVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Xml;
using SquadForger.Model;
using SquadForger.Services;

Expand All @@ -23,12 +26,14 @@ public string TextToSend
}
public RelayCommand PostToDiscordCommand { get; private set; }
public RelayCommand UpdatePreviewCommand { get; private set; }
public RelayCommand UpdateWebhookIDCommand { get; private set; }
public List<Team> Teams { get; private set; } = new List<Team>();
public string WebhookID { get; set; } = "";
public DiscordVM()
{
PostToDiscordCommand = new RelayCommand(PostToDiscord);
UpdatePreviewCommand = new RelayCommand(UpdatePreview);

UpdateWebhookIDCommand = new RelayCommand(UpdateWebhookID);
ServiceLocator.Instance.EventAggregator.Subscribe<TeamsUpdatedEvent>(OnTeamsUpdated);
}
private void OnTeamsUpdated(TeamsUpdatedEvent eventArgs)
Expand All @@ -48,6 +53,51 @@ private void UpdatePreview()

TextToSend = stringBuilder.ToString();
}

private void UpdateWebhookID()
{
if (string.IsNullOrEmpty(WebhookID))
{
MessageBox.Show("Webhook ID is empty!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}

string discordWebhookPattern = @"^https:\/\/discord\.com\/api\/webhooks\/\d+\/[A-Za-z0-9_-]+$";
if (!Regex.IsMatch(WebhookID, discordWebhookPattern))
{
MessageBox.Show("Invalid Webhook ID format. Please provide a valid Discord Webhook URL.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
UpdateWebhookInConfig(WebhookID);
}

private void UpdateWebhookInConfig( string newWebhookID)
{
string configFilePath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

// Step 1: Load the app.config file
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(configFilePath);

// Find the 'dev_webhook' node
XmlNode webhookNode = xmlDoc.SelectSingleNode("//appSettings/add[@key='dev_webhook']");

if (webhookNode != null)
{
// Update the webhook ID value
webhookNode.Attributes["value"].Value = newWebhookID;

// Save the updated config file
xmlDoc.Save(configFilePath);
ConfigurationManager.RefreshSection("appSettings");
MessageBox.Show("Webhook ID updated successfully in app.config!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
MessageBox.Show("Webhook ID key not found in app.config.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}

private async void PostToDiscord()
{
if (TextToSend.Equals("<empty>"))
Expand Down