-
Notifications
You must be signed in to change notification settings - Fork 4
/
DiscordWebhookWindow.xaml.cs
139 lines (128 loc) · 5.21 KB
/
DiscordWebhookWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace ValheimServerWarden
{
/// <summary>
/// Interaction logic for DiscordWebhookWindow.xaml
/// </summary>
public partial class DiscordWebhookWindow : Window
{
private ValheimServer _server;
private List<TextBox> messageControls;
private TextBox clickedTextBox;
private MenuItem menuEditNames;
public DiscordWebhookWindow(ValheimServer server)
{
InitializeComponent();
WindowStartupLocation = WindowStartupLocation.CenterOwner;
_server = server;
txtWebhook.Text = _server.DiscordWebhook;
var contextMenu = new ContextMenu();
var defaultMenu = new MenuItem();
contextMenu.Items.Add(defaultMenu);
defaultMenu.Header = "Reset to default";
defaultMenu.Icon = FindResource("Cancel");
defaultMenu.Click += DefaultMenu_Click;
menuEditNames = new MenuItem();
contextMenu.Items.Add(menuEditNames);
menuEditNames.Header = "Edit random event names";
menuEditNames.Icon = FindResource("Edit");
menuEditNames.Click += MenuEditNames_Click;
messageControls = new();
messageControls.Add(txtOnPlayerConnected);
messageControls.Add(txtOnPlayerDisconnected);
messageControls.Add(txtOnPlayerDied);
messageControls.Add(txtOnFailedPassword);
messageControls.Add(txtOnRandomServerEvent);
messageControls.Add(txtOnStarted);
messageControls.Add(txtOnStartFailed);
messageControls.Add(txtOnServerExited);
messageControls.Add(txtOnUpdated);
foreach (var textBox in messageControls)
{
textBox.Text = server.GetWebhookMessage(textBox.Tag.ToString());
textBox.ContextMenu = contextMenu;
textBox.ContextMenuOpening += TextBox_ContextMenuOpening;
}
}
private void MenuEditNames_Click(object sender, RoutedEventArgs e)
{
var win = new DiscordWebhookEventNamesWindow(_server);
win.Owner = this;
win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
win.ShowDialog();
}
private void TextBox_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
clickedTextBox = (TextBox)sender;
if (clickedTextBox.Tag.ToString() == "OnRandomServerEvent")
{
menuEditNames.Visibility = Visibility.Visible;
}
else
{
menuEditNames.Visibility = Visibility.Collapsed;
}
}
private void DefaultMenu_Click(object sender, RoutedEventArgs e)
{
clickedTextBox.Text = ValheimServer.DiscordWebhookDefaultMessages[clickedTextBox.Tag.ToString()];
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
_server.DiscordWebhook = txtWebhook.Text;
foreach (var textBox in messageControls)
{
var webHookName = textBox.Tag.ToString();
if (textBox.Text != null && ValheimServer.DiscordWebhookDefaultMessages.ContainsKey(webHookName) && textBox.Text != ValheimServer.DiscordWebhookDefaultMessages[webHookName])
{
_server.DiscordWebhookMessages[webHookName] = textBox.Text;
}
else if (ValheimServer.DiscordWebhookDefaultMessages.ContainsKey(webHookName) && textBox.Text == ValheimServer.DiscordWebhookDefaultMessages[webHookName])
{
if (_server.DiscordWebhookMessages.ContainsKey(webHookName))
{
_server.DiscordWebhookMessages.Remove(webHookName);
}
}
}
DialogResult = true;
Close();
}
private void btnTestWebhook_Click(object sender, RoutedEventArgs e)
{
var oldUrl = _server.DiscordWebhook;
_server.DiscordWebhook = txtWebhook.Text;
var events = (ValheimServer.DiscordWebhookDefaultAttackNames.Keys).ToList<string>();
foreach (var cust in _server.DiscordServerEventNames.Keys)
{
if (!events.Contains(cust))
{
events.Add(cust);
}
}
_server.SendDiscordWebhook(((Button)sender).Tag.ToString(), new Player("Bjorn", "123456789101112"), events[(new Random()).Next(events.Count)]);
_server.DiscordWebhook = oldUrl;
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void menuServerEventNames_Click(object sender, RoutedEventArgs e)
{
}
}
}