From a0177ce1454963bcf4fa135ea5fbdf6035d84d4a Mon Sep 17 00:00:00 2001 From: Andrei Tudor Date: Thu, 22 May 2025 18:07:25 +0300 Subject: [PATCH] Fixed PluginLoading to use the context instead of appDomain --- .../PluginLoader.cs | 9 +- Plugins/LevelingSystem/LevelCommand.cs | 7 +- WebUI/Components/Pages/Settings.razor | 127 +++++++++--------- 3 files changed, 70 insertions(+), 73 deletions(-) diff --git a/DiscordBotCore.PluginManagement.Loading/PluginLoader.cs b/DiscordBotCore.PluginManagement.Loading/PluginLoader.cs index 5ce855d..d60ea63 100644 --- a/DiscordBotCore.PluginManagement.Loading/PluginLoader.cs +++ b/DiscordBotCore.PluginManagement.Loading/PluginLoader.cs @@ -143,9 +143,16 @@ public class PluginLoader : IPluginLoader private void LoadEverythingOfType() { - var types = AppDomain.CurrentDomain.GetAssemblies() + if (PluginLoaderContext is null) + { + _Logger.Log("The plugins are not loaded. Please load the plugins before loading them.", this, LogType.Error); + return; + } + + var types = PluginLoaderContext.Assemblies .SelectMany(s => s.GetTypes()) .Where(p => typeof(T).IsAssignableFrom(p) && !p.IsInterface); + foreach (var type in types) { diff --git a/Plugins/LevelingSystem/LevelCommand.cs b/Plugins/LevelingSystem/LevelCommand.cs index 726bb56..af07d02 100644 --- a/Plugins/LevelingSystem/LevelCommand.cs +++ b/Plugins/LevelingSystem/LevelCommand.cs @@ -1,6 +1,4 @@ using Discord; -using DiscordBotCore.Logging; -using DiscordBotCore.PluginCore.Helpers; using DiscordBotCore.PluginCore.Helpers.Execution.DbCommand; using DiscordBotCore.PluginCore.Interfaces; @@ -12,7 +10,7 @@ internal class LevelCommand: IDbCommand public List Aliases => ["lvl", "rank"]; - public string Description => "Display tour current level"; + public string Description => "Display your current level"; public string Usage => "level"; @@ -25,8 +23,7 @@ internal class LevelCommand: IDbCommand args.Logger.Log("Database is not initialized", this); return; } - - + object[]? user = await Variables.Database.ReadDataArrayAsync($"SELECT * FROM Levels WHERE UserID=@userId", new KeyValuePair("userId", args.Context.Message.Author.Id)); diff --git a/WebUI/Components/Pages/Settings.razor b/WebUI/Components/Pages/Settings.razor index f23f08a..55d1383 100644 --- a/WebUI/Components/Pages/Settings.razor +++ b/WebUI/Components/Pages/Settings.razor @@ -2,114 +2,107 @@ @using System.ComponentModel.DataAnnotations @using DiscordBotCore.Configuration @using DiscordBotCore.Logging - @inject NavigationManager Navigation @rendermode InteractiveServer @if (_settingsViewModel is not null) { - - - +
+
+
+

Bot Settings

-
- - + + + + +
+ + +
+ +
+ + +
+ +
+ + +
+ + +
+
- -
- - -
- -
- - -
- - - +
} + @code { - [Inject] - public ILogger Logger { get; set; } - - [Inject] - public IConfiguration Configuration { get; set; } - + [Inject] public ILogger Logger { get; set; } + [Inject] public IConfiguration Configuration { get; set; } + private SettingsViewModel? _settingsViewModel; - + protected override void OnInitialized() { var token = Configuration.Get("token"); var prefix = Configuration.Get("prefix"); var serverIds = Configuration.GetList("ServerIds", new List()); - if(token == null || prefix == null) + + if (token is null || prefix is null) { Logger.Log("Token or Prefix is not set in the configuration.", this); - _settingsViewModel = new SettingsViewModel - { - Token = "", - Prefix = "", - ServerIds = "" - }; - + _settingsViewModel = new SettingsViewModel(); return; } - - + _settingsViewModel = new SettingsViewModel { Token = token, Prefix = prefix, - ServerIds = string.Join(",", serverIds) + ServerIds = string.Join(',', serverIds) }; - - StateHasChanged(); } private async Task HandleSubmitTask() { - if (_settingsViewModel is null) - { - return; - } - - var token = _settingsViewModel.Token; - var prefix = _settingsViewModel.Prefix; - var serverIds = _settingsViewModel.ServerIds - .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) - .Select(id => ulong.TryParse(id.Trim(), out var result) ? result : 0) - .Where(id => id != 0) - .ToList(); + if (_settingsViewModel is null) return; - if (string.IsNullOrWhiteSpace(token) || string.IsNullOrWhiteSpace(prefix)) - { + var ids = _settingsViewModel.ServerIds + .Split(',', StringSplitOptions.RemoveEmptyEntries) + .Select(id => ulong.TryParse(id.Trim(), out var v) ? v : 0) + .Where(v => v != 0) + .ToList(); + + if (string.IsNullOrWhiteSpace(_settingsViewModel.Token) || + string.IsNullOrWhiteSpace(_settingsViewModel.Prefix)) return; - } - - Configuration.Set("token", token); - Configuration.Set("prefix", prefix); - Configuration.Set("ServerIds", serverIds); + + Configuration.Set("token", _settingsViewModel.Token); + Configuration.Set("prefix", _settingsViewModel.Prefix); + Configuration.Set("ServerIds", ids); await Configuration.SaveToFile(); Logger.Log("Settings saved successfully.", this); - - Navigation.NavigateTo($"/"); + Navigation.NavigateTo("/"); } - private class SettingsViewModel { [Required(ErrorMessage = "Token is required.")] - public string Token { get; set; } + public string Token { get; set; } = string.Empty; [Required(ErrorMessage = "Prefix is required.")] - public string Prefix { get; set; } + public string Prefix { get; set; } = string.Empty; [Required(ErrorMessage = "Server IDs are required.")] - public string ServerIds { get; set; } + public string ServerIds { get; set; } = string.Empty; } - -} \ No newline at end of file +}