Cleaned up the Console bot. Added PluginNotFoundException

This commit is contained in:
2024-05-12 21:47:59 +03:00
parent 17147d920d
commit 152e09f4af
10 changed files with 72 additions and 57 deletions
+8 -3
View File
@@ -14,9 +14,9 @@ namespace DiscordBotCore
{
public class Application
{
public static Application CurrentApplication { get; private set; } = null;
public static Application CurrentApplication { get; private set; } = null!;
private static readonly string _DefaultLogMessageFormat = "{SenderName} {Message}";
private static readonly string _DefaultLogMessageFormat = "{ThrowTime} {SenderName} {Message}";
private static readonly string _ConfigFile = "./Data/Resources/config.json";
private static readonly string _PluginsDatabaseFile = "./Data/Resources/plugins.json";
@@ -30,7 +30,7 @@ namespace DiscordBotCore
public string ServerID => ApplicationEnvironmentVariables["ServerID"];
public string PluginDatabase => ApplicationEnvironmentVariables["PluginDatabase"];
public string LogFile => $"{ApplicationEnvironmentVariables["LogFolder"]}/{DateTime.Now.ToLongDateString().Replace(" / ", "")}.log";
public string DataFolder => _ResourcesFolder;
public SettingsDictionary<string, string> ApplicationEnvironmentVariables { get; private set; }
public InternalActionManager InternalActionManager { get; private set; }
@@ -69,7 +69,12 @@ namespace DiscordBotCore
await JsonManager.SaveToJsonFile(_PluginsDatabaseFile, plugins);
}
#if DEBUG
CurrentApplication.PluginManager = new PluginManager("tests");
#else
CurrentApplication.PluginManager = new PluginManager();
#endif
await CurrentApplication.PluginManager.UninstallMarkedPlugins();
await CurrentApplication.PluginManager.CheckForUpdates();
@@ -0,0 +1,12 @@
using System;
namespace DiscordBotCore.Others.Exceptions
{
internal class PluginNotFoundException : Exception
{
public PluginNotFoundException(string pluginName) : base($"Plugin {pluginName} was not found") { }
public PluginNotFoundException(string pluginName, string url, string branch) :
base ($"Plugin {pluginName} was not found on {url} (branch: {branch}") { }
}
}
+1 -1
View File
@@ -15,7 +15,7 @@ public static class Functions
/// The location for the Resources folder
/// String: ./Data/Resources/
/// </summary>
public static readonly string dataFolder = @"./Data/Resources/";
public static string dataFolder => Application.CurrentApplication.DataFolder;
public static Color RandomColor
{
+3 -1
View File
@@ -8,7 +8,7 @@ namespace DiscordBotCore.Others;
public class SettingsDictionary<TKey, TValue>
{
private string _File { get; }
private IDictionary<TKey, TValue> _Dictionary;
protected IDictionary<TKey, TValue> _Dictionary;
public SettingsDictionary(string file)
{
@@ -84,4 +84,6 @@ public class SettingsDictionary<TKey, TValue>
}
set => _Dictionary[key] = value;
}
}
+1
View File
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
@@ -4,6 +4,7 @@ using System.IO;
using System.Threading.Tasks;
using DiscordBotCore.Online;
using DiscordBotCore.Others;
using DiscordBotCore.Others.Exceptions;
using DiscordBotCore.Plugin;
namespace DiscordBotCore.Updater.Plugins;
@@ -20,6 +21,8 @@ public class PluginUpdater
public async Task<PluginOnlineInfo> GetPluginInfo(string pluginName)
{
var result = await _PluginsManager.GetPluginDataByName(pluginName);
if(result is null)
throw new PluginNotFoundException(pluginName, _PluginsManager.BaseUrl, _PluginsManager.Branch);
return result;
}
@@ -29,7 +32,8 @@ public class PluginUpdater
List<PluginInfo> installedPlugins = await JsonManager.ConvertFromJson<List<PluginInfo>>(pluginsDatabase);
var result = installedPlugins.Find(p => p.PluginName == pluginName);
if (result is null)
throw new PluginNotFoundException(pluginName);
return result;
}