From 6315d13d18a9a1653d0823f0b84152baf473ddde Mon Sep 17 00:00:00 2001 From: Andrei Tudor Date: Tue, 15 Aug 2023 16:17:32 +0300 Subject: [PATCH] Fixed Internal Actions to refresh after external actions are loaded --- DiscordBot/DiscordBot.csproj | 1 + DiscordBot/Program.cs | 6 ++-- PluginManager/Loaders/ActionsLoader.cs | 2 +- .../Others/Actions/InternalActionsManager.cs | 30 ++++++++++++------- .../Others/DBCommandExecutingArguments.cs | 29 +++++++++++++++++- SethDiscordBot.sln | 7 +++++ 6 files changed, 59 insertions(+), 16 deletions(-) diff --git a/DiscordBot/DiscordBot.csproj b/DiscordBot/DiscordBot.csproj index 0d7c74f..614d97f 100644 --- a/DiscordBot/DiscordBot.csproj +++ b/DiscordBot/DiscordBot.csproj @@ -34,6 +34,7 @@ + diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index b927b1f..ee062dc 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -44,7 +44,9 @@ public class Program { #if DEBUG Console.WriteLine("Debug mode enabled"); + internalActionManager.Initialize().Wait(); internalActionManager.Execute("plugin", "load").Wait(); // Load plugins at startup + internalActionManager.Refresh().Wait(); #endif while (true) @@ -139,9 +141,7 @@ public class Program var b = await StartNoGui(); try { - internalActionManager = new InternalActionManager("./Data/Actions", "*.dll"); - await internalActionManager.Initialize(); - + internalActionManager = new InternalActionManager("./Data/Plugins", "*.dll"); NoGUI(); } catch ( IOException ex ) diff --git a/PluginManager/Loaders/ActionsLoader.cs b/PluginManager/Loaders/ActionsLoader.cs index ded77b7..c6504f3 100644 --- a/PluginManager/Loaders/ActionsLoader.cs +++ b/PluginManager/Loaders/ActionsLoader.cs @@ -15,7 +15,7 @@ public class ActionsLoader private readonly string actionExtension = "dll"; - private readonly string actionFolder = @"./Data/Actions/"; + private readonly string actionFolder = @"./Data/Plugins/"; public ActionsLoader(string path, string extension) { diff --git a/PluginManager/Others/Actions/InternalActionsManager.cs b/PluginManager/Others/Actions/InternalActionsManager.cs index b7b40c9..0ee4498 100644 --- a/PluginManager/Others/Actions/InternalActionsManager.cs +++ b/PluginManager/Others/Actions/InternalActionsManager.cs @@ -18,23 +18,31 @@ public class InternalActionManager public async Task Initialize() { - loader.ActionLoadedEvent += OnActionLoaded; + //loader.ActionLoadedEvent += OnActionLoaded; var m_actions = await loader.Load(); if (m_actions == null) return; foreach (var action in m_actions) - Actions.Add(action.ActionName, action); - } - - private void OnActionLoaded(string name, string typeName, bool success, Exception? e) - { - if (!success) { - Config.Logger.Error(e); - return; + Actions.TryAdd(action.ActionName, action); } - - Config.Logger.Log($"Action {name} loaded successfully", LogLevel.INFO, true); } + + public async Task Refresh() + { + Actions.Clear(); + await Initialize(); + } + + // private void OnActionLoaded(string name, string typeName, bool success, Exception? e) + // { + // if (!success) + // { + // Config.Logger.Error(e); + // return; + // } + // + // Config.Logger.Log($"Action {name} loaded successfully", LogLevel.INFO, true); + // } public async Task Execute(string actionName, params string[]? args) { diff --git a/PluginManager/Others/DBCommandExecutingArguments.cs b/PluginManager/Others/DBCommandExecutingArguments.cs index 75141c5..4b44a8f 100644 --- a/PluginManager/Others/DBCommandExecutingArguments.cs +++ b/PluginManager/Others/DBCommandExecutingArguments.cs @@ -1,4 +1,7 @@ -using Discord.Commands; +using System.Linq; +using Discord.Commands; +using Discord.WebSocket; +using PluginManager.Bot; namespace PluginManager.Others; @@ -13,6 +16,30 @@ public class DBCommandExecutingArguments this.arguments = arguments; } + public DBCommandExecutingArguments(SocketUserMessage? message, DiscordSocketClient client) + { + this.context = new SocketCommandContext(client, message); + int pos = 0; + if (message.HasMentionPrefix(client.CurrentUser, ref pos)) + { + var mentionPrefix = "<@" + client.CurrentUser.Id + ">"; + this.cleanContent = message.Content.Substring(mentionPrefix.Length + 1); + } + else + { + this.cleanContent = message.Content.Substring(Config.DiscordBot.botPrefix.Length); + } + + var split = this.cleanContent.Split(' '); + + string[]? argsClean = null; + if (split.Length > 1) + argsClean = string.Join(' ', split, 1, split.Length - 1).Split(' '); + + this.commandUsed = split[0]; + this.arguments = argsClean; + } + public SocketCommandContext context { get; init; } public string cleanContent { get; init; } public string commandUsed { get; init; } diff --git a/SethDiscordBot.sln b/SethDiscordBot.sln index 4a6d285..a8d9f43 100644 --- a/SethDiscordBot.sln +++ b/SethDiscordBot.sln @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MusicPlayer", "..\SethPlugi EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LevelingSystem", "..\SethPlugins\LevelingSystem\LevelingSystem.csproj", "{BFE3491C-AC01-4252-B242-6451270FC548}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PythonCompatibilityLayer", "..\SethPlugins\PythonCompatibilityLayer\PythonCompatibilityLayer.csproj", "{81ED4953-13E5-4950-96A8-8CEF5FD59559}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -35,6 +37,10 @@ Global {BFE3491C-AC01-4252-B242-6451270FC548}.Debug|Any CPU.Build.0 = Debug|Any CPU {BFE3491C-AC01-4252-B242-6451270FC548}.Release|Any CPU.ActiveCfg = Release|Any CPU {BFE3491C-AC01-4252-B242-6451270FC548}.Release|Any CPU.Build.0 = Release|Any CPU + {81ED4953-13E5-4950-96A8-8CEF5FD59559}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {81ED4953-13E5-4950-96A8-8CEF5FD59559}.Debug|Any CPU.Build.0 = Debug|Any CPU + {81ED4953-13E5-4950-96A8-8CEF5FD59559}.Release|Any CPU.ActiveCfg = Release|Any CPU + {81ED4953-13E5-4950-96A8-8CEF5FD59559}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -42,6 +48,7 @@ Global GlobalSection(NestedProjects) = preSolution {1690CBBC-BDC0-4DD8-B701-F8817189D9D5} = {78B6D390-F61A-453F-B38D-E4C054321615} {BFE3491C-AC01-4252-B242-6451270FC548} = {78B6D390-F61A-453F-B38D-E4C054321615} + {81ED4953-13E5-4950-96A8-8CEF5FD59559} = {78B6D390-F61A-453F-B38D-E4C054321615} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {3FB3C5DE-ED21-4D2E-ABDD-3A00EE4A2FFF}