Added external repo support
This commit is contained in:
49
DiscordBotCore/Repository/ModuleRepository.cs
Normal file
49
DiscordBotCore/Repository/ModuleRepository.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using DiscordBotCore.Online;
|
||||
|
||||
namespace DiscordBotCore.Repository;
|
||||
|
||||
public sealed class ModuleRepository : RepositoryBase
|
||||
{
|
||||
public static readonly ModuleRepository Default = new ModuleRepository("Testing", "https://wizzy-server.ro/SethDiscordBot/ModulesRepo", "modules.json");
|
||||
private ModuleRepository(string repositoryName, string repositoryUrl, string databaseFile) : base(repositoryName, repositoryUrl, databaseFile)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<string> JsonGetAllModules()
|
||||
{
|
||||
var jsonResponse = await ServerCom.GetAllTextFromUrl(DatabasePath);
|
||||
return jsonResponse;
|
||||
}
|
||||
|
||||
private static ModuleRepository From(string repositoryName, string repositoryUrl, string databaseFile)
|
||||
{
|
||||
return new ModuleRepository(repositoryName, repositoryUrl, databaseFile);
|
||||
}
|
||||
|
||||
internal static ModuleRepository SolveRepo()
|
||||
{
|
||||
if (!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("ModuleRepository"))
|
||||
{
|
||||
return Default;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var moduleRepoDict = Application.CurrentApplication.ApplicationEnvironmentVariables.GetDictionary<string, string>("ModuleRepository");
|
||||
var moduleRepo = From(
|
||||
moduleRepoDict["Name"],
|
||||
moduleRepoDict["Url"],
|
||||
moduleRepoDict["DatabaseFile"]
|
||||
);
|
||||
return moduleRepo;
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Application.Logger.LogException(ex, Application.CurrentApplication);
|
||||
|
||||
return Default;
|
||||
}
|
||||
}
|
||||
}
|
||||
52
DiscordBotCore/Repository/PluginRepository.cs
Normal file
52
DiscordBotCore/Repository/PluginRepository.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using DiscordBotCore.Online;
|
||||
|
||||
namespace DiscordBotCore.Repository;
|
||||
|
||||
public sealed class PluginRepository : RepositoryBase
|
||||
{
|
||||
public static readonly PluginRepository Default = new PluginRepository("Testing", "https://wizzy-server.ro/SethDiscordBot/PluginsRepo", "PluginsList.json");
|
||||
|
||||
private PluginRepository(string repositoryName, string repositoryUrl, string databaseFile) : base(repositoryName, repositoryUrl, databaseFile)
|
||||
{
|
||||
}
|
||||
|
||||
private static PluginRepository From(string repositoryName, string repositoryUrl, string databaseFile)
|
||||
{
|
||||
return new PluginRepository(repositoryName, repositoryUrl, databaseFile);
|
||||
}
|
||||
|
||||
public async Task<string> JsonGetAllPlugins()
|
||||
{
|
||||
var jsonResponse = await ServerCom.GetAllTextFromUrl(DatabasePath);
|
||||
|
||||
return jsonResponse;
|
||||
}
|
||||
|
||||
internal static PluginRepository SolveRepo()
|
||||
{
|
||||
if (!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("PluginRepository"))
|
||||
{
|
||||
return Default;
|
||||
}
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
var pluginRepoDict = Application.CurrentApplication.ApplicationEnvironmentVariables.GetDictionary<string, string>("PluginRepository");
|
||||
var pluginRepo = PluginRepository.From(
|
||||
pluginRepoDict["Name"],
|
||||
pluginRepoDict["Url"],
|
||||
pluginRepoDict["DatabaseFile"]
|
||||
);
|
||||
return pluginRepo;
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Application.Logger.LogException(ex, Application.CurrentApplication);
|
||||
|
||||
return Default;
|
||||
}
|
||||
}
|
||||
}
|
||||
33
DiscordBotCore/Repository/RepositoryBase.cs
Normal file
33
DiscordBotCore/Repository/RepositoryBase.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
namespace DiscordBotCore.Repository;
|
||||
|
||||
public abstract class RepositoryBase
|
||||
{
|
||||
/*
|
||||
Example of JSON for config file :
|
||||
Please mind that the URL should not end with a slash. !
|
||||
|
||||
"PluginRepository": {
|
||||
"Name": "Default Plugins Repository",
|
||||
"Url": "http://blahblahblah.blah/MyCustomRepo",
|
||||
"DatabaseFile": "PluginsList.json"
|
||||
},
|
||||
"ModuleRepository": {
|
||||
"Name": "Default Modules Repository",
|
||||
"Url": "http://blahblahblah.blah/MyCustomRepo",
|
||||
"DatabaseFile": "modules.json"
|
||||
}
|
||||
*/
|
||||
public string RepositoryName { get; init; }
|
||||
private string RepositoryUrl { get; init; }
|
||||
private string DatabaseFile { get; init; }
|
||||
|
||||
protected string DatabasePath => $"{RepositoryUrl}/{DatabaseFile}";
|
||||
|
||||
protected RepositoryBase(string repositoryName, string repositoryUrl, string databaseFile)
|
||||
{
|
||||
RepositoryName = repositoryName;
|
||||
RepositoryUrl = repositoryUrl;
|
||||
DatabaseFile = databaseFile;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user