Added a minimal GUI based on Avalonia UI library for C#

This commit is contained in:
2022-05-22 16:27:37 +03:00
parent 9f656d5f3f
commit 96b681bbda
24 changed files with 2099 additions and 21 deletions

View File

@@ -0,0 +1,89 @@
using Avalonia.Controls;
using System.Threading.Tasks;
using PluginManager.Others;
using System.IO;
using System;
using System.Diagnostics;
namespace DiscordBotGUI
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
LoadElements();
}
private void LoadElements()
{
button1.Click += async (sender, e) =>
{
string token = textBox1.Text;
string prefix = textBox2.Text;
string args = textBox3.Text;
if (!((token.Length == 70 || token.Length == 59) && prefix.Length == 1))
{
label4.Content = "Invalid Token or Prefix.\n(Prefix must be 1 character long and token must be 59 or 79 characters long)";
await Task.Delay(5000);
label4.Content = "";
return;
}
Functions.WriteToSettings(Functions.dataFolder + "DiscordBotCore.data", "BOT_TOKEN", token, '=');
Functions.WriteToSettings(Functions.dataFolder + "DiscordBotCore.data", "BOT_PREFIX", prefix, '=');
RunDiscordBot(args);
};
button2.Click += (sender, e) =>
{
Close();
};
string folder = $"{Functions.dataFolder}DiscordBotCore.data";
Directory.CreateDirectory(Functions.dataFolder);
try
{
string? botToken = Functions.readCodeFromFile(folder, "BOT_TOKEN", '=');
string? botPrefix = Functions.readCodeFromFile(folder, "BOT_PREFIX", '=');
if (botToken == null || botPrefix == null)
{
textBox1.IsReadOnly = false;
textBox2.IsReadOnly = false;
}
else
{
textBox1.Text = botToken;
textBox2.Text = botPrefix;
}
}
catch (Exception ex)
{
textBox1.IsReadOnly = false;
textBox2.IsReadOnly = false;
}
}
private void RunDiscordBot(string args)
{
var os = Functions.GetOperatingSystem();
if (os == PluginManager.Others.OperatingSystem.WINDOWS)
Process.Start("./DiscordBot.exe", args);
else if (os == PluginManager.Others.OperatingSystem.LINUX)
Process.Start("./DiscordBot", args);
else if (os == PluginManager.Others.OperatingSystem.MAC_OS)
Process.Start("./DiscordBot.app/Contents/MacOS/DiscordBot", args);
Close();
return;
}
}
}