Added autoinstall for modules
This commit is contained in:
30
DiscordBotCore/Modules/ModuleDownloader.cs
Normal file
30
DiscordBotCore/Modules/ModuleDownloader.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user