Fixed some text and added some missing texts to commands. Added new command to clear screen and formated code.
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
using System.Net.Mime;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using PluginManager.Others;
|
||||
|
||||
namespace PluginManager.Bot;
|
||||
|
||||
@@ -44,35 +42,37 @@ public class Boot
|
||||
public Boot(string botToken, string botPrefix)
|
||||
{
|
||||
this.botPrefix = botPrefix;
|
||||
this.botToken = botToken;
|
||||
this.botToken = botToken;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the bot is ready
|
||||
/// </summary>
|
||||
/// <value> true if the bot is ready, othwerwise false </value>
|
||||
/// <value> true if the bot is ready, otherwise false </value>
|
||||
public bool isReady { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The start method for the bot. This method is used to load the bot
|
||||
/// </summary>
|
||||
/// <param name="config">The discord socket config. If null then the default one will be applied (AlwaysDownloadUsers=true, UseInteractionSnowflakeDate=false, GatewayIntents=GatewayIntents.All)</param>
|
||||
/// <param name="config">
|
||||
/// The discord socket config. If null then the default one will be applied (AlwaysDownloadUsers=true,
|
||||
/// UseInteractionSnowflakeDate=false, GatewayIntents=GatewayIntents.All)
|
||||
/// </param>
|
||||
/// <returns>Task</returns>
|
||||
public async Task Awake(DiscordSocketConfig? config = null)
|
||||
{
|
||||
if (config is null)
|
||||
config = new DiscordSocketConfig
|
||||
{
|
||||
|
||||
AlwaysDownloadUsers = true,
|
||||
|
||||
//Disable system clock checkup (for responses at slash commands)
|
||||
UseInteractionSnowflakeDate = false,
|
||||
GatewayIntents = GatewayIntents.All
|
||||
GatewayIntents = GatewayIntents.All
|
||||
};
|
||||
|
||||
client = new DiscordSocketClient(config);
|
||||
client = new DiscordSocketClient(config);
|
||||
service = new CommandService();
|
||||
|
||||
CommonTasks();
|
||||
@@ -86,7 +86,6 @@ public class Boot
|
||||
await commandServiceHandler.InstallCommandsAsync();
|
||||
|
||||
|
||||
|
||||
await Task.Delay(2000);
|
||||
|
||||
Config._DiscordBotClient = this;
|
||||
@@ -97,10 +96,10 @@ public class Boot
|
||||
private void CommonTasks()
|
||||
{
|
||||
if (client == null) return;
|
||||
client.LoggedOut += Client_LoggedOut;
|
||||
client.Log += Log;
|
||||
client.LoggedIn += LoggedIn;
|
||||
client.Ready += Ready;
|
||||
client.LoggedOut += Client_LoggedOut;
|
||||
client.Log += Log;
|
||||
client.LoggedIn += LoggedIn;
|
||||
client.Ready += Ready;
|
||||
client.Disconnected += Client_Disconnected;
|
||||
}
|
||||
|
||||
@@ -109,7 +108,8 @@ public class Boot
|
||||
if (arg.Message.Contains("401"))
|
||||
{
|
||||
Config.Data.Remove("token");
|
||||
Config.Logger.Log("The token is invalid. Please restart the bot and enter a valid token.", this, Others.LogLevel.ERROR);
|
||||
Config.Logger.Log("The token is invalid. Please restart the bot and enter a valid token.", this,
|
||||
LogLevel.ERROR);
|
||||
Config.Data.Save();
|
||||
await Task.Delay(4000);
|
||||
Environment.Exit(0);
|
||||
@@ -140,7 +140,7 @@ public class Boot
|
||||
{
|
||||
case LogSeverity.Error:
|
||||
case LogSeverity.Critical:
|
||||
Config.Logger.Log(message.Message, this, Others.LogLevel.ERROR);
|
||||
Config.Logger.Log(message.Message, this, LogLevel.ERROR);
|
||||
|
||||
break;
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using PluginManager.Interfaces;
|
||||
@@ -14,9 +13,9 @@ namespace PluginManager.Bot;
|
||||
|
||||
internal class CommandHandler
|
||||
{
|
||||
private readonly string botPrefix;
|
||||
private readonly string botPrefix;
|
||||
private readonly DiscordSocketClient client;
|
||||
private readonly CommandService commandService;
|
||||
private readonly CommandService commandService;
|
||||
|
||||
/// <summary>
|
||||
/// Command handler constructor
|
||||
@@ -26,10 +25,9 @@ internal class CommandHandler
|
||||
/// <param name="botPrefix">The prefix to watch for</param>
|
||||
public CommandHandler(DiscordSocketClient client, CommandService commandService, string botPrefix)
|
||||
{
|
||||
this.client = client;
|
||||
this.client = client;
|
||||
this.commandService = commandService;
|
||||
this.botPrefix = botPrefix;
|
||||
|
||||
this.botPrefix = botPrefix;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -38,7 +36,7 @@ internal class CommandHandler
|
||||
/// <returns></returns>
|
||||
public async Task InstallCommandsAsync()
|
||||
{
|
||||
client.MessageReceived += MessageHandler;
|
||||
client.MessageReceived += MessageHandler;
|
||||
client.SlashCommandExecuted += Client_SlashCommandExecuted;
|
||||
await commandService.AddModulesAsync(Assembly.GetEntryAssembly(), null);
|
||||
}
|
||||
@@ -63,7 +61,6 @@ internal class CommandHandler
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -73,10 +70,9 @@ internal class CommandHandler
|
||||
/// <returns></returns>
|
||||
private async Task MessageHandler(SocketMessage Message)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
if (Message.Author.IsBot)
|
||||
if (Message.Author.IsBot)
|
||||
return;
|
||||
|
||||
if (Message as SocketUserMessage == null)
|
||||
@@ -97,18 +93,22 @@ internal class CommandHandler
|
||||
await commandService.ExecuteAsync(context, argPos, null);
|
||||
|
||||
DBCommand? plugin;
|
||||
string cleanMessage = "";
|
||||
var cleanMessage = "";
|
||||
|
||||
if (message.HasMentionPrefix(client.CurrentUser, ref argPos))
|
||||
{
|
||||
string mentionPrefix = "<@" + client.CurrentUser.Id + ">";
|
||||
var mentionPrefix = "<@" + client.CurrentUser.Id + ">";
|
||||
|
||||
plugin = PluginLoader.Commands!
|
||||
.FirstOrDefault(plug => plug.Command == message.Content.Substring(mentionPrefix.Length+1).Split(' ')[0] ||
|
||||
(
|
||||
plug.Aliases is not null &&
|
||||
plug.Aliases.Contains(message.CleanContent.Substring(mentionPrefix.Length+1).Split(' ')[0])
|
||||
));
|
||||
.FirstOrDefault(plug => plug.Command ==
|
||||
message.Content.Substring(mentionPrefix.Length + 1)
|
||||
.Split(' ')[0] ||
|
||||
(
|
||||
plug.Aliases is not null &&
|
||||
plug.Aliases.Contains(message.CleanContent
|
||||
.Substring(mentionPrefix.Length + 1)
|
||||
.Split(' ')[0])
|
||||
));
|
||||
|
||||
cleanMessage = message.Content.Substring(mentionPrefix.Length + 1);
|
||||
}
|
||||
@@ -116,23 +116,27 @@ internal class CommandHandler
|
||||
else
|
||||
{
|
||||
plugin = PluginLoader.Commands!
|
||||
.FirstOrDefault(p => p.Command == message.Content.Split(' ')[0].Substring(botPrefix.Length) ||
|
||||
(p.Aliases is not null &&
|
||||
p.Aliases.Contains(
|
||||
message.Content.Split(' ')[0].Substring(botPrefix.Length))));
|
||||
.FirstOrDefault(p => p.Command ==
|
||||
message.Content.Split(' ')[0].Substring(botPrefix.Length) ||
|
||||
(p.Aliases is not null &&
|
||||
p.Aliases.Contains(
|
||||
message.Content.Split(' ')[0]
|
||||
.Substring(botPrefix.Length))));
|
||||
cleanMessage = message.Content.Substring(botPrefix.Length);
|
||||
}
|
||||
if (plugin is null)
|
||||
throw new Exception($"Failed to run command ! " + message.CleanContent + " (user: " + context.Message.Author.Username + " - " + context.Message.Author.Id + ")");
|
||||
|
||||
if (plugin is null)
|
||||
throw new Exception("Failed to run command ! " + message.CleanContent + " (user: " +
|
||||
context.Message.Author.Username + " - " + context.Message.Author.Id + ")");
|
||||
|
||||
if (plugin.requireAdmin && !context.Message.Author.isAdmin())
|
||||
return;
|
||||
|
||||
string[] split = cleanMessage.Split(' ');
|
||||
var split = cleanMessage.Split(' ');
|
||||
|
||||
string[]? argsClean = null;
|
||||
if(split.Length > 1)
|
||||
argsClean = string.Join(' ', split, 1, split.Length-1).Split(' ');
|
||||
if (split.Length > 1)
|
||||
argsClean = string.Join(' ', split, 1, split.Length - 1).Split(' ');
|
||||
|
||||
DBCommandExecutingArguments cmd = new(context, cleanMessage, split[0], argsClean);
|
||||
|
||||
|
||||
@@ -1,27 +1,24 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
|
||||
using System.Collections.Generic;
|
||||
using PluginManager.Others;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using PluginManager.Bot;
|
||||
using PluginManager.Others;
|
||||
using PluginManager.Others.Logger;
|
||||
|
||||
namespace PluginManager;
|
||||
|
||||
public class Config
|
||||
{
|
||||
private static bool IsLoaded = false;
|
||||
public static DBLogger Logger;
|
||||
public static Json<string, string> Data;
|
||||
public static Json<string, string> Plugins;
|
||||
private static bool IsLoaded;
|
||||
public static DBLogger Logger;
|
||||
public static Json<string, string>? Data;
|
||||
public static Json<string, string>? Plugins;
|
||||
|
||||
internal static Bot.Boot? _DiscordBotClient;
|
||||
internal static Boot? _DiscordBotClient;
|
||||
|
||||
public static Bot.Boot? DiscordBot
|
||||
{
|
||||
get => _DiscordBotClient;
|
||||
}
|
||||
public static Boot? DiscordBot => _DiscordBotClient;
|
||||
|
||||
public static async Task Initialize()
|
||||
{
|
||||
@@ -34,14 +31,14 @@ public class Config
|
||||
Directory.CreateDirectory("./Data/Logs/Logs");
|
||||
Directory.CreateDirectory("./Data/Logs/Errors");
|
||||
|
||||
Data = new Json<string, string>("./Data/Resources/config.json");
|
||||
Data = new Json<string, string>("./Data/Resources/config.json");
|
||||
Plugins = new Json<string, string>("./Data/Resources/Plugins.json");
|
||||
|
||||
Config.Data["LogFolder"] = "./Data/Logs/Logs";
|
||||
Config.Data["ErrorFolder"] = "./Data/Logs/Errors";
|
||||
Data["LogFolder"] = "./Data/Logs/Logs";
|
||||
Data["ErrorFolder"] = "./Data/Logs/Errors";
|
||||
|
||||
Logger = new DBLogger();
|
||||
|
||||
|
||||
ArchiveManager.Initialize();
|
||||
|
||||
IsLoaded = true;
|
||||
@@ -51,11 +48,11 @@ public class Config
|
||||
|
||||
public class Json<TKey, TValue> : IDictionary<TKey, TValue>
|
||||
{
|
||||
protected IDictionary<TKey, TValue> _dictionary;
|
||||
private readonly string _file = "";
|
||||
|
||||
private readonly string _file = "";
|
||||
private readonly IDictionary<TKey, TValue>? _dictionary;
|
||||
|
||||
/// <summary>
|
||||
/// Empty constructor
|
||||
/// Empty constructor
|
||||
/// </summary>
|
||||
public Json()
|
||||
{
|
||||
@@ -65,13 +62,7 @@ public class Config
|
||||
public Json(string file)
|
||||
{
|
||||
_dictionary = PrivateReadConfig(file).GetAwaiter().GetResult();
|
||||
this._file = file;
|
||||
}
|
||||
|
||||
public async void Save()
|
||||
{
|
||||
if(!string.IsNullOrEmpty(_file))
|
||||
await Functions.SaveToJsonFile(_file, _dictionary);
|
||||
_file = file;
|
||||
}
|
||||
|
||||
public virtual void Add(TKey key, TValue value)
|
||||
@@ -101,9 +92,9 @@ public class Config
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_dictionary.TryGetValue(key, out TValue value)) return value;
|
||||
throw new Exception("Key not found in dictionary " + key.ToString() + " (Json )" + this.GetType().Name + ")");
|
||||
|
||||
if (_dictionary.TryGetValue(key, out var value)) return value;
|
||||
throw new Exception("Key not found in dictionary " + key + " (Json )" + GetType().Name +
|
||||
")");
|
||||
}
|
||||
set
|
||||
{
|
||||
@@ -118,30 +109,6 @@ public class Config
|
||||
return _dictionary.TryGetValue(key, out value);
|
||||
}
|
||||
|
||||
private async Task<Dictionary<TKey, TValue>> PrivateReadConfig(string file)
|
||||
{
|
||||
if (!File.Exists(file))
|
||||
{
|
||||
var dictionary = new Dictionary<TKey, TValue>();
|
||||
await Functions.SaveToJsonFile(file, _dictionary);
|
||||
return dictionary;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var d = await Functions.ConvertFromJson<Dictionary<TKey, TValue>>(file);
|
||||
if (d is null)
|
||||
throw new Exception("Failed to read config file");
|
||||
|
||||
return d;
|
||||
}catch (Exception ex)
|
||||
{
|
||||
File.Delete(file);
|
||||
return new Dictionary<TKey, TValue>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public bool Remove(TKey key)
|
||||
{
|
||||
return _dictionary.Remove(key);
|
||||
@@ -176,6 +143,35 @@ public class Config
|
||||
{
|
||||
return ((IEnumerable)_dictionary).GetEnumerator();
|
||||
}
|
||||
}
|
||||
|
||||
public async void Save()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_file))
|
||||
await Functions.SaveToJsonFile(_file, _dictionary);
|
||||
}
|
||||
|
||||
private async Task<Dictionary<TKey, TValue>> PrivateReadConfig(string file)
|
||||
{
|
||||
if (!File.Exists(file))
|
||||
{
|
||||
var dictionary = new Dictionary<TKey, TValue>();
|
||||
await Functions.SaveToJsonFile(file, _dictionary);
|
||||
return dictionary;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var d = await Functions.ConvertFromJson<Dictionary<TKey, TValue>>(file);
|
||||
if (d is null)
|
||||
throw new Exception("Failed to read config file");
|
||||
|
||||
return d;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
File.Delete(file);
|
||||
return new Dictionary<TKey, TValue>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,489 +1,483 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SQLite;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PluginManager.Database
|
||||
namespace PluginManager.Database;
|
||||
|
||||
public class SqlDatabase
|
||||
{
|
||||
public class SqlDatabase
|
||||
private readonly SQLiteConnection Connection;
|
||||
private readonly string ConnectionString;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize a SQL connection by specifing its private path
|
||||
/// </summary>
|
||||
/// <param name="fileName">The path to the database (it is starting from ./Data/Resources/)</param>
|
||||
public SqlDatabase(string fileName)
|
||||
{
|
||||
private string ConnectionString;
|
||||
private SQLiteConnection Connection;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize a SQL connection by specifing its private path
|
||||
/// </summary>
|
||||
/// <param name="fileName">The path to the database (it is starting from ./Data/Resources/)</param>
|
||||
public SqlDatabase(string fileName)
|
||||
{
|
||||
if (!fileName.StartsWith("./Data/Resources/"))
|
||||
fileName = Path.Combine("./Data/Resources", fileName);
|
||||
if (!File.Exists(fileName))
|
||||
SQLiteConnection.CreateFile(fileName);
|
||||
ConnectionString = $"URI=file:{fileName}";
|
||||
Connection = new SQLiteConnection(ConnectionString);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Open the SQL Connection. To close use the Stop() method
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task Open()
|
||||
{
|
||||
await Connection.OpenAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// Insert into a specified table some values
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="values">The values to be inserted (in the correct order and number)</param>
|
||||
/// <returns></returns>
|
||||
public async Task InsertAsync(string tableName, params string[] values)
|
||||
{
|
||||
string query = $"INSERT INTO {tableName} VALUES (";
|
||||
for (int i = 0; i < values.Length; i++)
|
||||
{
|
||||
query += $"'{values[i]}'";
|
||||
if (i != values.Length - 1)
|
||||
query += ", ";
|
||||
}
|
||||
|
||||
query += ")";
|
||||
|
||||
SQLiteCommand command = new SQLiteCommand(query, Connection);
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// Insert into a specified table some values
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="values">The values to be inserted (in the correct order and number)</param>
|
||||
/// <returns></returns>
|
||||
public void Insert(string tableName, params string[] values)
|
||||
{
|
||||
string query = $"INSERT INTO {tableName} VALUES (";
|
||||
for (int i = 0; i < values.Length; i++)
|
||||
{
|
||||
query += $"'{values[i]}'";
|
||||
if (i != values.Length - 1)
|
||||
query += ", ";
|
||||
}
|
||||
|
||||
query += ")";
|
||||
|
||||
SQLiteCommand command = new SQLiteCommand(query, Connection);
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove every row in a table that has a certain propery
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="KeyName">The column name that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the specified column</param>
|
||||
/// <returns></returns>
|
||||
public async Task RemoveKeyAsync(string tableName, string KeyName, string KeyValue)
|
||||
{
|
||||
string query = $"DELETE FROM {tableName} WHERE {KeyName} = '{KeyValue}'";
|
||||
|
||||
SQLiteCommand command = new SQLiteCommand(query, Connection);
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove every row in a table that has a certain propery
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="KeyName">The column name that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the specified column</param>
|
||||
/// <returns></returns>
|
||||
public void RemoveKey(string tableName, string KeyName, string KeyValue)
|
||||
{
|
||||
string query = $"DELETE FROM {tableName} WHERE {KeyName} = '{KeyValue}'";
|
||||
|
||||
SQLiteCommand command = new SQLiteCommand(query, Connection);
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if the key exists in the table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="keyName">The column that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the specified column</param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> KeyExistsAsync(string tableName, string keyName, string KeyValue)
|
||||
{
|
||||
string query = $"SELECT * FROM {tableName} where {keyName} = '{KeyValue}'";
|
||||
|
||||
if (await ReadDataAsync(query) is not null)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if the key exists in the table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="keyName">The column that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the specified column</param>
|
||||
/// <returns></returns>
|
||||
public bool KeyExists(string tableName, string keyName, string KeyValue)
|
||||
{
|
||||
string query = $"SELECT * FROM {tableName} where {keyName} = '{KeyValue}'";
|
||||
|
||||
if (ReadData(query) is not null)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set value of a column in a table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="keyName">The column that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the column specified</param>
|
||||
/// <param name="ResultColumnName">The column that has to be modified</param>
|
||||
/// <param name="ResultColumnValue">The new value that will replace the old value from the column specified</param>
|
||||
|
||||
public async Task SetValueAsync(string tableName, string keyName, string KeyValue, string ResultColumnName,
|
||||
string ResultColumnValue)
|
||||
{
|
||||
if (!await TableExistsAsync(tableName))
|
||||
throw new System.Exception($"Table {tableName} does not exist");
|
||||
|
||||
await ExecuteAsync(
|
||||
$"UPDATE {tableName} SET {ResultColumnName}='{ResultColumnValue}' WHERE {keyName}='{KeyValue}'");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set value of a column in a table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="keyName">The column that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the column specified</param>
|
||||
/// <param name="ResultColumnName">The column that has to be modified</param>
|
||||
/// <param name="ResultColumnValue">The new value that will replace the old value from the column specified</param>
|
||||
|
||||
public void SetValue(string tableName, string keyName, string KeyValue, string ResultColumnName,
|
||||
string ResultColumnValue)
|
||||
{
|
||||
if (!TableExists(tableName))
|
||||
throw new System.Exception($"Table {tableName} does not exist");
|
||||
|
||||
Execute($"UPDATE {tableName} SET {ResultColumnName}='{ResultColumnValue}' WHERE {keyName}='{KeyValue}'");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get value from a column in a table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="keyName">The column that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the specified column</param>
|
||||
/// <param name="ResultColumnName">The column that has the result</param>
|
||||
/// <returns>A string that has the requested value (can be null if nothing found)</returns>
|
||||
public async Task<string?> GetValueAsync(string tableName, string keyName, string KeyValue,
|
||||
string ResultColumnName)
|
||||
{
|
||||
if (!await TableExistsAsync(tableName))
|
||||
throw new System.Exception($"Table {tableName} does not exist");
|
||||
|
||||
return await ReadDataAsync($"SELECT {ResultColumnName} FROM {tableName} WHERE {keyName}='{KeyValue}'");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get value from a column in a table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="keyName">The column that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the specified column</param>
|
||||
/// <param name="ResultColumnName">The column that has the result</param>
|
||||
/// <returns>A string that has the requested value (can be null if nothing found)</returns>
|
||||
|
||||
public string? GetValue(string tableName, string keyName, string KeyValue, string ResultColumnName)
|
||||
{
|
||||
if (!TableExists(tableName))
|
||||
throw new System.Exception($"Table {tableName} does not exist");
|
||||
|
||||
return ReadData($"SELECT {ResultColumnName} FROM {tableName} WHERE {keyName}='{KeyValue}'");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop the connection to the SQL Database
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async void Stop()
|
||||
{
|
||||
await Connection.CloseAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Change the structure of a table by adding new columns
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="columns">The columns to be added</param>
|
||||
/// <param name="TYPE">The type of the columns (TEXT, INTEGER, FLOAT, etc)</param>
|
||||
/// <returns></returns>
|
||||
public async Task AddColumnsToTableAsync(string tableName, string[] columns, string TYPE = "TEXT")
|
||||
{
|
||||
var command = Connection.CreateCommand();
|
||||
command.CommandText = $"SELECT * FROM {tableName}";
|
||||
var reader = await command.ExecuteReaderAsync();
|
||||
var tableColumns = new List<string>();
|
||||
for (int i = 0; i < reader.FieldCount; i++)
|
||||
tableColumns.Add(reader.GetName(i));
|
||||
|
||||
foreach (var column in columns)
|
||||
{
|
||||
if (!tableColumns.Contains(column))
|
||||
{
|
||||
command.CommandText = $"ALTER TABLE {tableName} ADD COLUMN {column} {TYPE}";
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Change the structure of a table by adding new columns
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="columns">The columns to be added</param>
|
||||
/// <param name="TYPE">The type of the columns (TEXT, INTEGER, FLOAT, etc)</param>
|
||||
/// <returns></returns>
|
||||
|
||||
public void AddColumnsToTable(string tableName, string[] columns, string TYPE = "TEXT")
|
||||
{
|
||||
var command = Connection.CreateCommand();
|
||||
command.CommandText = $"SELECT * FROM {tableName}";
|
||||
var reader = command.ExecuteReader();
|
||||
var tableColumns = new List<string>();
|
||||
for (int i = 0; i < reader.FieldCount; i++)
|
||||
tableColumns.Add(reader.GetName(i));
|
||||
|
||||
foreach (var column in columns)
|
||||
{
|
||||
if (!tableColumns.Contains(column))
|
||||
{
|
||||
command.CommandText = $"ALTER TABLE {tableName} ADD COLUMN {column} {TYPE}";
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if a table exists
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <returns>True if the table exists, false if not</returns>
|
||||
public async Task<bool> TableExistsAsync(string tableName)
|
||||
{
|
||||
var cmd = Connection.CreateCommand();
|
||||
cmd.CommandText = $"SELECT name FROM sqlite_master WHERE type='table' AND name='{tableName}'";
|
||||
var result = await cmd.ExecuteScalarAsync();
|
||||
|
||||
if (result == null)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if a table exists
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <returns>True if the table exists, false if not</returns>
|
||||
public bool TableExists(string tableName)
|
||||
{
|
||||
var cmd = Connection.CreateCommand();
|
||||
cmd.CommandText = $"SELECT name FROM sqlite_master WHERE type='table' AND name='{tableName}'";
|
||||
var result = cmd.ExecuteScalar();
|
||||
|
||||
if (result == null)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="columns">The columns of the table</param>
|
||||
/// <returns></returns>
|
||||
|
||||
public async Task CreateTableAsync(string tableName, params string[] columns)
|
||||
{
|
||||
var cmd = Connection.CreateCommand();
|
||||
cmd.CommandText = $"CREATE TABLE IF NOT EXISTS {tableName} ({string.Join(", ", columns)})";
|
||||
await cmd.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="columns">The columns of the table</param>
|
||||
/// <returns></returns>
|
||||
|
||||
public void CreateTable(string tableName, params string[] columns)
|
||||
{
|
||||
var cmd = Connection.CreateCommand();
|
||||
cmd.CommandText = $"CREATE TABLE IF NOT EXISTS {tableName} ({string.Join(", ", columns)})";
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Execute a custom query
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>The number of rows that the query modified</returns>
|
||||
public async Task<int> ExecuteAsync(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
||||
await Connection.OpenAsync();
|
||||
var command = new SQLiteCommand(query, Connection);
|
||||
int answer = await command.ExecuteNonQueryAsync();
|
||||
return answer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Execute a custom query
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>The number of rows that the query modified</returns>
|
||||
public int Execute(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
||||
Connection.Open();
|
||||
var command = new SQLiteCommand(query, Connection);
|
||||
int r = command.ExecuteNonQuery();
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read data from the result table and return the first row
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>The result is a string that has all values separated by space character</returns>
|
||||
public async Task<string?> ReadDataAsync(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
||||
await Connection.OpenAsync();
|
||||
var command = new SQLiteCommand(query, Connection);
|
||||
var reader = await command.ExecuteReaderAsync();
|
||||
|
||||
object[] values = new object[reader.FieldCount];
|
||||
if (reader.Read())
|
||||
{
|
||||
reader.GetValues(values);
|
||||
return string.Join<object>(" ", values);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read data from the result table and return the first row
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>The result is a string that has all values separated by space character</returns>
|
||||
public string? ReadData(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
||||
Connection.Open();
|
||||
var command = new SQLiteCommand(query, Connection);
|
||||
var reader = command.ExecuteReader();
|
||||
|
||||
object[] values = new object[reader.FieldCount];
|
||||
if (reader.Read())
|
||||
{
|
||||
reader.GetValues(values);
|
||||
return string.Join<object>(" ", values);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read data from the result table and return the first row
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>The first row as separated items</returns>
|
||||
public async Task<object[]?> ReadDataArrayAsync(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
||||
await Connection.OpenAsync();
|
||||
var command = new SQLiteCommand(query, Connection);
|
||||
var reader = await command.ExecuteReaderAsync();
|
||||
|
||||
object[] values = new object[reader.FieldCount];
|
||||
if (reader.Read())
|
||||
{
|
||||
reader.GetValues(values);
|
||||
return values;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Read data from the result table and return the first row
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>The first row as separated items</returns>
|
||||
public object[]? ReadDataArray(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
||||
Connection.Open();
|
||||
var command = new SQLiteCommand(query, Connection);
|
||||
var reader = command.ExecuteReader();
|
||||
|
||||
object[] values = new object[reader.FieldCount];
|
||||
if (reader.Read())
|
||||
{
|
||||
reader.GetValues(values);
|
||||
return values;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read all rows from the result table and return them as a list of string arrays. The string arrays contain the values of each row
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>A list of string arrays representing the values that the query returns</returns>
|
||||
|
||||
public async Task<List<string[]>?> ReadAllRowsAsync(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(System.Data.ConnectionState.Open))
|
||||
await Connection.OpenAsync();
|
||||
var command = new SQLiteCommand(query, Connection);
|
||||
var reader = await command.ExecuteReaderAsync();
|
||||
|
||||
if (!reader.HasRows)
|
||||
return null;
|
||||
|
||||
List<string[]> rows = new();
|
||||
while (await reader.ReadAsync())
|
||||
{
|
||||
string[] values = new string[reader.FieldCount];
|
||||
reader.GetValues(values);
|
||||
rows.Add(values);
|
||||
}
|
||||
|
||||
if (rows.Count == 0) return null;
|
||||
|
||||
return rows;
|
||||
}
|
||||
if (!fileName.StartsWith("./Data/Resources/"))
|
||||
fileName = Path.Combine("./Data/Resources", fileName);
|
||||
if (!File.Exists(fileName))
|
||||
SQLiteConnection.CreateFile(fileName);
|
||||
ConnectionString = $"URI=file:{fileName}";
|
||||
Connection = new SQLiteConnection(ConnectionString);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Open the SQL Connection. To close use the Stop() method
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task Open()
|
||||
{
|
||||
await Connection.OpenAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// Insert into a specified table some values
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="values">The values to be inserted (in the correct order and number)</param>
|
||||
/// <returns></returns>
|
||||
public async Task InsertAsync(string tableName, params string[] values)
|
||||
{
|
||||
var query = $"INSERT INTO {tableName} VALUES (";
|
||||
for (var i = 0; i < values.Length; i++)
|
||||
{
|
||||
query += $"'{values[i]}'";
|
||||
if (i != values.Length - 1)
|
||||
query += ", ";
|
||||
}
|
||||
|
||||
query += ")";
|
||||
|
||||
var command = new SQLiteCommand(query, Connection);
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// Insert into a specified table some values
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="values">The values to be inserted (in the correct order and number)</param>
|
||||
/// <returns></returns>
|
||||
public void Insert(string tableName, params string[] values)
|
||||
{
|
||||
var query = $"INSERT INTO {tableName} VALUES (";
|
||||
for (var i = 0; i < values.Length; i++)
|
||||
{
|
||||
query += $"'{values[i]}'";
|
||||
if (i != values.Length - 1)
|
||||
query += ", ";
|
||||
}
|
||||
|
||||
query += ")";
|
||||
|
||||
var command = new SQLiteCommand(query, Connection);
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove every row in a table that has a certain propery
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="KeyName">The column name that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the specified column</param>
|
||||
/// <returns></returns>
|
||||
public async Task RemoveKeyAsync(string tableName, string KeyName, string KeyValue)
|
||||
{
|
||||
var query = $"DELETE FROM {tableName} WHERE {KeyName} = '{KeyValue}'";
|
||||
|
||||
var command = new SQLiteCommand(query, Connection);
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove every row in a table that has a certain propery
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="KeyName">The column name that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the specified column</param>
|
||||
/// <returns></returns>
|
||||
public void RemoveKey(string tableName, string KeyName, string KeyValue)
|
||||
{
|
||||
var query = $"DELETE FROM {tableName} WHERE {KeyName} = '{KeyValue}'";
|
||||
|
||||
var command = new SQLiteCommand(query, Connection);
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if the key exists in the table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="keyName">The column that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the specified column</param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> KeyExistsAsync(string tableName, string keyName, string KeyValue)
|
||||
{
|
||||
var query = $"SELECT * FROM {tableName} where {keyName} = '{KeyValue}'";
|
||||
|
||||
if (await ReadDataAsync(query) is not null)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if the key exists in the table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="keyName">The column that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the specified column</param>
|
||||
/// <returns></returns>
|
||||
public bool KeyExists(string tableName, string keyName, string KeyValue)
|
||||
{
|
||||
var query = $"SELECT * FROM {tableName} where {keyName} = '{KeyValue}'";
|
||||
|
||||
if (ReadData(query) is not null)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set value of a column in a table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="keyName">The column that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the column specified</param>
|
||||
/// <param name="ResultColumnName">The column that has to be modified</param>
|
||||
/// <param name="ResultColumnValue">The new value that will replace the old value from the column specified</param>
|
||||
public async Task SetValueAsync(
|
||||
string tableName, string keyName, string KeyValue, string ResultColumnName,
|
||||
string ResultColumnValue)
|
||||
{
|
||||
if (!await TableExistsAsync(tableName))
|
||||
throw new Exception($"Table {tableName} does not exist");
|
||||
|
||||
await ExecuteAsync(
|
||||
$"UPDATE {tableName} SET {ResultColumnName}='{ResultColumnValue}' WHERE {keyName}='{KeyValue}'");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set value of a column in a table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="keyName">The column that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the column specified</param>
|
||||
/// <param name="ResultColumnName">The column that has to be modified</param>
|
||||
/// <param name="ResultColumnValue">The new value that will replace the old value from the column specified</param>
|
||||
public void SetValue(
|
||||
string tableName, string keyName, string KeyValue, string ResultColumnName,
|
||||
string ResultColumnValue)
|
||||
{
|
||||
if (!TableExists(tableName))
|
||||
throw new Exception($"Table {tableName} does not exist");
|
||||
|
||||
Execute($"UPDATE {tableName} SET {ResultColumnName}='{ResultColumnValue}' WHERE {keyName}='{KeyValue}'");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get value from a column in a table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="keyName">The column that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the specified column</param>
|
||||
/// <param name="ResultColumnName">The column that has the result</param>
|
||||
/// <returns>A string that has the requested value (can be null if nothing found)</returns>
|
||||
public async Task<string?> GetValueAsync(
|
||||
string tableName, string keyName, string KeyValue,
|
||||
string ResultColumnName)
|
||||
{
|
||||
if (!await TableExistsAsync(tableName))
|
||||
throw new Exception($"Table {tableName} does not exist");
|
||||
|
||||
return await ReadDataAsync($"SELECT {ResultColumnName} FROM {tableName} WHERE {keyName}='{KeyValue}'");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get value from a column in a table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="keyName">The column that the search is made by</param>
|
||||
/// <param name="KeyValue">The value that is searched in the specified column</param>
|
||||
/// <param name="ResultColumnName">The column that has the result</param>
|
||||
/// <returns>A string that has the requested value (can be null if nothing found)</returns>
|
||||
public string? GetValue(string tableName, string keyName, string KeyValue, string ResultColumnName)
|
||||
{
|
||||
if (!TableExists(tableName))
|
||||
throw new Exception($"Table {tableName} does not exist");
|
||||
|
||||
return ReadData($"SELECT {ResultColumnName} FROM {tableName} WHERE {keyName}='{KeyValue}'");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop the connection to the SQL Database
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async void Stop()
|
||||
{
|
||||
await Connection.CloseAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Change the structure of a table by adding new columns
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="columns">The columns to be added</param>
|
||||
/// <param name="TYPE">The type of the columns (TEXT, INTEGER, FLOAT, etc)</param>
|
||||
/// <returns></returns>
|
||||
public async Task AddColumnsToTableAsync(string tableName, string[] columns, string TYPE = "TEXT")
|
||||
{
|
||||
var command = Connection.CreateCommand();
|
||||
command.CommandText = $"SELECT * FROM {tableName}";
|
||||
var reader = await command.ExecuteReaderAsync();
|
||||
var tableColumns = new List<string>();
|
||||
for (var i = 0; i < reader.FieldCount; i++)
|
||||
tableColumns.Add(reader.GetName(i));
|
||||
|
||||
foreach (var column in columns)
|
||||
if (!tableColumns.Contains(column))
|
||||
{
|
||||
command.CommandText = $"ALTER TABLE {tableName} ADD COLUMN {column} {TYPE}";
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Change the structure of a table by adding new columns
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="columns">The columns to be added</param>
|
||||
/// <param name="TYPE">The type of the columns (TEXT, INTEGER, FLOAT, etc)</param>
|
||||
/// <returns></returns>
|
||||
public void AddColumnsToTable(string tableName, string[] columns, string TYPE = "TEXT")
|
||||
{
|
||||
var command = Connection.CreateCommand();
|
||||
command.CommandText = $"SELECT * FROM {tableName}";
|
||||
var reader = command.ExecuteReader();
|
||||
var tableColumns = new List<string>();
|
||||
for (var i = 0; i < reader.FieldCount; i++)
|
||||
tableColumns.Add(reader.GetName(i));
|
||||
|
||||
foreach (var column in columns)
|
||||
if (!tableColumns.Contains(column))
|
||||
{
|
||||
command.CommandText = $"ALTER TABLE {tableName} ADD COLUMN {column} {TYPE}";
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if a table exists
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <returns>True if the table exists, false if not</returns>
|
||||
public async Task<bool> TableExistsAsync(string tableName)
|
||||
{
|
||||
var cmd = Connection.CreateCommand();
|
||||
cmd.CommandText = $"SELECT name FROM sqlite_master WHERE type='table' AND name='{tableName}'";
|
||||
var result = await cmd.ExecuteScalarAsync();
|
||||
|
||||
if (result == null)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if a table exists
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <returns>True if the table exists, false if not</returns>
|
||||
public bool TableExists(string tableName)
|
||||
{
|
||||
var cmd = Connection.CreateCommand();
|
||||
cmd.CommandText = $"SELECT name FROM sqlite_master WHERE type='table' AND name='{tableName}'";
|
||||
var result = cmd.ExecuteScalar();
|
||||
|
||||
if (result == null)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="columns">The columns of the table</param>
|
||||
/// <returns></returns>
|
||||
public async Task CreateTableAsync(string tableName, params string[] columns)
|
||||
{
|
||||
var cmd = Connection.CreateCommand();
|
||||
cmd.CommandText = $"CREATE TABLE IF NOT EXISTS {tableName} ({string.Join(", ", columns)})";
|
||||
await cmd.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a table
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table name</param>
|
||||
/// <param name="columns">The columns of the table</param>
|
||||
/// <returns></returns>
|
||||
public void CreateTable(string tableName, params string[] columns)
|
||||
{
|
||||
var cmd = Connection.CreateCommand();
|
||||
cmd.CommandText = $"CREATE TABLE IF NOT EXISTS {tableName} ({string.Join(", ", columns)})";
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Execute a custom query
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>The number of rows that the query modified</returns>
|
||||
public async Task<int> ExecuteAsync(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(ConnectionState.Open))
|
||||
await Connection.OpenAsync();
|
||||
var command = new SQLiteCommand(query, Connection);
|
||||
var answer = await command.ExecuteNonQueryAsync();
|
||||
return answer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Execute a custom query
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>The number of rows that the query modified</returns>
|
||||
public int Execute(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(ConnectionState.Open))
|
||||
Connection.Open();
|
||||
var command = new SQLiteCommand(query, Connection);
|
||||
var r = command.ExecuteNonQuery();
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read data from the result table and return the first row
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>The result is a string that has all values separated by space character</returns>
|
||||
public async Task<string?> ReadDataAsync(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(ConnectionState.Open))
|
||||
await Connection.OpenAsync();
|
||||
var command = new SQLiteCommand(query, Connection);
|
||||
var reader = await command.ExecuteReaderAsync();
|
||||
|
||||
var values = new object[reader.FieldCount];
|
||||
if (reader.Read())
|
||||
{
|
||||
reader.GetValues(values);
|
||||
return string.Join<object>(" ", values);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read data from the result table and return the first row
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>The result is a string that has all values separated by space character</returns>
|
||||
public string? ReadData(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(ConnectionState.Open))
|
||||
Connection.Open();
|
||||
var command = new SQLiteCommand(query, Connection);
|
||||
var reader = command.ExecuteReader();
|
||||
|
||||
var values = new object[reader.FieldCount];
|
||||
if (reader.Read())
|
||||
{
|
||||
reader.GetValues(values);
|
||||
return string.Join<object>(" ", values);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read data from the result table and return the first row
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>The first row as separated items</returns>
|
||||
public async Task<object[]?> ReadDataArrayAsync(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(ConnectionState.Open))
|
||||
await Connection.OpenAsync();
|
||||
var command = new SQLiteCommand(query, Connection);
|
||||
var reader = await command.ExecuteReaderAsync();
|
||||
|
||||
var values = new object[reader.FieldCount];
|
||||
if (reader.Read())
|
||||
{
|
||||
reader.GetValues(values);
|
||||
return values;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Read data from the result table and return the first row
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>The first row as separated items</returns>
|
||||
public object[]? ReadDataArray(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(ConnectionState.Open))
|
||||
Connection.Open();
|
||||
var command = new SQLiteCommand(query, Connection);
|
||||
var reader = command.ExecuteReader();
|
||||
|
||||
var values = new object[reader.FieldCount];
|
||||
if (reader.Read())
|
||||
{
|
||||
reader.GetValues(values);
|
||||
return values;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read all rows from the result table and return them as a list of string arrays. The string arrays contain the
|
||||
/// values of each row
|
||||
/// </summary>
|
||||
/// <param name="query">The query</param>
|
||||
/// <returns>A list of string arrays representing the values that the query returns</returns>
|
||||
public async Task<List<string[]>?> ReadAllRowsAsync(string query)
|
||||
{
|
||||
if (!Connection.State.HasFlag(ConnectionState.Open))
|
||||
await Connection.OpenAsync();
|
||||
var command = new SQLiteCommand(query, Connection);
|
||||
var reader = await command.ExecuteReaderAsync();
|
||||
|
||||
if (!reader.HasRows)
|
||||
return null;
|
||||
|
||||
List<string[]> rows = new();
|
||||
while (await reader.ReadAsync())
|
||||
{
|
||||
var values = new string[reader.FieldCount];
|
||||
reader.GetValues(values);
|
||||
rows.Add(values);
|
||||
}
|
||||
|
||||
if (rows.Count == 0) return null;
|
||||
|
||||
return rows;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,4 +47,4 @@ public interface DBCommand
|
||||
void ExecuteDM(DBCommandExecutingArguments args)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,4 +19,4 @@ public interface DBEvent
|
||||
/// </summary>
|
||||
/// <param name="client">The discord bot client</param>
|
||||
void Start(DiscordSocketClient client);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,21 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace PluginManager.Interfaces
|
||||
namespace PluginManager.Interfaces;
|
||||
|
||||
public interface DBSlashCommand
|
||||
{
|
||||
public interface DBSlashCommand
|
||||
string Name { get; }
|
||||
string Description { get; }
|
||||
|
||||
bool canUseDM { get; }
|
||||
|
||||
List<SlashCommandOptionBuilder> Options { get; }
|
||||
|
||||
void ExecuteServer(SocketSlashCommand context)
|
||||
{
|
||||
string Name { get; }
|
||||
string Description { get; }
|
||||
|
||||
bool canUseDM { get; }
|
||||
|
||||
List<SlashCommandOptionBuilder> Options { get; }
|
||||
|
||||
void ExecuteServer(SocketSlashCommand context)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ExecuteDM(SocketSlashCommand context) { }
|
||||
|
||||
}
|
||||
|
||||
void ExecuteDM(SocketSlashCommand context) { }
|
||||
}
|
||||
|
||||
@@ -1,23 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using PluginManager.Others;
|
||||
|
||||
namespace PluginManager.Interfaces
|
||||
namespace PluginManager.Interfaces;
|
||||
|
||||
public interface ICommandAction
|
||||
{
|
||||
public interface ICommandAction
|
||||
{
|
||||
public string ActionName { get; }
|
||||
public string ActionName { get; }
|
||||
|
||||
public string? Description { get; }
|
||||
public string? Description { get; }
|
||||
|
||||
public string? Usage { get; }
|
||||
public string? Usage { get; }
|
||||
|
||||
public InternalActionRunType RunType { get; }
|
||||
public InternalActionRunType RunType { get; }
|
||||
|
||||
public Task Execute(string[]? args);
|
||||
|
||||
}
|
||||
}
|
||||
public Task Execute(string[]? args);
|
||||
}
|
||||
|
||||
@@ -1,76 +1,72 @@
|
||||
using System.IO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Reflection;
|
||||
using PluginManager.Others.Actions;
|
||||
using System.Threading.Tasks;
|
||||
using PluginManager.Interfaces;
|
||||
using System.Collections;
|
||||
using PluginManager.Others;
|
||||
|
||||
namespace PluginManager.Loaders
|
||||
namespace PluginManager.Loaders;
|
||||
|
||||
public class ActionsLoader
|
||||
{
|
||||
public class ActionsLoader
|
||||
public delegate void ActionLoaded(string name, string typeName, bool success, Exception? e = null);
|
||||
|
||||
private readonly string actionExtension = "dll";
|
||||
|
||||
private readonly string actionFolder = @"./Data/Actions/";
|
||||
|
||||
public ActionsLoader(string path, string extension)
|
||||
{
|
||||
public delegate void ActionLoaded(string name, string typeName, bool success, Exception? e = null);
|
||||
public event ActionLoaded? ActionLoadedEvent;
|
||||
|
||||
private string actionFolder = @"./Data/Actions/";
|
||||
private string actionExtension = "dll";
|
||||
|
||||
public ActionsLoader(string path, string extension)
|
||||
{
|
||||
actionFolder = path;
|
||||
actionExtension = extension;
|
||||
}
|
||||
|
||||
public async Task<List<ICommandAction>?> Load()
|
||||
{
|
||||
Directory.CreateDirectory(actionFolder);
|
||||
var files = Directory.GetFiles(actionFolder, $"*.{actionExtension}", SearchOption.AllDirectories);
|
||||
|
||||
List<ICommandAction> actions = new List<ICommandAction>();
|
||||
|
||||
foreach (var file in files)
|
||||
{
|
||||
try
|
||||
{
|
||||
Assembly.LoadFrom(file);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ActionLoadedEvent?.Invoke(file, "", false, e);
|
||||
}
|
||||
}
|
||||
|
||||
var types = AppDomain.CurrentDomain.GetAssemblies()
|
||||
.SelectMany(s => s.GetTypes())
|
||||
.Where(p => typeof(ICommandAction).IsAssignableFrom(p) && !p.IsInterface);
|
||||
|
||||
foreach (var type in types)
|
||||
{
|
||||
try
|
||||
{
|
||||
var action = (ICommandAction) Activator.CreateInstance(type);
|
||||
if (action.ActionName == null)
|
||||
{
|
||||
ActionLoadedEvent?.Invoke(action.ActionName, type.Name, false);
|
||||
continue;
|
||||
}
|
||||
|
||||
if(action.RunType == PluginManager.Others.InternalActionRunType.ON_STARTUP)
|
||||
await action.Execute(null);
|
||||
|
||||
ActionLoadedEvent?.Invoke(action.ActionName, type.Name, true);
|
||||
actions.Add(action);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ActionLoadedEvent?.Invoke(type.Name, type.Name, false, e);
|
||||
}
|
||||
}
|
||||
|
||||
return actions;
|
||||
}
|
||||
actionFolder = path;
|
||||
actionExtension = extension;
|
||||
}
|
||||
}
|
||||
|
||||
public event ActionLoaded? ActionLoadedEvent;
|
||||
|
||||
public async Task<List<ICommandAction>?> Load()
|
||||
{
|
||||
Directory.CreateDirectory(actionFolder);
|
||||
var files = Directory.GetFiles(actionFolder, $"*.{actionExtension}", SearchOption.AllDirectories);
|
||||
|
||||
var actions = new List<ICommandAction>();
|
||||
|
||||
foreach (var file in files)
|
||||
try
|
||||
{
|
||||
Assembly.LoadFrom(file);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ActionLoadedEvent?.Invoke(file, "", false, e);
|
||||
}
|
||||
|
||||
var types = AppDomain.CurrentDomain.GetAssemblies()
|
||||
.SelectMany(s => s.GetTypes())
|
||||
.Where(p => typeof(ICommandAction).IsAssignableFrom(p) && !p.IsInterface);
|
||||
|
||||
foreach (var type in types)
|
||||
try
|
||||
{
|
||||
var action = (ICommandAction)Activator.CreateInstance(type);
|
||||
if (action.ActionName == null)
|
||||
{
|
||||
ActionLoadedEvent?.Invoke(action.ActionName, type.Name, false);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (action.RunType == InternalActionRunType.ON_STARTUP)
|
||||
await action.Execute(null);
|
||||
|
||||
ActionLoadedEvent?.Invoke(action.ActionName, type.Name, true);
|
||||
actions.Add(action);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ActionLoadedEvent?.Invoke(type.Name, type.Name, false, e);
|
||||
}
|
||||
|
||||
return actions;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,143 +3,143 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
using PluginManager.Interfaces;
|
||||
using PluginManager.Others;
|
||||
|
||||
namespace PluginManager.Loaders
|
||||
namespace PluginManager.Loaders;
|
||||
|
||||
internal class LoaderArgs : EventArgs
|
||||
{
|
||||
internal class LoaderArgs : EventArgs
|
||||
internal string? PluginName { get; init; }
|
||||
internal string? TypeName { get; init; }
|
||||
internal bool IsLoaded { get; init; }
|
||||
internal Exception? Exception { get; init; }
|
||||
internal object? Plugin { get; init; }
|
||||
}
|
||||
|
||||
internal class Loader
|
||||
{
|
||||
internal Loader(string path, string extension)
|
||||
{
|
||||
internal string? PluginName { get; init; }
|
||||
internal string? TypeName { get; init; }
|
||||
internal bool IsLoaded { get; init; }
|
||||
internal Exception? Exception { get; init; }
|
||||
internal object? Plugin { get; init; }
|
||||
this.path = path;
|
||||
this.extension = extension;
|
||||
}
|
||||
internal class Loader
|
||||
|
||||
|
||||
private string path { get; }
|
||||
private string extension { get; }
|
||||
|
||||
internal event FileLoadedEventHandler? FileLoaded;
|
||||
|
||||
internal event PluginLoadedEventHandler? PluginLoaded;
|
||||
|
||||
|
||||
internal (List<DBEvent>?, List<DBCommand>?, List<DBSlashCommand>?) Load()
|
||||
{
|
||||
internal Loader(string path, string extension)
|
||||
List<DBEvent> events = new();
|
||||
List<DBSlashCommand> slashCommands = new();
|
||||
List<DBCommand> commands = new();
|
||||
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
this.path = path;
|
||||
this.extension = extension;
|
||||
Directory.CreateDirectory(path);
|
||||
return (null, null, null);
|
||||
}
|
||||
|
||||
|
||||
private string path { get; }
|
||||
private string extension { get; }
|
||||
|
||||
internal event FileLoadedEventHandler? FileLoaded;
|
||||
|
||||
internal event PluginLoadedEventHandler? PluginLoaded;
|
||||
|
||||
|
||||
internal delegate void FileLoadedEventHandler(LoaderArgs args);
|
||||
|
||||
internal delegate void PluginLoadedEventHandler(LoaderArgs args);
|
||||
|
||||
|
||||
internal (List<DBEvent>?, List<DBCommand>?, List<DBSlashCommand>?) Load()
|
||||
var files = Directory.GetFiles(path, $"*.{extension}", SearchOption.AllDirectories);
|
||||
foreach (var file in files)
|
||||
{
|
||||
|
||||
List<DBEvent> events = new();
|
||||
List<DBSlashCommand> slashCommands = new();
|
||||
List<DBCommand> commands = new();
|
||||
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
Directory.CreateDirectory(path);
|
||||
return (null, null, null);
|
||||
}
|
||||
|
||||
var files = Directory.GetFiles(path, $"*.{extension}", SearchOption.AllDirectories);
|
||||
foreach (var file in files)
|
||||
{
|
||||
try
|
||||
{
|
||||
Assembly.LoadFrom(file);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Config.Logger.Log("PluginName: " + new FileInfo(file).Name.Split('.')[0] + " not loaded", this, Others.LogLevel.ERROR);
|
||||
continue;
|
||||
}
|
||||
if (FileLoaded != null)
|
||||
{
|
||||
var args = new LoaderArgs
|
||||
{
|
||||
Exception = null,
|
||||
TypeName = null,
|
||||
IsLoaded = false,
|
||||
PluginName = new FileInfo(file).Name.Split('.')[0],
|
||||
Plugin = null
|
||||
};
|
||||
FileLoaded.Invoke(args);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return (LoadItems<DBEvent>(), LoadItems<DBCommand>(), LoadItems<DBSlashCommand>());
|
||||
}
|
||||
|
||||
internal List<T> LoadItems<T>()
|
||||
{
|
||||
List<T> list = new();
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
var interfaceType = typeof(T);
|
||||
var types = AppDomain.CurrentDomain.GetAssemblies()
|
||||
.SelectMany(a => a.GetTypes())
|
||||
.Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass)
|
||||
.ToArray();
|
||||
|
||||
|
||||
list.Clear();
|
||||
foreach (var type in types)
|
||||
try
|
||||
{
|
||||
var plugin = (T)Activator.CreateInstance(type)!;
|
||||
list.Add(plugin);
|
||||
|
||||
|
||||
if (PluginLoaded != null)
|
||||
PluginLoaded.Invoke(new LoaderArgs
|
||||
{
|
||||
Exception = null,
|
||||
IsLoaded = true,
|
||||
PluginName = type.FullName,
|
||||
TypeName = typeof(T) == typeof(DBCommand) ? "DBCommand" :
|
||||
typeof(T) == typeof(DBEvent) ? "DBEvent" :
|
||||
typeof(T) == typeof(DBSlashCommand) ? "DBSlashCommand" :
|
||||
null,
|
||||
Plugin = plugin
|
||||
}
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (PluginLoaded != null)
|
||||
PluginLoaded.Invoke(new LoaderArgs
|
||||
{
|
||||
Exception = ex,
|
||||
IsLoaded = false,
|
||||
PluginName = type.FullName,
|
||||
TypeName = nameof(T)
|
||||
});
|
||||
}
|
||||
|
||||
return list;
|
||||
Assembly.LoadFrom(file);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Config.Logger.Log(ex.Message, this, Others.LogLevel.ERROR);
|
||||
|
||||
return null;
|
||||
Config.Logger.Log("PluginName: " + new FileInfo(file).Name.Split('.')[0] + " not loaded", this,
|
||||
LogLevel.ERROR);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (FileLoaded != null)
|
||||
{
|
||||
var args = new LoaderArgs
|
||||
{
|
||||
Exception = null,
|
||||
TypeName = null,
|
||||
IsLoaded = false,
|
||||
PluginName = new FileInfo(file).Name.Split('.')[0],
|
||||
Plugin = null
|
||||
};
|
||||
FileLoaded.Invoke(args);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return (LoadItems<DBEvent>(), LoadItems<DBCommand>(), LoadItems<DBSlashCommand>());
|
||||
}
|
||||
|
||||
internal List<T> LoadItems<T>()
|
||||
{
|
||||
List<T> list = new();
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
var interfaceType = typeof(T);
|
||||
var types = AppDomain.CurrentDomain.GetAssemblies()
|
||||
.SelectMany(a => a.GetTypes())
|
||||
.Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass)
|
||||
.ToArray();
|
||||
|
||||
|
||||
list.Clear();
|
||||
foreach (var type in types)
|
||||
try
|
||||
{
|
||||
var plugin = (T)Activator.CreateInstance(type)!;
|
||||
list.Add(plugin);
|
||||
|
||||
|
||||
if (PluginLoaded != null)
|
||||
PluginLoaded.Invoke(new LoaderArgs
|
||||
{
|
||||
Exception = null,
|
||||
IsLoaded = true,
|
||||
PluginName = type.FullName,
|
||||
TypeName = typeof(T) == typeof(DBCommand) ? "DBCommand" :
|
||||
typeof(T) == typeof(DBEvent) ? "DBEvent" :
|
||||
typeof(T) == typeof(DBSlashCommand) ? "DBSlashCommand" :
|
||||
null,
|
||||
Plugin = plugin
|
||||
}
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (PluginLoaded != null)
|
||||
PluginLoaded.Invoke(new LoaderArgs
|
||||
{
|
||||
Exception = ex,
|
||||
IsLoaded = false,
|
||||
PluginName = type.FullName,
|
||||
TypeName = nameof(T)
|
||||
});
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Config.Logger.Log(ex.Message, this, LogLevel.ERROR);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
internal delegate void FileLoadedEventHandler(LoaderArgs args);
|
||||
|
||||
internal delegate void PluginLoadedEventHandler(LoaderArgs args);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
|
||||
using PluginManager.Interfaces;
|
||||
using PluginManager.Online;
|
||||
using PluginManager.Others;
|
||||
|
||||
namespace PluginManager.Loaders;
|
||||
@@ -23,7 +19,7 @@ public class PluginLoader
|
||||
|
||||
private const string pluginFolder = @"./Data/Plugins/";
|
||||
|
||||
internal const string pluginExtension = "dll";
|
||||
internal const string pluginExtension = "dll";
|
||||
private readonly DiscordSocketClient _client;
|
||||
|
||||
/// <summary>
|
||||
@@ -62,20 +58,24 @@ public class PluginLoader
|
||||
public static List<DBEvent>? Events { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A list of <see cref="DBSlashCommand"/> commands
|
||||
/// A list of <see cref="DBSlashCommand" /> commands
|
||||
/// </summary>
|
||||
public static List<DBSlashCommand>? SlashCommands { get; set; }
|
||||
|
||||
public static int PluginsLoaded { get {
|
||||
var count = 0;
|
||||
if (Commands is not null)
|
||||
count += Commands.Count;
|
||||
if (Events is not null)
|
||||
count += Events.Count;
|
||||
if (SlashCommands is not null)
|
||||
count += SlashCommands.Count;
|
||||
return count;
|
||||
}}
|
||||
public static int PluginsLoaded
|
||||
{
|
||||
get
|
||||
{
|
||||
var count = 0;
|
||||
if (Commands is not null)
|
||||
count += Commands.Count;
|
||||
if (Events is not null)
|
||||
count += Events.Count;
|
||||
if (SlashCommands is not null)
|
||||
count += SlashCommands.Count;
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The main mathod that is called to load all events
|
||||
@@ -84,24 +84,24 @@ public class PluginLoader
|
||||
{
|
||||
//Load all plugins
|
||||
|
||||
Commands = new List<DBCommand>();
|
||||
Events = new List<DBEvent>();
|
||||
Commands = new List<DBCommand>();
|
||||
Events = new List<DBEvent>();
|
||||
SlashCommands = new List<DBSlashCommand>();
|
||||
|
||||
Config.Logger.Log("Starting plugin loader ... Client: " + _client.CurrentUser.Username, this, Others.LogLevel.INFO);
|
||||
Config.Logger.Log("Starting plugin loader ... Client: " + _client.CurrentUser.Username, this,
|
||||
LogLevel.INFO);
|
||||
|
||||
var loader = new Loader("./Data/Plugins", "dll");
|
||||
loader.FileLoaded += (args) => Config.Logger.Log($"{args.PluginName} file Loaded", this , Others.LogLevel.INFO);
|
||||
loader.FileLoaded += args => Config.Logger.Log($"{args.PluginName} file Loaded", this, LogLevel.INFO);
|
||||
loader.PluginLoaded += Loader_PluginLoaded;
|
||||
var res = loader.Load();
|
||||
Events = res.Item1;
|
||||
Commands = res.Item2;
|
||||
Events = res.Item1;
|
||||
Commands = res.Item2;
|
||||
SlashCommands = res.Item3;
|
||||
}
|
||||
|
||||
private async void Loader_PluginLoaded(LoaderArgs args)
|
||||
{
|
||||
|
||||
switch (args.TypeName)
|
||||
{
|
||||
case "DBCommand":
|
||||
@@ -117,27 +117,29 @@ public class PluginLoader
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Config.Logger.Log(ex.Message, this, Others.LogLevel.ERROR);
|
||||
Config.Logger.Log(ex.Message, this, LogLevel.ERROR);
|
||||
}
|
||||
|
||||
break;
|
||||
case "DBSlashCommand":
|
||||
if (args.IsLoaded)
|
||||
{
|
||||
var slash = (DBSlashCommand)args.Plugin;
|
||||
SlashCommandBuilder builder = new SlashCommandBuilder();
|
||||
var slash = (DBSlashCommand)args.Plugin;
|
||||
var builder = new SlashCommandBuilder();
|
||||
builder.WithName(slash.Name);
|
||||
builder.WithDescription(slash.Description);
|
||||
builder.WithDMPermission(slash.canUseDM);
|
||||
builder.Options = slash.Options;
|
||||
|
||||
onSLSHLoad?.Invoke(((DBSlashCommand)args.Plugin!).Name, args.TypeName, args.IsLoaded, args.Exception);
|
||||
onSLSHLoad?.Invoke(((DBSlashCommand)args.Plugin!).Name, args.TypeName, args.IsLoaded,
|
||||
args.Exception);
|
||||
await _client.CreateGlobalApplicationCommandAsync(builder.Build());
|
||||
|
||||
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task LoadPluginFromAssembly(Assembly asmb, DiscordSocketClient client)
|
||||
{
|
||||
var types = asmb.GetTypes();
|
||||
@@ -146,28 +148,27 @@ public class PluginLoader
|
||||
{
|
||||
var instance = (DBEvent)Activator.CreateInstance(type);
|
||||
instance.Start(client);
|
||||
PluginLoader.Events.Add(instance);
|
||||
Config.Logger.Log($"[EVENT] Loaded external {type.FullName}!", Others.LogLevel.INFO);
|
||||
Events.Add(instance);
|
||||
Config.Logger.Log($"[EVENT] Loaded external {type.FullName}!", LogLevel.INFO);
|
||||
}
|
||||
else if (type.IsClass && typeof(DBCommand).IsAssignableFrom(type))
|
||||
{
|
||||
var instance = (DBCommand)Activator.CreateInstance(type);
|
||||
PluginLoader.Commands.Add(instance);
|
||||
Config.Logger.Log($"[CMD] Instance: {type.FullName} loaded !", Others.LogLevel.INFO);
|
||||
Commands.Add(instance);
|
||||
Config.Logger.Log($"[CMD] Instance: {type.FullName} loaded !", LogLevel.INFO);
|
||||
}
|
||||
else if (type.IsClass && typeof(DBSlashCommand).IsAssignableFrom(type))
|
||||
{
|
||||
var instance = (DBSlashCommand)Activator.CreateInstance(type);
|
||||
SlashCommandBuilder builder = new SlashCommandBuilder();
|
||||
var builder = new SlashCommandBuilder();
|
||||
builder.WithName(instance.Name);
|
||||
builder.WithDescription(instance.Description);
|
||||
builder.WithDMPermission(instance.canUseDM);
|
||||
builder.Options = instance.Options;
|
||||
|
||||
await client.CreateGlobalApplicationCommandAsync(builder.Build());
|
||||
PluginLoader.SlashCommands.Add(instance);
|
||||
Config.Logger.Log($"[SLASH] Instance: {type.FullName} loaded !", Others.LogLevel.INFO);
|
||||
|
||||
SlashCommands.Add(instance);
|
||||
Config.Logger.Log($"[SLASH] Instance: {type.FullName} loaded !", LogLevel.INFO);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using PluginManager.Others;
|
||||
|
||||
namespace PluginManager.Online.Helpers;
|
||||
@@ -19,10 +18,11 @@ internal static class OnlineFunctions
|
||||
/// <param name="progress">The <see cref="IProgress{T}" /> that is used to track the download progress</param>
|
||||
/// <param name="cancellation">The cancellation token</param>
|
||||
/// <returns></returns>
|
||||
internal static async Task DownloadFileAsync(this HttpClient client, string url, Stream destination,
|
||||
IProgress<float>? progress = null,
|
||||
IProgress<long>? downloadedBytes = null, int bufferSize = 81920,
|
||||
CancellationToken cancellation = default)
|
||||
internal static async Task DownloadFileAsync(
|
||||
this HttpClient client, string url, Stream destination,
|
||||
IProgress<float>? progress = null,
|
||||
IProgress<long>? downloadedBytes = null, int bufferSize = 81920,
|
||||
CancellationToken cancellation = default)
|
||||
{
|
||||
using (var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, cancellation))
|
||||
{
|
||||
@@ -40,11 +40,12 @@ internal static class OnlineFunctions
|
||||
|
||||
// Convert absolute progress (bytes downloaded) into relative progress (0% - 100%)
|
||||
var relativeProgress = new Progress<long>(totalBytes =>
|
||||
{
|
||||
progress?.Report((float)totalBytes / contentLength.Value * 100);
|
||||
downloadedBytes?.Report(totalBytes);
|
||||
}
|
||||
);
|
||||
{
|
||||
progress?.Report((float)totalBytes / contentLength.Value *
|
||||
100);
|
||||
downloadedBytes?.Report(totalBytes);
|
||||
}
|
||||
);
|
||||
|
||||
// Use extension method to report progress while downloading
|
||||
await download.CopyToOtherStreamAsync(destination, bufferSize, relativeProgress, cancellation);
|
||||
@@ -64,4 +65,4 @@ internal static class OnlineFunctions
|
||||
using var client = new HttpClient();
|
||||
return await client.GetStringAsync(url, cancellation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,24 +4,6 @@ namespace PluginManager.Online.Helpers;
|
||||
|
||||
public class VersionString
|
||||
{
|
||||
private bool Equals(VersionString other)
|
||||
{
|
||||
return PackageCheckVersion == other.PackageCheckVersion && PackageMainVersion == other.PackageMainVersion && PackageVersionID == other.PackageVersionID;
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
if (ReferenceEquals(null, obj)) return false;
|
||||
if (ReferenceEquals(this, obj)) return true;
|
||||
if (obj.GetType() != this.GetType()) return false;
|
||||
return Equals((VersionString)obj);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(PackageCheckVersion, PackageMainVersion, PackageVersionID);
|
||||
}
|
||||
|
||||
public int PackageCheckVersion;
|
||||
public int PackageMainVersion;
|
||||
public int PackageVersionID;
|
||||
@@ -33,20 +15,21 @@ public class VersionString
|
||||
{
|
||||
if (data.Length == 3)
|
||||
{
|
||||
PackageVersionID = int.Parse(data[0]);
|
||||
PackageMainVersion = int.Parse(data[1]);
|
||||
PackageVersionID = int.Parse(data[0]);
|
||||
PackageMainVersion = int.Parse(data[1]);
|
||||
PackageCheckVersion = int.Parse(data[2]);
|
||||
}
|
||||
else if (data.Length == 4)
|
||||
{
|
||||
// ignore the first item data[0]
|
||||
PackageVersionID = int.Parse(data[1]);
|
||||
PackageMainVersion = int.Parse(data[2]);
|
||||
PackageVersionID = int.Parse(data[1]);
|
||||
PackageMainVersion = int.Parse(data[2]);
|
||||
PackageCheckVersion = int.Parse(data[3]);
|
||||
}
|
||||
else
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid version string");
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -55,6 +38,25 @@ public class VersionString
|
||||
}
|
||||
}
|
||||
|
||||
private bool Equals(VersionString other)
|
||||
{
|
||||
return PackageCheckVersion == other.PackageCheckVersion && PackageMainVersion == other.PackageMainVersion &&
|
||||
PackageVersionID == other.PackageVersionID;
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
if (ReferenceEquals(null, obj)) return false;
|
||||
if (ReferenceEquals(this, obj)) return true;
|
||||
if (obj.GetType() != GetType()) return false;
|
||||
return Equals((VersionString)obj);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(PackageCheckVersion, PackageMainVersion, PackageVersionID);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "{PackageID: " + PackageVersionID + ", PackageVersion: " + PackageMainVersion +
|
||||
@@ -112,4 +114,4 @@ public class VersionString
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using PluginManager.Online.Helpers;
|
||||
using PluginManager.Others;
|
||||
|
||||
using OperatingSystem = PluginManager.Others.OperatingSystem;
|
||||
|
||||
namespace PluginManager.Online;
|
||||
@@ -18,7 +16,7 @@ public class PluginsManager
|
||||
/// <param name="vlink">The link to the file where all plugin versions are stored</param>
|
||||
public PluginsManager(string plink, string vlink)
|
||||
{
|
||||
PluginsLink = plink;
|
||||
PluginsLink = plink;
|
||||
VersionsLink = vlink;
|
||||
}
|
||||
|
||||
@@ -26,7 +24,8 @@ public class PluginsManager
|
||||
/// The URL of the server
|
||||
/// </summary>
|
||||
public string PluginsLink { get; }
|
||||
public string VersionsLink {get; }
|
||||
|
||||
public string VersionsLink { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The method to load all plugins
|
||||
@@ -36,11 +35,11 @@ public class PluginsManager
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = await ServerCom.ReadTextFromURL(PluginsLink);
|
||||
var list = await ServerCom.ReadTextFromURL(PluginsLink);
|
||||
var lines = list.ToArray();
|
||||
|
||||
var data = new List<string[]>();
|
||||
var op = Functions.GetOperatingSystem();
|
||||
var op = Functions.GetOperatingSystem();
|
||||
|
||||
var len = lines.Length;
|
||||
for (var i = 0; i < len; i++)
|
||||
@@ -58,7 +57,7 @@ public class PluginsManager
|
||||
display[2] = content[2];
|
||||
display[3] =
|
||||
(await GetVersionOfPackageFromWeb(content[0]) ?? new VersionString("0.0.0"))
|
||||
.ToShortString();
|
||||
.ToShortString();
|
||||
data.Add(display);
|
||||
}
|
||||
}
|
||||
@@ -71,7 +70,7 @@ public class PluginsManager
|
||||
display[2] = content[2];
|
||||
display[3] =
|
||||
(await GetVersionOfPackageFromWeb(content[0]) ?? new VersionString("0.0.0"))
|
||||
.ToShortString();
|
||||
.ToShortString();
|
||||
data.Add(display);
|
||||
}
|
||||
}
|
||||
@@ -83,7 +82,8 @@ public class PluginsManager
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Config.Logger.Log("Failed to execute command: listplugs\nReason: " + exception.Message, this, LogLevel.ERROR);
|
||||
Config.Logger.Log("Failed to execute command: listplugs\nReason: " + exception.Message, this,
|
||||
LogLevel.ERROR);
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -97,14 +97,15 @@ public class PluginsManager
|
||||
if (item.StartsWith("#"))
|
||||
continue;
|
||||
|
||||
string[] split = item.Split(',');
|
||||
var split = item.Split(',');
|
||||
if (split[0] == pakName)
|
||||
{
|
||||
Console.WriteLine("Searched for " + pakName + " and found " + split[1] + " as version.\nUsed url: " + VersionsLink);
|
||||
Console.WriteLine("Searched for " + pakName + " and found " + split[1] + " as version.\nUsed url: " +
|
||||
VersionsLink);
|
||||
return new VersionString(split[1]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -117,9 +118,9 @@ public class PluginsManager
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = await ServerCom.ReadTextFromURL(PluginsLink);
|
||||
var list = await ServerCom.ReadTextFromURL(PluginsLink);
|
||||
var lines = list.ToArray();
|
||||
var len = lines.Length;
|
||||
var len = lines.Length;
|
||||
for (var i = 0; i < len; i++)
|
||||
{
|
||||
var contents = lines[i].Split(',');
|
||||
@@ -135,7 +136,8 @@ public class PluginsManager
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Config.Logger.Log("Failed to execute command: listplugs\nReason: " + exception.Message, this, LogLevel.ERROR);
|
||||
Config.Logger.Log("Failed to execute command: listplugs\nReason: " + exception.Message, this,
|
||||
LogLevel.ERROR);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using PluginManager.Online.Helpers;
|
||||
using PluginManager.Others;
|
||||
|
||||
namespace PluginManager.Online;
|
||||
|
||||
@@ -21,7 +18,7 @@ public static class ServerCom
|
||||
public static async Task<List<string>> ReadTextFromURL(string link)
|
||||
{
|
||||
var response = await OnlineFunctions.DownloadStringAsync(link);
|
||||
var lines = response.Split('\n');
|
||||
var lines = response.Split('\n');
|
||||
return lines.ToList();
|
||||
}
|
||||
|
||||
@@ -32,8 +29,9 @@ public static class ServerCom
|
||||
/// <param name="location">The location where to store the downloaded data</param>
|
||||
/// <param name="progress">The <see cref="IProgress{T}" /> to track the download</param>
|
||||
/// <returns></returns>
|
||||
public static async Task DownloadFileAsync(string URL, string location, IProgress<float> progress,
|
||||
IProgress<long>? downloadedBytes)
|
||||
public static async Task DownloadFileAsync(
|
||||
string URL, string location, IProgress<float> progress,
|
||||
IProgress<long>? downloadedBytes)
|
||||
{
|
||||
using (var client = new HttpClient())
|
||||
{
|
||||
@@ -50,4 +48,4 @@ public static class ServerCom
|
||||
{
|
||||
await DownloadFileAsync(URl, location, progress, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,57 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using PluginManager.Interfaces;
|
||||
using PluginManager.Loaders;
|
||||
|
||||
namespace PluginManager.Others.Actions
|
||||
namespace PluginManager.Others.Actions;
|
||||
|
||||
public class InternalActionManager
|
||||
{
|
||||
public class InternalActionManager
|
||||
public Dictionary<string, ICommandAction> Actions = new();
|
||||
public ActionsLoader loader;
|
||||
|
||||
public InternalActionManager(string path, string extension)
|
||||
{
|
||||
public ActionsLoader loader;
|
||||
public Dictionary<string, ICommandAction> Actions = new Dictionary<string, ICommandAction>();
|
||||
loader = new ActionsLoader(path, extension);
|
||||
}
|
||||
|
||||
public InternalActionManager(string path, string extension)
|
||||
public async Task Initialize()
|
||||
{
|
||||
loader.ActionLoadedEvent += OnActionLoaded;
|
||||
var m_actions = await loader.Load();
|
||||
if (m_actions == null) return;
|
||||
foreach (var action in m_actions)
|
||||
Actions.Add(action.ActionName, action);
|
||||
}
|
||||
|
||||
private void OnActionLoaded(string name, string typeName, bool success, Exception? e)
|
||||
{
|
||||
if (!success)
|
||||
{
|
||||
loader = new ActionsLoader(path, extension);
|
||||
Config.Logger.Error(e);
|
||||
return;
|
||||
}
|
||||
|
||||
public async Task Initialize()
|
||||
Config.Logger.Log($"Action {name} loaded successfully", typeName);
|
||||
}
|
||||
|
||||
public async Task<string> Execute(string actionName, params string[]? args)
|
||||
{
|
||||
if (!Actions.ContainsKey(actionName))
|
||||
{
|
||||
loader.ActionLoadedEvent += OnActionLoaded;
|
||||
var m_actions = await loader.Load();
|
||||
if(m_actions == null) return;
|
||||
foreach(var action in m_actions)
|
||||
Actions.Add(action.ActionName, action);
|
||||
Config.Logger.Log($"Action {actionName} not found", "InternalActionManager", LogLevel.WARNING);
|
||||
return "Action not found";
|
||||
}
|
||||
|
||||
private void OnActionLoaded(string name, string typeName, bool success, Exception? e)
|
||||
try
|
||||
{
|
||||
if (!success)
|
||||
{
|
||||
Config.Logger.Error(e);
|
||||
return;
|
||||
}
|
||||
|
||||
Config.Logger.Log($"Action {name} loaded successfully", typeName, LogLevel.INFO);
|
||||
await Actions[actionName].Execute(args);
|
||||
return "Action executed";
|
||||
}
|
||||
|
||||
public async Task<string> Execute(string actionName, params string[]? args)
|
||||
catch (Exception e)
|
||||
{
|
||||
if (!Actions.ContainsKey(actionName))
|
||||
{
|
||||
Config.Logger.Log($"Action {actionName} not found", "InternalActionManager", LogLevel.WARNING);
|
||||
return "Action not found";
|
||||
}
|
||||
|
||||
try{
|
||||
await Actions[actionName].Execute(args);
|
||||
return "Action executed";
|
||||
}catch(Exception e){
|
||||
Config.Logger.Log(e.Message, "InternalActionManager", LogLevel.ERROR);
|
||||
return e.Message;
|
||||
}
|
||||
Config.Logger.Log(e.Message, "InternalActionManager", LogLevel.ERROR);
|
||||
return e.Message;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,136 +4,138 @@ using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PluginManager.Others
|
||||
namespace PluginManager.Others;
|
||||
|
||||
public static class ArchiveManager
|
||||
{
|
||||
public static class ArchiveManager
|
||||
private static string? archiveFolder;
|
||||
public static bool isInitialized { get; private set; }
|
||||
|
||||
public static void Initialize()
|
||||
{
|
||||
public static bool isInitialized { get; private set; }
|
||||
private static string? archiveFolder;
|
||||
if (isInitialized) throw new Exception("ArchiveManager is already initialized");
|
||||
|
||||
public static void Initialize()
|
||||
if (!Config.Data.ContainsKey("ArchiveFolder"))
|
||||
Config.Data["ArchiveFolder"] = "./Data/PAKS/";
|
||||
|
||||
archiveFolder = Config.Data["ArchiveFolder"];
|
||||
|
||||
isInitialized = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read data from a file that is inside an archive (ZIP format)
|
||||
/// </summary>
|
||||
/// <param name="FileName">The file name that is inside the archive or its full path</param>
|
||||
/// <param name="archFile">The archive location from the PAKs folder</param>
|
||||
/// <returns>A string that represents the content of the file or null if the file does not exists or it has no content</returns>
|
||||
public static async Task<string?> ReadFromPakAsync(string FileName, string archFile)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("ArchiveManager is not initialized");
|
||||
archFile = archiveFolder + archFile;
|
||||
if (!File.Exists(archFile))
|
||||
throw new Exception("Failed to load file !");
|
||||
|
||||
try
|
||||
{
|
||||
if (isInitialized) throw new Exception("ArchiveManager is already initialized");
|
||||
|
||||
if (!Config.Data.ContainsKey("ArchiveFolder"))
|
||||
Config.Data["ArchiveFolder"] = "./Data/PAKS/";
|
||||
|
||||
archiveFolder = Config.Data["ArchiveFolder"];
|
||||
|
||||
isInitialized = true;
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Read data from a file that is inside an archive (ZIP format)
|
||||
/// </summary>
|
||||
/// <param name="FileName">The file name that is inside the archive or its full path</param>
|
||||
/// <param name="archFile">The archive location from the PAKs folder</param>
|
||||
/// <returns>A string that represents the content of the file or null if the file does not exists or it has no content</returns>
|
||||
public static async Task<string?> ReadFromPakAsync(string FileName, string archFile)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("ArchiveManager is not initialized");
|
||||
archFile = archiveFolder + archFile;
|
||||
if (!File.Exists(archFile))
|
||||
throw new Exception("Failed to load file !");
|
||||
|
||||
try
|
||||
{
|
||||
string? textValue = null;
|
||||
using (var fs = new FileStream(archFile, FileMode.Open))
|
||||
string? textValue = null;
|
||||
using (var fs = new FileStream(archFile, FileMode.Open))
|
||||
using (var zip = new ZipArchive(fs, ZipArchiveMode.Read))
|
||||
{
|
||||
foreach (var entry in zip.Entries)
|
||||
if (entry.Name == FileName || entry.FullName == FileName)
|
||||
using (var s = entry.Open())
|
||||
using (var reader = new StreamReader(s))
|
||||
{
|
||||
textValue = await reader.ReadToEndAsync();
|
||||
reader.Close();
|
||||
s.Close();
|
||||
fs.Close();
|
||||
}
|
||||
using (var reader = new StreamReader(s))
|
||||
{
|
||||
textValue = await reader.ReadToEndAsync();
|
||||
reader.Close();
|
||||
s.Close();
|
||||
fs.Close();
|
||||
}
|
||||
}
|
||||
|
||||
return textValue;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Config.Logger.Log(ex.Message, "Archive Manager", LogLevel.ERROR); // Write the error to a file
|
||||
await Task.Delay(100);
|
||||
return await ReadFromPakAsync(FileName, archFile);
|
||||
}
|
||||
return textValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extract zip to location
|
||||
/// </summary>
|
||||
/// <param name="zip">The zip location</param>
|
||||
/// <param name="folder">The target location</param>
|
||||
/// <param name="progress">The progress that is updated as a file is processed</param>
|
||||
/// <param name="type">The type of progress</param>
|
||||
/// <returns></returns>
|
||||
public static async Task ExtractArchive(string zip, string folder, IProgress<float> progress,
|
||||
UnzipProgressType type)
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("ArchiveManager is not initialized");
|
||||
Directory.CreateDirectory(folder);
|
||||
using (var archive = ZipFile.OpenRead(zip))
|
||||
Config.Logger.Log(ex.Message, "Archive Manager", LogLevel.ERROR); // Write the error to a file
|
||||
await Task.Delay(100);
|
||||
return await ReadFromPakAsync(FileName, archFile);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extract zip to location
|
||||
/// </summary>
|
||||
/// <param name="zip">The zip location</param>
|
||||
/// <param name="folder">The target location</param>
|
||||
/// <param name="progress">The progress that is updated as a file is processed</param>
|
||||
/// <param name="type">The type of progress</param>
|
||||
/// <returns></returns>
|
||||
public static async Task ExtractArchive(
|
||||
string zip, string folder, IProgress<float> progress,
|
||||
UnzipProgressType type)
|
||||
{
|
||||
if (!isInitialized) throw new Exception("ArchiveManager is not initialized");
|
||||
Directory.CreateDirectory(folder);
|
||||
using (var archive = ZipFile.OpenRead(zip))
|
||||
{
|
||||
if (type == UnzipProgressType.PERCENTAGE_FROM_NUMBER_OF_FILES)
|
||||
{
|
||||
if (type == UnzipProgressType.PERCENTAGE_FROM_NUMBER_OF_FILES)
|
||||
var totalZIPFiles = archive.Entries.Count();
|
||||
var currentZIPFile = 0;
|
||||
foreach (var entry in archive.Entries)
|
||||
{
|
||||
var totalZIPFiles = archive.Entries.Count();
|
||||
var currentZIPFile = 0;
|
||||
foreach (var entry in archive.Entries)
|
||||
{
|
||||
if (entry.FullName.EndsWith("/")) // it is a folder
|
||||
Directory.CreateDirectory(Path.Combine(folder, entry.FullName));
|
||||
|
||||
else
|
||||
try
|
||||
{
|
||||
entry.ExtractToFile(Path.Combine(folder, entry.FullName), true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Config.Logger.Log($"Failed to extract {entry.Name}. Exception: {ex.Message}", "Archive Manager", LogLevel.ERROR);
|
||||
}
|
||||
|
||||
currentZIPFile++;
|
||||
await Task.Delay(10);
|
||||
if (progress != null)
|
||||
progress.Report((float)currentZIPFile / totalZIPFiles * 100);
|
||||
}
|
||||
}
|
||||
else if (type == UnzipProgressType.PERCENTAGE_FROM_TOTAL_SIZE)
|
||||
{
|
||||
ulong zipSize = 0;
|
||||
|
||||
foreach (var entry in archive.Entries)
|
||||
zipSize += (ulong)entry.CompressedLength;
|
||||
|
||||
ulong currentSize = 0;
|
||||
foreach (var entry in archive.Entries)
|
||||
{
|
||||
if (entry.FullName.EndsWith("/"))
|
||||
{
|
||||
Directory.CreateDirectory(Path.Combine(folder, entry.FullName));
|
||||
continue;
|
||||
}
|
||||
if (entry.FullName.EndsWith("/")) // it is a folder
|
||||
Directory.CreateDirectory(Path.Combine(folder, entry.FullName));
|
||||
|
||||
else
|
||||
try
|
||||
{
|
||||
entry.ExtractToFile(Path.Combine(folder, entry.FullName), true);
|
||||
currentSize += (ulong)entry.CompressedLength;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Config.Logger.Log($"Failed to extract {entry.Name}. Exception: {ex.Message}", "Archive Manager", LogLevel.ERROR);
|
||||
Config.Logger.Log($"Failed to extract {entry.Name}. Exception: {ex.Message}",
|
||||
"Archive Manager", LogLevel.ERROR);
|
||||
}
|
||||
|
||||
await Task.Delay(10);
|
||||
if (progress != null)
|
||||
progress.Report((float)currentSize / zipSize * 100);
|
||||
currentZIPFile++;
|
||||
await Task.Delay(10);
|
||||
if (progress != null)
|
||||
progress.Report((float)currentZIPFile / totalZIPFiles * 100);
|
||||
}
|
||||
}
|
||||
else if (type == UnzipProgressType.PERCENTAGE_FROM_TOTAL_SIZE)
|
||||
{
|
||||
ulong zipSize = 0;
|
||||
|
||||
foreach (var entry in archive.Entries)
|
||||
zipSize += (ulong)entry.CompressedLength;
|
||||
|
||||
ulong currentSize = 0;
|
||||
foreach (var entry in archive.Entries)
|
||||
{
|
||||
if (entry.FullName.EndsWith("/"))
|
||||
{
|
||||
Directory.CreateDirectory(Path.Combine(folder, entry.FullName));
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
entry.ExtractToFile(Path.Combine(folder, entry.FullName), true);
|
||||
currentSize += (ulong)entry.CompressedLength;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Config.Logger.Log($"Failed to extract {entry.Name}. Exception: {ex.Message}",
|
||||
"Archive Manager", LogLevel.ERROR);
|
||||
}
|
||||
|
||||
await Task.Delay(10);
|
||||
if (progress != null)
|
||||
progress.Report((float)currentSize / zipSize * 100);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,27 +1,20 @@
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PluginManager.Others
|
||||
namespace PluginManager.Others;
|
||||
|
||||
public class DBCommandExecutingArguments
|
||||
{
|
||||
public class DBCommandExecutingArguments
|
||||
public DBCommandExecutingArguments(
|
||||
SocketCommandContext context, string cleanContent, string commandUsed, string[]? arguments)
|
||||
{
|
||||
public SocketCommandContext context { get; init; }
|
||||
public string cleanContent { get; init; }
|
||||
public string commandUsed { get;init; }
|
||||
public string[]? arguments { get;init; }
|
||||
|
||||
public DBCommandExecutingArguments(SocketCommandContext context, string cleanContent, string commandUsed, string[]? arguments)
|
||||
{
|
||||
this.context = context;
|
||||
this.cleanContent = cleanContent;
|
||||
this.commandUsed = commandUsed;
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
this.context = context;
|
||||
this.cleanContent = cleanContent;
|
||||
this.commandUsed = commandUsed;
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
public SocketCommandContext context { get; init; }
|
||||
public string cleanContent { get; init; }
|
||||
public string commandUsed { get; init; }
|
||||
public string[]? arguments { get; init; }
|
||||
}
|
||||
|
||||
@@ -39,4 +39,4 @@ public enum InternalActionRunType
|
||||
{
|
||||
ON_STARTUP,
|
||||
ON_CALL
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,15 @@ public static class Functions
|
||||
/// </summary>
|
||||
public static readonly string dataFolder = @"./Data/Resources/";
|
||||
|
||||
public static Color RandomColor
|
||||
{
|
||||
get
|
||||
{
|
||||
var random = new Random();
|
||||
return new Color(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the Operating system you are runnin on
|
||||
/// </summary>
|
||||
@@ -43,9 +52,10 @@ public static class Functions
|
||||
/// <exception cref="ArgumentOutOfRangeException">Triggered if <paramref name="bufferSize" /> is less then or equal to 0</exception>
|
||||
/// <exception cref="InvalidOperationException">Triggered if <paramref name="stream" /> is not readable</exception>
|
||||
/// <exception cref="ArgumentException">Triggered in <paramref name="destination" /> is not writable</exception>
|
||||
public static async Task CopyToOtherStreamAsync(this Stream stream, Stream destination, int bufferSize,
|
||||
IProgress<long>? progress = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
public static async Task CopyToOtherStreamAsync(
|
||||
this Stream stream, Stream destination, int bufferSize,
|
||||
IProgress<long>? progress = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (stream == null) throw new ArgumentNullException(nameof(stream));
|
||||
if (destination == null) throw new ArgumentNullException(nameof(destination));
|
||||
@@ -54,10 +64,11 @@ public static class Functions
|
||||
if (!destination.CanWrite)
|
||||
throw new ArgumentException("Destination stream is not writable", nameof(destination));
|
||||
|
||||
var buffer = new byte[bufferSize];
|
||||
var buffer = new byte[bufferSize];
|
||||
long totalBytesRead = 0;
|
||||
int bytesRead;
|
||||
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
|
||||
int bytesRead;
|
||||
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken)
|
||||
.ConfigureAwait(false)) != 0)
|
||||
{
|
||||
await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
|
||||
totalBytesRead += bytesRead;
|
||||
@@ -102,9 +113,9 @@ public static class Functions
|
||||
return (obj ?? default)!;
|
||||
}
|
||||
|
||||
public static T SelectRandomValueOf<T> ()
|
||||
public static T SelectRandomValueOf<T>()
|
||||
{
|
||||
var enums = Enum.GetValues(typeof(T));
|
||||
var enums = Enum.GetValues(typeof(T));
|
||||
var random = new Random();
|
||||
return (T)enums.GetValue(random.Next(enums.Length));
|
||||
}
|
||||
@@ -114,13 +125,4 @@ public static class Functions
|
||||
Random random = new();
|
||||
return values[random.Next(values.Length)];
|
||||
}
|
||||
|
||||
public static Discord.Color RandomColor
|
||||
{
|
||||
get
|
||||
{
|
||||
Random random = new Random();
|
||||
return new Discord.Color(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,53 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PluginManager.Others.Logger
|
||||
namespace PluginManager.Others.Logger;
|
||||
|
||||
public class DBLogger
|
||||
{
|
||||
public class DBLogger
|
||||
public delegate void LogHandler(string message, LogLevel logType);
|
||||
|
||||
private readonly string _errFolder;
|
||||
|
||||
private readonly string _logFolder;
|
||||
private readonly List<LogMessage> ErrorHistory = new();
|
||||
private readonly List<LogMessage> LogHistory = new();
|
||||
|
||||
public DBLogger()
|
||||
{
|
||||
|
||||
private List<LogMessage> LogHistory = new List<LogMessage>();
|
||||
private List<LogMessage> ErrorHistory = new List<LogMessage>();
|
||||
_logFolder = Config.Data["LogFolder"];
|
||||
_errFolder = Config.Data["ErrorFolder"];
|
||||
}
|
||||
|
||||
public IReadOnlyList<LogMessage> Logs => LogHistory;
|
||||
public IReadOnlyList<LogMessage> Errors => ErrorHistory;
|
||||
public IReadOnlyList<LogMessage> Logs => LogHistory;
|
||||
public IReadOnlyList<LogMessage> Errors => ErrorHistory;
|
||||
|
||||
public delegate void LogHandler(string message, LogLevel logType);
|
||||
public event LogHandler LogEvent;
|
||||
public event LogHandler LogEvent;
|
||||
|
||||
private string _logFolder;
|
||||
private string _errFolder;
|
||||
public void Log(string message, string sender = "unknown", LogLevel type = LogLevel.INFO)
|
||||
{
|
||||
Log(new LogMessage(message, type, sender));
|
||||
}
|
||||
|
||||
public DBLogger()
|
||||
{
|
||||
_logFolder = Config.Data["LogFolder"];
|
||||
_errFolder = Config.Data["ErrorFolder"];
|
||||
}
|
||||
public void Error(Exception? e)
|
||||
{
|
||||
Log(e.Message, e.Source, LogLevel.ERROR);
|
||||
}
|
||||
|
||||
public void Log(string message, string sender = "unknown", LogLevel type = LogLevel.INFO) => Log(new LogMessage(message, type, sender));
|
||||
public void Error(Exception? e) => Log(e.Message, e.Source, LogLevel.ERROR);
|
||||
public void Log(LogMessage message)
|
||||
{
|
||||
if (LogEvent is not null)
|
||||
LogEvent?.Invoke(message.Message, message.Type);
|
||||
|
||||
public void Log(LogMessage message)
|
||||
{
|
||||
if(LogEvent is not null)
|
||||
LogEvent?.Invoke(message.Message, message.Type);
|
||||
if (message.Type != LogLevel.ERROR && message.Type != LogLevel.CRITICAL)
|
||||
LogHistory.Add(message);
|
||||
else
|
||||
ErrorHistory.Add(message);
|
||||
}
|
||||
|
||||
if (message.Type != LogLevel.ERROR && message.Type != LogLevel.CRITICAL)
|
||||
LogHistory.Add(message);
|
||||
else
|
||||
ErrorHistory.Add(message);
|
||||
}
|
||||
public void Log(string message, object sender, LogLevel type = LogLevel.NONE)
|
||||
{
|
||||
Log(message, sender.GetType().Name, type);
|
||||
}
|
||||
|
||||
public void Log(string message, object sender, LogLevel type = LogLevel.NONE) => Log(message, sender.GetType().Name, type);
|
||||
|
||||
public async void SaveToFile()
|
||||
{
|
||||
await Functions.SaveToJsonFile(_logFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json", LogHistory);
|
||||
await Functions.SaveToJsonFile(_errFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json", ErrorHistory);
|
||||
}
|
||||
public async void SaveToFile()
|
||||
{
|
||||
await Functions.SaveToJsonFile(_logFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json",
|
||||
LogHistory);
|
||||
await Functions.SaveToJsonFile(_errFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json",
|
||||
ErrorHistory);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,47 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PluginManager.Others.Logger
|
||||
namespace PluginManager.Others.Logger;
|
||||
|
||||
public class LogMessage
|
||||
{
|
||||
public class LogMessage
|
||||
public LogMessage(string message, LogLevel type)
|
||||
{
|
||||
public string Message { get; set; }
|
||||
public LogLevel Type { get; set; }
|
||||
public string Time { get; set; }
|
||||
public string Sender { get; set; }
|
||||
public LogMessage(string message, LogLevel type)
|
||||
{
|
||||
Message = message;
|
||||
Type = type;
|
||||
Time = DateTime.Now.ToString("HH:mm:ss");
|
||||
}
|
||||
Message = message;
|
||||
Type = type;
|
||||
Time = DateTime.Now.ToString("HH:mm:ss");
|
||||
}
|
||||
|
||||
public LogMessage(string message, LogLevel type, string sender) : this(message, type)
|
||||
{
|
||||
Sender = sender;
|
||||
}
|
||||
public LogMessage(string message, LogLevel type, string sender) : this(message, type)
|
||||
{
|
||||
Sender = sender;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"[{Time}] {Message}";
|
||||
}
|
||||
public string Message { get; set; }
|
||||
public LogLevel Type { get; set; }
|
||||
public string Time { get; set; }
|
||||
public string Sender { get; set; }
|
||||
|
||||
public static explicit operator LogMessage(string message)
|
||||
{
|
||||
return new LogMessage(message, LogLevel.INFO);
|
||||
}
|
||||
public override string ToString()
|
||||
{
|
||||
return $"[{Time}] {Message}";
|
||||
}
|
||||
|
||||
public static explicit operator LogMessage((string message, LogLevel type) tuple)
|
||||
{
|
||||
return new LogMessage(tuple.message, tuple.type);
|
||||
}
|
||||
public static explicit operator LogMessage(string message)
|
||||
{
|
||||
return new LogMessage(message, LogLevel.INFO);
|
||||
}
|
||||
|
||||
public static explicit operator LogMessage((string message, LogLevel type, string sender) tuple)
|
||||
{
|
||||
return new LogMessage(tuple.message, tuple.type, tuple.sender);
|
||||
}
|
||||
public static explicit operator LogMessage((string message, LogLevel type) tuple)
|
||||
{
|
||||
return new LogMessage(tuple.message, tuple.type);
|
||||
}
|
||||
|
||||
public static explicit operator LogMessage((string message, LogLevel type, string sender) tuple)
|
||||
{
|
||||
return new LogMessage(tuple.message, tuple.type, tuple.sender);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System.Linq;
|
||||
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
|
||||
@@ -62,4 +61,4 @@ public static class DiscordPermissions
|
||||
{
|
||||
return isAdmin((SocketGuildUser)user);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<DebugType>none</DebugType>
|
||||
<DebugSymbols>false</DebugSymbols>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<None Remove="BlankWindow1.xaml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Discord.Net" Version="3.10.0" />
|
||||
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.118" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<DebugType>none</DebugType>
|
||||
<DebugSymbols>false</DebugSymbols>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<None Remove="BlankWindow1.xaml"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Discord.Net" Version="3.10.0"/>
|
||||
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.118"/>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user