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,9 +143,16 @@ public class PluginLoader : IPluginLoader
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())
.Where(p => typeof(T).IsAssignableFrom(p) && !p.IsInterface);
foreach (var type in types)
{

View File

@@ -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<string> 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<string, object>("userId", args.Context.Message.Author.Id));

View File

@@ -2,114 +2,107 @@
@using System.ComponentModel.DataAnnotations
@using DiscordBotCore.Configuration
@using DiscordBotCore.Logging
@inject NavigationManager Navigation
@rendermode InteractiveServer
@if (_settingsViewModel is not null)
{
<EditForm Model="@_settingsViewModel" OnValidSubmit="HandleSubmitTask">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="container-fluid d-flex justify-content-center align-items-center" style="height: 95vh;">
<div class="card shadow-lg border-0" style="max-width: 500px; width: 100%;">
<div class="card-body p-4">
<h2 class="card-title text-center mb-4 fw-semibold">Bot Settings</h2>
<div class="mb-3">
<label class="form-label" for="tokenInput">Token</label>
<InputText id="tokenInput" class="form-control" @bind-Value="_settingsViewModel.Token" />
<EditForm Model="_settingsViewModel" OnValidSubmit="HandleSubmitTask">
<DataAnnotationsValidator />
<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 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>
</div>
}
@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<string>("token");
var prefix = Configuration.Get<string>("prefix");
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);
_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;
}
}
}