Added autoinstall for modules

This commit is contained in:
2024-07-14 21:24:49 +03:00
parent 3f8590b8f3
commit 13900bb3f3
10 changed files with 153 additions and 40 deletions

View File

@@ -15,6 +15,7 @@ using DiscordBotCore.Interfaces.Logger;
using DiscordBotCore.Modules;
using System.Linq;
using System.Collections.Immutable;
using System.Diagnostics;
namespace DiscordBotCore
@@ -46,7 +47,28 @@ namespace DiscordBotCore
public SettingsDictionary<string, string> ApplicationEnvironmentVariables { get; private set; }
public InternalActionManager InternalActionManager { get; private set; }
public ILogger Logger => _ModuleManager.GetModule<ILogger>();
public ILogger Logger
{
get
{
try { return _ModuleManager.GetModule<ILogger>(); }
catch (ModuleNotFoundException<ILogger> ex)
{
Console.WriteLine("No logger found");
Console.WriteLine("Not having a valid logger is NOT an option. The default module will be downloaded from the official repo.");
Console.WriteLine("Install the default one ? [y/n]");
ConsoleKey response = Console.ReadKey().Key;
if (response is ConsoleKey.Y)
{
Process.Start("DiscordBot", "--module-install LoggerModule");
}
Environment.Exit(0);
return null!;
}
}
}
public IPluginManager PluginManager { get; private set; }
public Bot.App DiscordBotClient { get; internal set; }
@@ -61,6 +83,7 @@ namespace DiscordBotCore
Directory.CreateDirectory(_PluginsFolder);
Directory.CreateDirectory(_ArchivesFolder);
Directory.CreateDirectory(_LogsFolder);
Directory.CreateDirectory(_ModuleFolder);
CurrentApplication.ApplicationEnvironmentVariables = new SettingsDictionary<string, string>(_ConfigFile);
bool result = await CurrentApplication.ApplicationEnvironmentVariables.LoadFromFile();

View File

@@ -14,16 +14,13 @@ namespace DiscordBotCore.Interfaces.Logger
string LogMessageFormat { get; set; }
event EventHandler<FormattedMessage> OnFormattedLog;
event EventHandler<ILogMessage> OnRawLog;
void Log(ILogMessage message);
void Log(ILogMessage message, string format);
void Log(string message);
void Log(string message, LogType logType);
void Log(string message, LogType logType, string format);
void Log(string message, object Sender);
void Log(string message, object Sender, LogType type);
void LogException(Exception exception, object Sender, bool logFullStack = false);
void SetOutFunction(Action<string> outFunction);
}
}

View File

@@ -6,7 +6,6 @@ using System.Threading.Tasks;
using DiscordBotCore.Interfaces.Modules;
using System.Reflection;
using Discord.Commands;
namespace DiscordBotCore.Loaders
{

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DiscordBotCore.Online;
namespace DiscordBotCore.Modules
{
public class ModuleDownloader
{
private string _moduleName;
private readonly string _baseUrl = "https://raw.githubusercontent.com/andreitdr/SethPlugins/tests/Modules/";
private readonly string _moduleFolder = "./Data/Modules";
public ModuleDownloader(string moduleName)
{
_moduleName = moduleName;
}
public async Task DownloadModule(IProgress<float> progressToWrite)
{
Directory.CreateDirectory(_moduleFolder);
string url = _baseUrl + _moduleName + ".dll";
await ServerCom.DownloadFileAsync(url, _moduleFolder + "/" + _moduleName + ".dll", progressToWrite);
}
}
}

View File

@@ -6,6 +6,7 @@ using System.Threading.Tasks;
using DiscordBotCore.Interfaces.Logger;
using DiscordBotCore.Interfaces.Modules;
using DiscordBotCore.Loaders;
using DiscordBotCore.Others.Exceptions;
namespace DiscordBotCore.Modules
{
@@ -23,7 +24,10 @@ namespace DiscordBotCore.Modules
public T GetModule<T>() where T : IBaseModule
{
if(!LoadedModules.ContainsKey(typeof(T)))
throw new Exception($"No module loaded with this signature: {nameof(T)}");
throw new ModuleNotFoundException<T>();
if (!LoadedModules[typeof(T)].Any())
throw new ModuleNotFoundException<T>();
IModule<T> module = (IModule<T>)LoadedModules[typeof(T)][0];
return module.Module;

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DiscordBotCore.Others.Exceptions
{
internal class ModuleNotFoundException<T> : Exception
{
private Type _type = typeof(T);
public ModuleNotFoundException() : base($"No module loaded with this signature: {typeof(T)}")
{
}
}
}