This commit is contained in:
2022-06-07 11:13:03 +03:00
parent 195c082cd7
commit c66ff52d94
8 changed files with 131 additions and 31 deletions

View File

@@ -3,7 +3,6 @@ using PluginManager;
using PluginManager.Items;
using PluginManager.Others;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
@@ -29,7 +28,14 @@ namespace DiscordBot
Directory.CreateDirectory("./Data/Plugins/Commands");
Directory.CreateDirectory("./Data/Plugins/Events");
Config.LoadConfig().Wait();
if (!Config.ContainsKey("token") || Config.GetValue("token") == null || Config.GetValue("token")?.Length != 70)
if (Config.ContainsKey("DeleteLogsAtStartup"))
if (Config.GetValue<bool>("DeleteLogsAtStartup"))
foreach (string file in Directory.GetFiles("./Output/Logs/"))
File.Delete(file);
if (!Config.ContainsKey("token") || Config.GetValue<string>("token") == null || Config.GetValue<string>("token")?.Length != 70)
{
while (true)
{
@@ -101,8 +107,8 @@ namespace DiscordBot
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("============================ Discord BOT - Cross Platform ============================");
string token = Config.GetValue("token");
string prefix = Config.GetValue("prefix");
string token = Config.GetValue<string>("token");
string prefix = Config.GetValue<string>("prefix");
var discordbooter = new Boot(token, prefix);
await discordbooter.Awake();

View File

@@ -16,6 +16,7 @@ namespace DiscordBotGUI
public AppUpdater()
{
InitializeComponent();
Config.LoadConfig().Wait();
if (!File.Exists("./Version.txt"))
{
File.WriteAllText("./Version.txt", "DiscordBotVersion=0");

View File

@@ -14,7 +14,8 @@
<MenuItem Header="Plugins">
<MenuItem Header="Commands" x:Class="DiscordBotGUI.MainWindow" x:Name="commandsSettingMenuItem" />
<MenuItem Header="Events" x:Class="DiscordBotGUI.MainWindow" x:Name="eventsSettingMenuItem" />
</MenuItem>
</MenuItem>
<MenuItem Header="Application Variables" x:Class="DiscordBotGUI.MainWindow" x:Name="applicationVariablesMenuItem"/>
</Menu>
<Label x:Class="DiscordBotGUI.MainWindow" x:Name="label1" Content="Discord Token" />
<TextBox x:Class="DiscordBotGUI.MainWindow" x:Name="textBox1" IsReadOnly="True" TextAlignment="Center" Text=""/>

View File

@@ -23,8 +23,6 @@ namespace DiscordBotGUI
private void LoadElements()
{
textBox3.Watermark = "Insert start arguments";
if (File.Exists("./Version.txt")) label5.Content = Config.GetValue("Version");
button1.Click += async (sender, e) =>
@@ -48,9 +46,9 @@ namespace DiscordBotGUI
};
commandsSettingMenuItem.Click += (sender, e) => new Commands() /*{ Height = 200, Width = 550 }*/.ShowDialog(this);
eventsSettingMenuItem.Click += (sender, e) => new Events() /*{ Height = 200, Width = 550 }*/.ShowDialog(this);
commandsSettingMenuItem.Click += (sender, e) => new Commands() /*{ Height = 200, Width = 550 }*/.ShowDialog(this);
eventsSettingMenuItem.Click += (sender, e) => new Events() /*{ Height = 200, Width = 550 }*/.ShowDialog(this);
applicationVariablesMenuItem.Click += (sender, e) => new ApplicationVariables().ShowDialog(this);
string folder = $"{Functions.dataFolder}DiscordBotCore.data";
Directory.CreateDirectory(Functions.dataFolder);

View File

@@ -0,0 +1,23 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="DiscordBotGUI.Settings.ApplicationVariables"
Title="ApplicationVariables">
<StackPanel>
<Label Content="Available commands"/>
<TextBox x:Name="textBox1" x:Class="DiscordBotGUI.Settings.ApplicationVariables"/>
<Label Content="Insert a new variable"/>
<Border Background="White" BorderThickness="3" Margin="10"/>
<Label Content="New Variable Name"/>
<TextBox x:Name="textBox2" x:Class="DiscordBotGUI.Settings.ApplicationVariables"/>
<Label Content="New Variable Value"/>
<TextBox x:Name="textBox3" x:Class="DiscordBotGUI.Settings.ApplicationVariables"/>
<Label Content="New Variable IsReadOnly"/>
<CheckBox x:Class="DiscordBotGUI.Settings.ApplicationVariables" x:Name="checkBox1"/>
<Button Content="Add command" x:Class="DiscordBotGUI.Settings.ApplicationVariables" x:Name="button1"/>
</StackPanel>
</Window>

View File

@@ -0,0 +1,44 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using PluginManager;
namespace DiscordBotGUI.Settings
{
public partial class ApplicationVariables : Window
{
public ApplicationVariables()
{
InitializeComponent();
Load();
}
private void Load()
{
ClearEverything();
button1.Click += (sedner, e) =>
{
string key = textBox2.Text;
if (Config.ContainsKey(key))
{
ClearEverything();
return;
}
string value = textBox3.Text;
Config.AddValueToVariables(key, value, checkBox1.IsChecked!.Value);
ClearEverything();
};
}
private void ClearEverything()
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
checkBox1.IsChecked = false;
var allvars = Config.GetAllVariables();
foreach (var kvp in allvars) textBox1.Text += kvp.Key + " => " + kvp.Value + "\n";
}
}
}

View File

@@ -3,13 +3,14 @@ using PluginManager.Others;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
namespace PluginManager
{
internal class AppConfig
{
public Dictionary<string, string> ApplicationVariables { get; set; }
public Dictionary<string, object> ApplicationVariables { get; set; }
public List<string> ProtectedKeyWords { get; set; }
}
@@ -17,26 +18,30 @@ namespace PluginManager
{
private static AppConfig appConfig = null;
public static bool AddValueToVariables(string key, string value, bool isProtected)
public static bool AddValueToVariables<T>(string key, T value, bool isProtected)
{
if (appConfig.ApplicationVariables.ContainsKey(key)) return false;
if (value == null) return false;
appConfig.ApplicationVariables.Add(key, value);
if (isProtected) appConfig.ProtectedKeyWords.Add(key);
SaveConfig();
return true;
}
public static string? GetValue(string key)
public static T? GetValue<T>(string key)
{
if (!appConfig.ApplicationVariables.ContainsKey(key)) return null;
return appConfig.ApplicationVariables[key];
if (!appConfig.ApplicationVariables.ContainsKey(key)) return default;
JsonElement element = (JsonElement)appConfig.ApplicationVariables[key];
return element.Deserialize<T>();
}
public static bool SetValue(string key, string value)
public static bool SetValue<T>(string key, T value)
{
if (!appConfig.ApplicationVariables.ContainsKey(key)) return false;
if (appConfig.ProtectedKeyWords.Contains(key)) return false;
appConfig.ApplicationVariables[key] = value;
if (value == null) return false;
appConfig.ApplicationVariables[key] = JsonSerializer.SerializeToElement(value);
SaveConfig();
return true;
}
@@ -45,6 +50,7 @@ namespace PluginManager
{
appConfig.ApplicationVariables.Remove(key);
appConfig.ProtectedKeyWords.Remove(key);
SaveConfig();
return true;
}
@@ -63,13 +69,13 @@ namespace PluginManager
Functions.WriteLogFile($"Loaded {appConfig.ApplicationVariables.Keys.Count} application variables.\nLoaded {appConfig.ProtectedKeyWords.Count} readonly variables.");
}
else
appConfig = new() { ApplicationVariables = new Dictionary<string, string>(), ProtectedKeyWords = new List<string>() };
appConfig = new() { ApplicationVariables = new Dictionary<string, object>(), ProtectedKeyWords = new List<string>() };
}
public static string? GetKey(string value) => appConfig.ApplicationVariables.Keys.FirstOrDefault(x => appConfig.ApplicationVariables[x] == value);
public static bool ContainsValue(string value) => appConfig.ApplicationVariables.ContainsValue(value);
public static bool ContainsKey(string key) => appConfig.ApplicationVariables.ContainsKey(key);
public static Dictionary<string, string> GetAllVariables() => new Dictionary<string, string>(appConfig.ApplicationVariables);
public static Dictionary<string, object> GetAllVariables() => new(appConfig.ApplicationVariables);
}
}

View File

@@ -9,6 +9,7 @@ using System.Threading.Tasks;
using System.Threading;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using Newtonsoft.Json.Converters;
namespace PluginManager.Items
{
@@ -193,22 +194,43 @@ namespace PluginManager.Items
if (args.Length != 2) return;
if (!Config.ContainsKey(args[1])) return;
string data = Config.GetValue(args[1]);
string data = Config.GetValue<string>(args[1]);
Console.WriteLine($"{args[1]} => {data}");
}
);
AddCommand("addv", "add variable to the system variables", async (args) =>
AddCommand("add", "add variable to the system variables\nadd [key] [value] [isReadOnly=true/false]", async (args) =>
{
if (args.Length < 3) return;
string in1 = args[1];
if (!Config.ContainsKey(in1))
Config.AddValueToVariables(in1, Functions.MergeStrings(args, 2), false);
else
Config.SetValue(in1, Functions.MergeStrings(args, 2));
if (args.Length < 4) return;
string key = args[1];
string value = args[2];
bool isReadOnly = args[3].Equals("true", StringComparison.CurrentCultureIgnoreCase);
Console.WriteLine($"Updated config file with the following command: {in1} => {Config.GetValue(in1)}");
Config.SaveConfig();
try
{
if (Config.ContainsKey(key)) return;
if (int.TryParse(value, out int intValue))
Config.AddValueToVariables(key, intValue, isReadOnly);
else if (bool.TryParse(value, out bool boolValue))
Config.AddValueToVariables(key, boolValue, isReadOnly);
else if (float.TryParse(value, out float floatValue))
Config.AddValueToVariables(key, floatValue, isReadOnly);
else if (double.TryParse(value, out double doubleValue))
Config.AddValueToVariables(key, doubleValue, isReadOnly);
else if (uint.TryParse(value, out uint uintValue))
Config.AddValueToVariables(key, uintValue, isReadOnly);
else if (long.TryParse(value, out long longValue))
Config.AddValueToVariables(key, longValue, isReadOnly);
else if (byte.TryParse(value, out byte byteValue))
Config.AddValueToVariables(key, byteValue, isReadOnly);
else
Config.AddValueToVariables(key, value, isReadOnly);
Console.WriteLine($"Updated config file with the following command: {args[1]} => {value}");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
);
@@ -216,7 +238,6 @@ namespace PluginManager.Items
{
if (args.Length < 2) return;
Config.RemoveKey(args[1]);
Config.SaveConfig();
}
);
@@ -227,7 +248,7 @@ namespace PluginManager.Items
data.Add(new string[] { "-", "-" });
data.Add(new string[] { "Key", "Value" });
data.Add(new string[] { "-", "-" });
foreach (var kvp in d) data.Add(new string[] { kvp.Key, kvp.Value });
foreach (var kvp in d) data.Add(new string[] { kvp.Key, kvp.Value.ToString() });
data.Add(new string[] { "-", "-" });
Console_Utilities.FormatAndAlignTable(data);
}