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

View File

@@ -7,19 +7,35 @@ namespace DiscordBot;
public static class Entry
{
private static readonly string logo = @"
private static readonly string logo =
#if DEBUG
@"
_____ _ _ _____ _ _ ____ _
/ ____| | | | | | __ \(_) | | | _ \ | |
| (___ ___| |_| |__ | | | |_ ___ ___ ___ _ __ __| | | |_) | ___ | |_
\___ \ / _ \ __| '_ \ | | | | / __|/ __/ _ \| '__/ _` | | _ < / _ \| __|
____) | __/ |_| | | | | |__| | \__ \ (_| (_) | | | (_| | | |_) | (_) | |_
|_____/ \___|\__|_| |_| |_____/|_|___/\___\___/|_| \__,_| |____/ \___/ \__|
|_____/ \___|\__|_| |_| |_____/|_|___/\___\___/|_| \__,_| |____/ \___/ \__|
(Debug)
";
#else
@"
_____ _ _ _____ _ _ ____ _
/ ____| | | | | | __ \(_) | | | _ \ | |
| (___ ___| |_| |__ | | | |_ ___ ___ ___ _ __ __| | | |_) | ___ | |_
\___ \ / _ \ __| '_ \ | | | | / __|/ __/ _ \| '__/ _` | | _ < / _ \| __|
____) | __/ |_| | | | | |__| | \__ \ (_| (_) | | | (_| | | |_) | (_) | |_
|_____/ \___|\__|_| |_| |_____/|_|___/\___\___/|_| \__,_| |____/ \___/ \__|
";
#endif
public static void Main(string[] args)
{
#if DEBUG
#if DEBUG
if (args.Length == 1 && args[0] == "/purge_plugins" )
{
foreach (var plugin in Directory.GetFiles("./Data/Plugins", "*.dll", SearchOption.AllDirectories))
@@ -28,7 +44,7 @@ public static class Entry
}
}
#endif
#endif
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(logo);
@@ -48,6 +64,6 @@ public static class Entry
return assembly;
}
Program.Startup(args);
Program.Startup(args).Wait();
}
}

View File

@@ -7,7 +7,7 @@ namespace DiscordBot;
public static class Installer
{
private static async Task AskForConfig(string key, string message)
private static void AskForConfig(string key, string message)
{
var value = AnsiConsole.Ask<string>($"[green]{message}[/]");
@@ -24,13 +24,13 @@ public static class Installer
{
if(!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("token"))
await AskForConfig("token", "Token:");
AskForConfig("token", "Token:");
if(!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("prefix"))
await AskForConfig("prefix", "Prefix:");
AskForConfig("prefix", "Prefix:");
if(!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("ServerID"))
await AskForConfig("ServerID", "Server ID:");
AskForConfig("ServerID", "Server ID:");
await Application.CurrentApplication.ApplicationEnvironmentVariables.SaveToFile();

View File

@@ -7,7 +7,6 @@ using System.Threading.Tasks;
using DiscordBotCore;
using DiscordBotCore.Bot;
using DiscordBotCore.Others;
using DiscordBotCore.Others.Actions;
using DiscordBotCore.Updater.Application;
using Spectre.Console;
@@ -19,25 +18,18 @@ public class Program
/// <summary>
/// The main entry point for the application.
/// </summary>
public static void Startup(string[] args)
public static async Task Startup(string[] args)
{
PreLoadComponents(args).Wait();
await LoadComponents(args);
if (!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("ServerID") ||
!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("token") ||
!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("prefix")
)
Installer.GenerateStartupConfig().Wait();
HandleInput().Wait();
await PrepareConsole();
await ConsoleInputHandler();
}
/// <summary>
/// The main loop for the discord bot
/// </summary>
private static void NoGUI()
private static async Task ConsoleInputHandler()
{
Application.CurrentApplication.InternalActionManager.Execute("plugin", "load").Wait();
@@ -50,7 +42,7 @@ public class Program
if (args.Length == 0)
args = null;
Application.CurrentApplication.InternalActionManager.Execute(command, args).Wait();
await Application.CurrentApplication.InternalActionManager.Execute(command, args);
}
}
@@ -58,7 +50,7 @@ public class Program
/// Start the bot without user interface
/// </summary>
/// <returns>Returns the bootloader for the Discord Bot</returns>
private static async Task StartNoGui()
private static async Task PrepareConsole()
{
AnsiConsole.MarkupLine($"[yellow]Running on version: {Application.CurrentApplication.ApplicationEnvironmentVariables["Version"]}[/]");
@@ -83,29 +75,7 @@ public class Program
}
}
/// <summary>
/// Handle user input arguments from the startup of the application
/// </summary>
private static async Task HandleInput()
{
await StartNoGui();
try
{
NoGUI();
}
catch (IOException ex)
{
if (ex.Message == "No process is on the other end of the pipe." || (uint)ex.HResult == 0x800700E9)
{
Application.CurrentApplication.Logger.Log("An error occured while closing the bot last time. Please consider closing the bot using the &rexit&c method !\n" +
"There is a risk of losing all data or corruption of the save file, which in some cases requires to reinstall the bot !",
typeof(Program), LogType.ERROR
);
}
}
}
private static async Task PreLoadComponents(string[] args)
private static async Task LoadComponents(string[] args)
{
await Application.CreateApplication();
@@ -145,5 +115,12 @@ public class Program
string messageToPrint = $"{messageColor}{logMessage.Message}[/]";
AnsiConsole.MarkupLine(messageToPrint);
};
if (!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("ServerID") ||
!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("token") ||
!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("prefix")
)
Installer.GenerateStartupConfig().Wait();
}
}

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();

View File

@@ -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}") { }
}
}

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
{

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;
}
}

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;

View File

@@ -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;
}

View File

@@ -4,8 +4,6 @@ VisualStudioVersion = 17.1.32421.90
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscordBot", "DiscordBot\DiscordBot.csproj", "{087E64F4-1E1C-4899-8223-295356C9894A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SethPlugins", "SethPlugins", "{78B6D390-F61A-453F-B38D-E4C054321615}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscordBotUI", "DiscordBotUI\DiscordBotUI\DiscordBotUI.csproj", "{71293BF7-79D0-4707-AB4B-FDD16800FA81}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscordBotUI.Desktop", "DiscordBotUI\DiscordBotUI.Desktop\DiscordBotUI.Desktop.csproj", "{F23CF852-2042-4BDE-ABFE-D4F5BD9B991D}"