This commit is contained in:
Binary file not shown.
@@ -7,6 +7,7 @@ using Discord;
|
|||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
using DiscordBot.Discord.Core;
|
using DiscordBot.Discord.Core;
|
||||||
|
using PluginManager;
|
||||||
using PluginManager.Interfaces;
|
using PluginManager.Interfaces;
|
||||||
using PluginManager.Others;
|
using PluginManager.Others;
|
||||||
using PluginManager.Others.Permissions;
|
using PluginManager.Others.Permissions;
|
||||||
@@ -58,10 +59,9 @@ namespace DiscordBot.Discord.Commands
|
|||||||
var channel = message.Channel;
|
var channel = message.Channel;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
string content = message.Content;
|
||||||
string content = message.Content;
|
string[] data = content.Split(' ');
|
||||||
string[] data = content.Split(' ');
|
string keyword = data[1];
|
||||||
string keyword = data[1];
|
|
||||||
if (keyword.ToLower() == "help")
|
if (keyword.ToLower() == "help")
|
||||||
{
|
{
|
||||||
await channel.SendMessageAsync("set token [new value] -- set the value of the new token (require restart)");
|
await channel.SendMessageAsync("set token [new value] -- set the value of the new token (require restart)");
|
||||||
@@ -78,7 +78,8 @@ namespace DiscordBot.Discord.Commands
|
|||||||
await channel.SendMessageAsync("Invalid token !");
|
await channel.SendMessageAsync("Invalid token !");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Functions.WriteToSettings("./Data/Resources/DiscordBotCore.data", "BOT_TOKEN", data[2], '\t');
|
|
||||||
|
Config.SetValue("token", data[2]);
|
||||||
break;
|
break;
|
||||||
case "prefix":
|
case "prefix":
|
||||||
if (data.Length != 3)
|
if (data.Length != 3)
|
||||||
@@ -86,7 +87,8 @@ namespace DiscordBot.Discord.Commands
|
|||||||
await channel.SendMessageAsync("Invalid token !");
|
await channel.SendMessageAsync("Invalid token !");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Functions.WriteToSettings("./Data/Resources/DiscordBotCore.data", "BOT_PREFIX", data[2], '\t');
|
|
||||||
|
Config.SetValue("token", data[2]);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
@@ -94,8 +96,9 @@ namespace DiscordBot.Discord.Commands
|
|||||||
|
|
||||||
await channel.SendMessageAsync("Restart required ...");
|
await channel.SendMessageAsync("Restart required ...");
|
||||||
}
|
}
|
||||||
catch
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
await channel.SendMessageAsync("Unknown usage to this command !\nUsage: " + Usage);
|
await channel.SendMessageAsync("Unknown usage to this command !\nUsage: " + Usage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ namespace DiscordBot.Discord.Core
|
|||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
Config.SaveDictionary();
|
Config.SaveConfig();
|
||||||
Thread.Sleep(10000);
|
Thread.Sleep(10000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,18 +28,16 @@ namespace DiscordBot
|
|||||||
Directory.CreateDirectory("./Data/Languages");
|
Directory.CreateDirectory("./Data/Languages");
|
||||||
Directory.CreateDirectory("./Data/Plugins/Commands");
|
Directory.CreateDirectory("./Data/Plugins/Commands");
|
||||||
Directory.CreateDirectory("./Data/Plugins/Events");
|
Directory.CreateDirectory("./Data/Plugins/Events");
|
||||||
if (File.Exists(Functions.dataFolder + "var.dat"))
|
Config.LoadConfig().Wait();
|
||||||
Config.LoadDictionary();
|
if (!Config.ContainsKey("token") || Config.GetValue("token") == null || Config.GetValue("token")?.Length != 70)
|
||||||
else if (Config.GetValue("token") == null || Config.GetValue("token")?.Length != 70)
|
|
||||||
{
|
{
|
||||||
Dictionary<string, string> d = new Dictionary<string, string>();
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Please insert your token");
|
Console.WriteLine("Please insert your token");
|
||||||
Console.Write("Token = ");
|
Console.Write("Token = ");
|
||||||
string token = Console.ReadLine();
|
string token = Console.ReadLine();
|
||||||
if (token?.Length == 59 || token?.Length == 70)
|
if (token?.Length == 59 || token?.Length == 70)
|
||||||
d.Add("token", token);
|
Config.AddValueToVariables("token", token, true);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Console.WriteLine("Invalid token");
|
Console.WriteLine("Invalid token");
|
||||||
@@ -53,13 +51,12 @@ namespace DiscordBot
|
|||||||
|
|
||||||
if (prefix == ' ' || char.IsDigit(prefix)) continue;
|
if (prefix == ' ' || char.IsDigit(prefix)) continue;
|
||||||
|
|
||||||
d.Add("prefix", prefix.ToString());
|
Config.AddValueToVariables("prefix", prefix.ToString(), false);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
Config.AppendToDictionary(d);
|
Config.SaveConfig();
|
||||||
d.Clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
HandleInput(args).Wait();
|
HandleInput(args).Wait();
|
||||||
@@ -84,7 +81,7 @@ namespace DiscordBot
|
|||||||
if (loadPluginsOnStartup) consoleCommandsHandler.HandleCommand("lp");
|
if (loadPluginsOnStartup) consoleCommandsHandler.HandleCommand("lp");
|
||||||
if (listPluginsAtStartup) consoleCommandsHandler.HandleCommand("listplugs");
|
if (listPluginsAtStartup) consoleCommandsHandler.HandleCommand("listplugs");
|
||||||
if (listLanguagAtStartup) consoleCommandsHandler.HandleCommand("listlang");
|
if (listLanguagAtStartup) consoleCommandsHandler.HandleCommand("listlang");
|
||||||
Config.SaveDictionary();
|
Config.SaveConfig();
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
Console.ForegroundColor = ConsoleColor.White;
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ namespace EVE_LevelingSystem
|
|||||||
public async void Start(DiscordSocketClient client)
|
public async void Start(DiscordSocketClient client)
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory("./Data/Resources/LevelingSystem");
|
Directory.CreateDirectory("./Data/Resources/LevelingSystem");
|
||||||
Config.AddValueToVariables("LevelingSystemPath", "./Data/Resources/LevelingSystem");
|
Config.AddValueToVariables("LevelingSystemPath", "./Data/Resources/LevelingSystem", true);
|
||||||
Config.AddValueToVariables("LevelingSystemSettingsFile", "./Data/Resources/LevelingSystemSettings.txt");
|
Config.AddValueToVariables("LevelingSystemSettingsFile", "./Data/Resources/LevelingSystemSettings.txt", true);
|
||||||
|
|
||||||
if (!File.Exists(Config.GetValue("LevelingSystemSettingsFile")))
|
if (!File.Exists(Config.GetValue("LevelingSystemSettingsFile")))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,86 +1,76 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using PluginManager.Others;
|
using PluginManager.Others;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace PluginManager
|
namespace PluginManager
|
||||||
{
|
{
|
||||||
|
internal class AppConfig
|
||||||
|
{
|
||||||
|
public Dictionary<string, string> ApplicationVariables { get; set; }
|
||||||
|
public List<string> ProtectedKeyWords { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
public static class Config
|
public static class Config
|
||||||
{
|
{
|
||||||
private static readonly Dictionary<string, string> ApplicationVariables = new();
|
private static AppConfig appConfig = null;
|
||||||
private static readonly List<string> ConstantTokens = new() { "token" };
|
|
||||||
|
|
||||||
public static void AppendToDictionary(Dictionary<string, string> dictionary)
|
public static bool AddValueToVariables(string key, string value, bool isReadOnly)
|
||||||
{
|
{
|
||||||
foreach (var kvp in dictionary) ApplicationVariables.TryAdd(kvp.Key, kvp.Value);
|
if (appConfig.ApplicationVariables.ContainsKey(key)) return false;
|
||||||
}
|
appConfig.ApplicationVariables.Add(key, value);
|
||||||
|
if (isReadOnly) appConfig.ProtectedKeyWords.Add(key);
|
||||||
public static bool AddValueToVariables(string key, string value, bool constant)
|
SaveConfig();
|
||||||
{
|
|
||||||
bool req = AddValueToVariables(key, value);
|
|
||||||
if (constant) ConstantTokens.Add(key);
|
|
||||||
|
|
||||||
return req;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool AddValueToVariables(string key, string value)
|
|
||||||
{
|
|
||||||
if (ApplicationVariables.ContainsKey(key))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
ApplicationVariables.Add(key, value);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string? GetValue(string key)
|
public static string? GetValue(string key)
|
||||||
{
|
{
|
||||||
if (!ApplicationVariables.ContainsKey(key))
|
if (!appConfig.ApplicationVariables.ContainsKey(key)) return null;
|
||||||
{
|
return appConfig.ApplicationVariables[key];
|
||||||
if (key != "token") Console.WriteLine("The key is not present in the dictionary");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ApplicationVariables[key];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool SetValue(string key, string value)
|
public static bool SetValue(string key, string value)
|
||||||
{
|
{
|
||||||
if (!ApplicationVariables.ContainsKey(key)) return false;
|
if (!appConfig.ApplicationVariables.ContainsKey(key)) return false;
|
||||||
if (ConstantTokens.Contains(key)) return false;
|
if (appConfig.ProtectedKeyWords.Contains(key)) return false;
|
||||||
ApplicationVariables[key] = value;
|
appConfig.ApplicationVariables[key] = value;
|
||||||
|
SaveConfig();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool RemoveKey(string key)
|
public static bool RemoveKey(string key)
|
||||||
{
|
{
|
||||||
if (ConstantTokens.Contains(key)) return false;
|
appConfig.ApplicationVariables.Remove(key);
|
||||||
|
appConfig.ProtectedKeyWords.Remove(key);
|
||||||
|
|
||||||
ApplicationVariables.Remove(key);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async void SaveDictionary()
|
public static async void SaveConfig()
|
||||||
{
|
{
|
||||||
string path = Functions.dataFolder + "var.dat";
|
string path = Functions.dataFolder + "var.dat";
|
||||||
await Functions.SaveToJsonFile(path, ApplicationVariables);
|
await Functions.SaveToJsonFile<AppConfig>(path, appConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async void LoadDictionary()
|
public static async Task LoadConfig()
|
||||||
{
|
{
|
||||||
string path = Functions.dataFolder + "var.dat";
|
string path = Functions.dataFolder + "var.dat";
|
||||||
var d = await Functions.ConvertFromJson<Dictionary<string, string>>(path);
|
if (File.Exists(path))
|
||||||
ApplicationVariables.Clear();
|
{
|
||||||
AppendToDictionary(d);
|
appConfig = await Functions.ConvertFromJson<AppConfig>(path);
|
||||||
|
Functions.WriteLogFile($"Loaded {appConfig.ApplicationVariables.Keys.Count} application variables.\nLoaded {appConfig.ProtectedKeyWords.Count} readonly variables.");
|
||||||
|
//Console.WriteLine($"Loaded {appConfig.ApplicationVariables.Count} application variables !");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
appConfig = new() { ApplicationVariables = new Dictionary<string, string>(), ProtectedKeyWords = new List<string>() };
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string GetKey(string value) => ApplicationVariables.Keys.FirstOrDefault(x => ApplicationVariables[x] == value);
|
public static string? GetKey(string value) => appConfig.ApplicationVariables.Keys.FirstOrDefault(x => appConfig.ApplicationVariables[x] == value);
|
||||||
public static bool ContainsValue(string value) => ApplicationVariables.ContainsValue(value);
|
public static bool ContainsValue(string value) => appConfig.ApplicationVariables.ContainsValue(value);
|
||||||
public static bool ContainsKey(string key) => ApplicationVariables.ContainsKey(key);
|
public static bool ContainsKey(string key) => appConfig.ApplicationVariables.ContainsKey(key);
|
||||||
|
|
||||||
|
public static Dictionary<string, string> GetAllVariables() => new Dictionary<string, string>(appConfig.ApplicationVariables);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -203,12 +203,12 @@ namespace PluginManager.Items
|
|||||||
if (args.Length < 3) return;
|
if (args.Length < 3) return;
|
||||||
string in1 = args[1];
|
string in1 = args[1];
|
||||||
if (!Config.ContainsKey(in1))
|
if (!Config.ContainsKey(in1))
|
||||||
Config.AddValueToVariables(in1, Functions.MergeStrings(args, 2));
|
Config.AddValueToVariables(in1, Functions.MergeStrings(args, 2), false);
|
||||||
else
|
else
|
||||||
Config.SetValue(in1, Functions.MergeStrings(args, 2));
|
Config.SetValue(in1, Functions.MergeStrings(args, 2));
|
||||||
|
|
||||||
Console.WriteLine($"Updated config file with the following command: {in1} => {Config.GetValue(in1)}");
|
Console.WriteLine($"Updated config file with the following command: {in1} => {Config.GetValue(in1)}");
|
||||||
Config.SaveDictionary();
|
Config.SaveConfig();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -216,7 +216,20 @@ namespace PluginManager.Items
|
|||||||
{
|
{
|
||||||
if (args.Length < 2) return;
|
if (args.Length < 2) return;
|
||||||
Config.RemoveKey(args[1]);
|
Config.RemoveKey(args[1]);
|
||||||
Config.SaveDictionary();
|
Config.SaveConfig();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
AddCommand("vars", "Display all variables", () =>
|
||||||
|
{
|
||||||
|
var d = Config.GetAllVariables();
|
||||||
|
List<string[]> data = new List<string[]>();
|
||||||
|
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 });
|
||||||
|
data.Add(new string[] { "-", "-" });
|
||||||
|
Console_Utilities.FormatAndAlignTable(data);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -224,7 +237,7 @@ namespace PluginManager.Items
|
|||||||
{
|
{
|
||||||
await client.StopAsync();
|
await client.StopAsync();
|
||||||
await client.DisposeAsync();
|
await client.DisposeAsync();
|
||||||
Config.SaveDictionary();
|
Config.SaveConfig();
|
||||||
Environment.Exit(0);
|
Environment.Exit(0);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -71,9 +71,9 @@ namespace PluginManager.Others
|
|||||||
/// <param name="data">The List of arrays of strings that represent the rows.</param>
|
/// <param name="data">The List of arrays of strings that represent the rows.</param>
|
||||||
public static void FormatAndAlignTable(List<string[]> data)
|
public static void FormatAndAlignTable(List<string[]> data)
|
||||||
{
|
{
|
||||||
char tableLine = '-';
|
char tableLine = '-';
|
||||||
char tableCross = '+';
|
char tableCross = '+';
|
||||||
char tableWall = '|';
|
char tableWall = '|';
|
||||||
|
|
||||||
int[] len = new int[data[0].Length];
|
int[] len = new int[data[0].Length];
|
||||||
foreach (var line in data)
|
foreach (var line in data)
|
||||||
|
|||||||
@@ -97,31 +97,6 @@ namespace PluginManager.Others
|
|||||||
File.AppendAllText(errPath, ErrMessage + " \n");
|
File.AppendAllText(errPath, ErrMessage + " \n");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Write to settings file
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="file">The settings file path</param>
|
|
||||||
/// <param name="Code">The Key value of the setting</param>
|
|
||||||
/// <param name="newValue">The new value of the settings</param>
|
|
||||||
/// <param name="separator">The separator between the key and the value</param>
|
|
||||||
public static void WriteToSettings(string file, string Code, string newValue, char separator)
|
|
||||||
{
|
|
||||||
|
|
||||||
string[] lines = File.ReadAllLines(file);
|
|
||||||
File.Delete(file);
|
|
||||||
bool ok = false;
|
|
||||||
foreach (var line in lines)
|
|
||||||
if (line.StartsWith(Code))
|
|
||||||
{
|
|
||||||
File.AppendAllText(file, Code + separator + newValue + "\n");
|
|
||||||
ok = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
File.AppendAllText(file, line + "\n");
|
|
||||||
|
|
||||||
if (!ok) File.AppendAllText(file, Code + separator + newValue + "\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Merge one array of strings into one string
|
/// Merge one array of strings into one string
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -161,29 +136,6 @@ namespace PluginManager.Others
|
|||||||
return command.Arguments;
|
return command.Arguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Write setting
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="SettingName">The full path to the setting</param>
|
|
||||||
/// <param name="NewValue">The new Value</param>
|
|
||||||
public static void WriteToSettingsFast(string SettingName, string NewValue)
|
|
||||||
{
|
|
||||||
|
|
||||||
string path = dataFolder; // Resources/
|
|
||||||
|
|
||||||
string[] args = SettingName.Split('.');
|
|
||||||
|
|
||||||
int len = args.Length;
|
|
||||||
if (len < 2) return;
|
|
||||||
for (int i = 0; i < len - 2; i++) path += args[i] + "/";
|
|
||||||
path += args[len - 2] + ".txt";
|
|
||||||
|
|
||||||
|
|
||||||
WriteToSettings(path, args[len - 1].Replace('_', ' '), NewValue, '=');
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Copy one Stream to another <see langword="async"/>
|
/// Copy one Stream to another <see langword="async"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -253,6 +205,11 @@ namespace PluginManager.Others
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Convert Bytes to highest measurement unit possible
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="bytes">The amount of bytes</param>
|
||||||
|
/// <returns></returns>
|
||||||
public static (double, string) ConvertBytes(long bytes)
|
public static (double, string) ConvertBytes(long bytes)
|
||||||
{
|
{
|
||||||
if (bytes < 1024) return (bytes, "B");
|
if (bytes < 1024) return (bytes, "B");
|
||||||
@@ -262,12 +219,25 @@ namespace PluginManager.Others
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Save to JSON file
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The class type</typeparam>
|
||||||
|
/// <param name="file">The file path</param>
|
||||||
|
/// <param name="Data">The values</param>
|
||||||
|
/// <returns></returns>
|
||||||
public static async Task SaveToJsonFile<T>(string file, T Data)
|
public static async Task SaveToJsonFile<T>(string file, T Data)
|
||||||
{
|
{
|
||||||
string jsonText = JsonSerializer.Serialize(Data, typeof(T), new JsonSerializerOptions() { WriteIndented = true });
|
string jsonText = JsonSerializer.Serialize(Data, typeof(T), new JsonSerializerOptions { WriteIndented = true });
|
||||||
await File.WriteAllTextAsync(file, jsonText);
|
await File.WriteAllTextAsync(file, jsonText);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Convert json text or file to some kind of data
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The data type</typeparam>
|
||||||
|
/// <param name="input">The file or json text</param>
|
||||||
|
/// <returns></returns>
|
||||||
public static async Task<T> ConvertFromJson<T>(string input)
|
public static async Task<T> ConvertFromJson<T>(string input)
|
||||||
{
|
{
|
||||||
Stream text;
|
Stream text;
|
||||||
|
|||||||
Reference in New Issue
Block a user