Fixed PluginLoading to use the context instead of appDomain

This commit is contained in:
2025-05-22 18:07:25 +03:00
parent e57e941c94
commit a0177ce145
3 changed files with 70 additions and 73 deletions

View File

@@ -143,10 +143,17 @@ public class PluginLoader : IPluginLoader
private void LoadEverythingOfType<T>() private void LoadEverythingOfType<T>()
{ {
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()) .SelectMany(s => s.GetTypes())
.Where(p => typeof(T).IsAssignableFrom(p) && !p.IsInterface); .Where(p => typeof(T).IsAssignableFrom(p) && !p.IsInterface);
foreach (var type in types) foreach (var type in types)
{ {
T? plugin = (T?)Activator.CreateInstance(type); T? plugin = (T?)Activator.CreateInstance(type);

View File

@@ -1,6 +1,4 @@
using Discord; using Discord;
using DiscordBotCore.Logging;
using DiscordBotCore.PluginCore.Helpers;
using DiscordBotCore.PluginCore.Helpers.Execution.DbCommand; using DiscordBotCore.PluginCore.Helpers.Execution.DbCommand;
using DiscordBotCore.PluginCore.Interfaces; using DiscordBotCore.PluginCore.Interfaces;
@@ -12,7 +10,7 @@ internal class LevelCommand: IDbCommand
public List<string> Aliases => ["lvl", "rank"]; public List<string> Aliases => ["lvl", "rank"];
public string Description => "Display tour current level"; public string Description => "Display your current level";
public string Usage => "level"; public string Usage => "level";
@@ -26,7 +24,6 @@ internal class LevelCommand: IDbCommand
return; return;
} }
object[]? user = await Variables.Database.ReadDataArrayAsync($"SELECT * FROM Levels WHERE UserID=@userId", object[]? user = await Variables.Database.ReadDataArrayAsync($"SELECT * FROM Levels WHERE UserID=@userId",
new KeyValuePair<string, object>("userId", args.Context.Message.Author.Id)); new KeyValuePair<string, object>("userId", args.Context.Message.Author.Id));

View File

@@ -2,41 +2,51 @@
@using System.ComponentModel.DataAnnotations @using System.ComponentModel.DataAnnotations
@using DiscordBotCore.Configuration @using DiscordBotCore.Configuration
@using DiscordBotCore.Logging @using DiscordBotCore.Logging
@inject NavigationManager Navigation @inject NavigationManager Navigation
@rendermode InteractiveServer @rendermode InteractiveServer
@if (_settingsViewModel is not null) @if (_settingsViewModel is not null)
{ {
<EditForm Model="@_settingsViewModel" OnValidSubmit="HandleSubmitTask"> <div class="container-fluid d-flex justify-content-center align-items-center" style="height: 95vh;">
<DataAnnotationsValidator /> <div class="card shadow-lg border-0" style="max-width: 500px; width: 100%;">
<ValidationSummary /> <div class="card-body p-4">
<h2 class="card-title text-center mb-4 fw-semibold">Bot Settings</h2>
<div class="mb-3"> <EditForm Model="_settingsViewModel" OnValidSubmit="HandleSubmitTask">
<label class="form-label" for="tokenInput">Token</label> <DataAnnotationsValidator />
<InputText id="tokenInput" class="form-control" @bind-Value="_settingsViewModel.Token" /> <ValidationSummary class="text-danger" />
<div class="mb-3">
<label class="form-label" for="tokenInput">Token</label>
<InputText id="tokenInput" class="form-control" placeholder="Enter bot token"
@bind-Value="_settingsViewModel.Token" />
</div>
<div class="mb-3">
<label class="form-label" for="prefixInput">Prefix</label>
<InputText id="prefixInput" class="form-control" placeholder="!"
@bind-Value="_settingsViewModel.Prefix" />
</div>
<div class="mb-4">
<label class="form-label" for="serverIdsInput">Server IDs (comma-separated)</label>
<InputTextArea id="serverIdsInput" class="form-control" placeholder="12345, 67890" Rows="3"
@bind-Value="_settingsViewModel.ServerIds" />
</div>
<button type="submit" class="btn btn-primary w-100">
Save&nbsp;<i class="bi bi-check-lg"></i>
</button>
</EditForm>
</div>
</div> </div>
</div>
<div class="mb-3">
<label class="form-label" for="prefixInput">Prefix</label>
<InputText id="prefixInput" class="form-control" @bind-Value="_settingsViewModel.Prefix" />
</div>
<div class="mb-3">
<label class="form-label" for="serverIdsInput">Server IDs (comma-separated)</label>
<InputTextArea id="serverIdsInput" class="form-control" @bind-Value="_settingsViewModel.ServerIds" Rows="3" />
</div>
<button type="submit" class="btn btn-primary">Save</button>
</EditForm>
} }
@code {
[Inject]
public ILogger Logger { get; set; }
[Inject] @code {
public IConfiguration Configuration { get; set; } [Inject] public ILogger Logger { get; set; }
[Inject] public IConfiguration Configuration { get; set; }
private SettingsViewModel? _settingsViewModel; private SettingsViewModel? _settingsViewModel;
@@ -45,71 +55,54 @@
var token = Configuration.Get<string>("token"); var token = Configuration.Get<string>("token");
var prefix = Configuration.Get<string>("prefix"); var prefix = Configuration.Get<string>("prefix");
var serverIds = Configuration.GetList<ulong>("ServerIds", new List<ulong>()); var serverIds = Configuration.GetList<ulong>("ServerIds", new List<ulong>());
if(token == null || prefix == null)
if (token is null || prefix is null)
{ {
Logger.Log("Token or Prefix is not set in the configuration.", this); Logger.Log("Token or Prefix is not set in the configuration.", this);
_settingsViewModel = new SettingsViewModel _settingsViewModel = new SettingsViewModel();
{
Token = "",
Prefix = "",
ServerIds = ""
};
return; return;
} }
_settingsViewModel = new SettingsViewModel _settingsViewModel = new SettingsViewModel
{ {
Token = token, Token = token,
Prefix = prefix, Prefix = prefix,
ServerIds = string.Join(",", serverIds) ServerIds = string.Join(',', serverIds)
}; };
StateHasChanged();
} }
private async Task HandleSubmitTask() private async Task HandleSubmitTask()
{ {
if (_settingsViewModel is null) if (_settingsViewModel is null) return;
{
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; return;
}
var token = _settingsViewModel.Token; Configuration.Set("token", _settingsViewModel.Token);
var prefix = _settingsViewModel.Prefix; Configuration.Set("prefix", _settingsViewModel.Prefix);
var serverIds = _settingsViewModel.ServerIds Configuration.Set("ServerIds", ids);
.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(id => ulong.TryParse(id.Trim(), out var result) ? result : 0)
.Where(id => id != 0)
.ToList();
if (string.IsNullOrWhiteSpace(token) || string.IsNullOrWhiteSpace(prefix))
{
return;
}
Configuration.Set("token", token);
Configuration.Set("prefix", prefix);
Configuration.Set("ServerIds", serverIds);
await Configuration.SaveToFile(); await Configuration.SaveToFile();
Logger.Log("Settings saved successfully.", this); Logger.Log("Settings saved successfully.", this);
Navigation.NavigateTo("/");
Navigation.NavigateTo($"/");
} }
private class SettingsViewModel private class SettingsViewModel
{ {
[Required(ErrorMessage = "Token is required.")] [Required(ErrorMessage = "Token is required.")]
public string Token { get; set; } public string Token { get; set; } = string.Empty;
[Required(ErrorMessage = "Prefix is required.")] [Required(ErrorMessage = "Prefix is required.")]
public string Prefix { get; set; } public string Prefix { get; set; } = string.Empty;
[Required(ErrorMessage = "Server IDs are required.")] [Required(ErrorMessage = "Server IDs are required.")]
public string ServerIds { get; set; } public string ServerIds { get; set; } = string.Empty;
} }
} }