From 1881102fb7414f3980a23ffa29b8a8d63a0be022 Mon Sep 17 00:00:00 2001 From: Andrei Tudor Date: Fri, 14 Jun 2024 17:09:02 +0300 Subject: [PATCH] Fixed duplicate actions --- DiscordBot/Program.cs | 3 +-- DiscordBotCore/Application.cs | 6 ++++-- DiscordBotCore/Loaders/PluginLoader.cs | 12 ++++++++++++ .../Others/Actions/InternalActionsManager.cs | 7 ++++++- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index da29545..79cb0fb 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -55,7 +55,7 @@ public class Program private static async Task PrepareConsole() { - AnsiConsole.MarkupLine($"[yellow]Running on version: {Application.CurrentApplication.ApplicationEnvironmentVariables["Version"]}[/]"); + AnsiConsole.MarkupLine($"[yellow]Running on version: {Assembly.GetExecutingAssembly().GetName().Version}[/]"); AnsiConsole.MarkupLine("[yellow]Git SethBot: https://github.com/andreitdr/SethDiscordBot [/]"); AnsiConsole.MarkupLine("[yellow]Git Plugins: https://github.com/andreitdr/SethPlugins [/]"); @@ -80,7 +80,6 @@ public class Program private static async Task LoadComponents(string[] args) { await Application.CreateApplication(); - Application.CurrentApplication.ApplicationEnvironmentVariables["Version"] = Assembly.GetExecutingAssembly().GetName().Version.ToString(); AppUpdater updater = new(); var update = await updater.CheckForUpdates(); diff --git a/DiscordBotCore/Application.cs b/DiscordBotCore/Application.cs index edc0a4f..ff8e7df 100644 --- a/DiscordBotCore/Application.cs +++ b/DiscordBotCore/Application.cs @@ -6,14 +6,16 @@ using DiscordBotCore.Plugin; using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; using System.Threading.Tasks; namespace DiscordBotCore { - public class Application + /// + /// The main Application and its components + /// + public sealed class Application { public static Application CurrentApplication { get; private set; } = null!; diff --git a/DiscordBotCore/Loaders/PluginLoader.cs b/DiscordBotCore/Loaders/PluginLoader.cs index 99e1138..fd2a614 100644 --- a/DiscordBotCore/Loaders/PluginLoader.cs +++ b/DiscordBotCore/Loaders/PluginLoader.cs @@ -38,6 +38,18 @@ public class PluginLoader public async Task LoadPlugins() { + + if (_Client == null) + { + Application.CurrentApplication.Logger.Log("Discord client is null", this, LogType.ERROR); + return; + } + + Commands.Clear(); + Events.Clear(); + SlashCommands.Clear(); + Actions.Clear(); + Application.CurrentApplication.Logger.Log("Loading plugins...", this); var loader = new Loader(); diff --git a/DiscordBotCore/Others/Actions/InternalActionsManager.cs b/DiscordBotCore/Others/Actions/InternalActionsManager.cs index 1ca470d..cf88cd8 100644 --- a/DiscordBotCore/Others/Actions/InternalActionsManager.cs +++ b/DiscordBotCore/Others/Actions/InternalActionsManager.cs @@ -13,12 +13,17 @@ public class InternalActionManager public async Task Initialize() { Actions.Clear(); + PluginLoader.Actions.ForEach(action => { if (action.RunType == InternalActionRunType.ON_CALL || action.RunType == InternalActionRunType.BOTH) { if (this.Actions.ContainsKey(action.ActionName)) - return; // ingore duplicates + { + // This should never happen. If it does, log it and return + Application.CurrentApplication.Logger.Log($"Action {action.ActionName} already exists", this, LogType.ERROR); + return; + } this.Actions.Add(action.ActionName, action); }