Added pages for module and plugin download

This commit is contained in:
2024-08-29 21:32:14 +03:00
parent 046c9bf98b
commit 34a54cd78f
10 changed files with 127 additions and 46 deletions

View File

@@ -40,11 +40,19 @@ public class PluginInfo
public static PluginInfo FromOnlineInfo(PluginOnlineInfo onlineInfo)
{
return new PluginInfo(onlineInfo.Name,
onlineInfo.Version,
onlineInfo.Dependencies
.Where(dep => dep.IsExecutable)
.Select(dep => new KeyValuePair<string, string>(dep.DependencyName, dep.DownloadLocation))
.ToDictionary());
var pluginName = onlineInfo.Name;
var version = onlineInfo.Version;
var dependencies= onlineInfo.Dependencies;
if(dependencies is null)
{
return new PluginInfo(pluginName, version, new Dictionary<string, string>());
}
var executableDependencies = dependencies.Where(dep => dep.IsExecutable);
var dictDependencies = executableDependencies.Select(dep => new KeyValuePair<string, string>(dep.DependencyName, dep.DownloadLocation)).ToDictionary();
return new PluginInfo(pluginName, version, dictDependencies);
}
}

View File

@@ -9,19 +9,21 @@ namespace DiscordBotCore.Plugin;
public class PluginOnlineInfo
{
public string Name { get; private set; }
public string Author { get; private set; }
public PluginVersion Version { get; private set; }
public string DownLoadLink { get; private set; }
public string Description { get; private set; }
public List<OnlineDependencyInfo> Dependencies { get; private set; }
public List<OnlineScriptDependencyInfo> ScriptDependencies { get; private set; }
public List<OnlineDependencyInfo>? Dependencies { get; private set; }
public List<OnlineScriptDependencyInfo>? ScriptDependencies { get; private set; }
public OSType SupportedOS { get; private set; }
public bool HasFileDependencies => Dependencies is not null && Dependencies.Count > 0;
public bool HasScriptDependencies => ScriptDependencies is not null && ScriptDependencies.Count > 0;
[JsonConstructor]
public PluginOnlineInfo(string name, PluginVersion version, string description, string downLoadLink, OSType supportedOS, List<OnlineDependencyInfo> dependencies, List<OnlineScriptDependencyInfo> scriptDependencies)
public PluginOnlineInfo(string name, string author, PluginVersion version, string description, string downLoadLink, OSType supportedOS, List<OnlineDependencyInfo> dependencies, List<OnlineScriptDependencyInfo> scriptDependencies)
{
Name = name;
Author = author;
Version = version;
Description = description;
DownLoadLink = downLoadLink;
@@ -30,9 +32,10 @@ public class PluginOnlineInfo
ScriptDependencies = scriptDependencies;
}
public PluginOnlineInfo(string name, PluginVersion version, string description, string downLoadLink, OSType supportedOS)
public PluginOnlineInfo(string name, string author, PluginVersion version, string description, string downLoadLink, OSType supportedOS)
{
Name = name;
Author = author;
Version = version;
Description = description;
DownLoadLink = downLoadLink;
@@ -48,6 +51,6 @@ public class PluginOnlineInfo
public override string ToString()
{
return $"{Name} - {Version} ({Description})";
return $"{Name} <{Author}> - {Version} ({Description})";
}
}

View File

@@ -2,8 +2,6 @@
@using DiscordBotCore.Plugin
@using DiscordBotWebUI.Types
@inject DialogService DialogService
<RadzenDataGrid AllowColumnResize="true" PageSize="5" AllowPaging="true" PagerHorizontalAlign="HorizontalAlign.Left" Data="ListedItems">
<Columns>
<RadzenDataGridColumn Property="@nameof(MarketItem.Name)" Title="Item Name"></RadzenDataGridColumn>
@@ -50,8 +48,6 @@
});
await Application.CurrentApplication.ModuleManager.InstallModule(moduleName, downloadProgress);
_ProgressBarVisible = false;
DialogService.Close(true);
}
private async Task DownloadPlugin(string pluginName)
@@ -68,7 +64,5 @@
await Application.CurrentApplication.PluginManager.InstallPlugin(pluginInfo, downloadProgress);
_ProgressBarVisible = false;
DialogService.Close(true);
}
}

View File

@@ -21,13 +21,13 @@
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="/settings" Match="NavLinkMatch.All">
<NavLink class="nav-link" href="/market/plugins" Match="NavLinkMatch.All">
<span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Plugins
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="/settings" Match="NavLinkMatch.All">
<NavLink class="nav-link" href="/market/modules" Match="NavLinkMatch.All">
<span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Modules
</NavLink>
</div>

View File

@@ -13,6 +13,14 @@
@code {
private string? _TextValue;
private DiscordBotStartup _DiscordBotStartup = null!;
protected override async void OnInitialized()
{
_DiscordBotStartup = new DiscordBotStartup(FixModules);
await _DiscordBotStartup.CreateApplication();
}
private async Task FixModules(ModuleRequirement requirements)
{
if(!requirements.RequireAny)
@@ -27,19 +35,17 @@
}
private async void Initialize()
{
DiscordBotStartup setup = new DiscordBotStartup(FixModules);
setup.Log += async (sender, str) => {
{
_DiscordBotStartup.Log += async (sender, str) => {
_TextValue += str + "\n";
await InvokeAsync(StateHasChanged);
};
dynamic result = await setup.LoadComponents();
dynamic result = _DiscordBotStartup.LoadComponents();
if (!result)
{
result = await DialogService.OpenAsync<FirstSetup>("Please complete this setup before starting the bot", new Dictionary<string, object>());
result = await DialogService.OpenAsync<Settings>("Please complete this setup before starting the bot", new Dictionary<string, object>());
if (result != true)
{
@@ -47,7 +53,7 @@
}
}
await setup.PrepareBot();
await setup.RefreshPlugins(false);
await _DiscordBotStartup.PrepareBot();
await _DiscordBotStartup.RefreshPlugins(false);
}
}

View File

@@ -0,0 +1,26 @@
@page "/market/modules"
@using DiscordBotCore
@using DiscordBotWebUI.Types
@using DiscordBotWebUI.Components.Items
@if(_MarketItems is not null && _MarketItems.Any())
{
<Marketplace ListedItems="_MarketItems"/>
} else
{
<RadzenText Text="There are no modules available right now ..."></RadzenText>
}
@code {
private readonly List<MarketItem>? _MarketItems = new List<MarketItem>();
protected override async Task OnInitializedAsync()
{
var modules = await Application.CurrentApplication.ModuleManager.ServerGetAllModules();
foreach(var onlineModule in modules)
{
var item = new MarketItem(onlineModule.ModuleName, onlineModule.ModuleAuthor, onlineModule.ModuleDescription, ItemType.Module);
_MarketItems.Add(item);
}
}
}

View File

@@ -0,0 +1,37 @@
@page "/market/plugins"
@using DiscordBotCore
@using DiscordBotWebUI.Types
@using DiscordBotWebUI.Components.Items
@if(_MarketItems is null)
{
<RadzenText Text="There are no plugins available right now ..."></RadzenText>
}
@if(_MarketItems is not null && _MarketItems.Any())
{
<Marketplace ListedItems="_MarketItems"/>
}
@code {
private List<MarketItem>? _MarketItems;
protected override async Task OnInitializedAsync()
{
var plugins = await Application.CurrentApplication.PluginManager.GetPluginsList();
if(plugins is null)
{
_MarketItems = null;
return;
}
_MarketItems = new List<MarketItem>();
foreach (var onlinePlugin in plugins)
{
var marketItem = new MarketItem(onlinePlugin.Name, onlinePlugin.Author, onlinePlugin.Description, ItemType.Plugin);
_MarketItems.Add(marketItem);
}
}
}

View File

@@ -1,5 +1,5 @@
@using DiscordBotCore
@inject DialogService DialogService
@page "/settings"
@using DiscordBotCore
<RadzenPanel>
<HeaderTemplate>
@@ -14,7 +14,7 @@
<RadzenTextBox Placeholder="Token..." Value="@_Token" ValueChanged="TokenValueChanged"></RadzenTextBox>
</RadzenColumn>
</RadzenRow>
<RadzenRow>
<RadzenColumn>
<RadzenText>Bot prefix</RadzenText>
@@ -23,7 +23,7 @@
<RadzenTextBox Placeholder="Bot prefix ..." MaxLength="1" Value="@_Prefix" ValueChanged="PrefixValueChanged"></RadzenTextBox>
</RadzenColumn>
</RadzenRow>
<RadzenRow>
<RadzenColumn>
<RadzenText>Bot Server Ids:</RadzenText>
@@ -33,14 +33,13 @@
</RadzenColumn>
</RadzenRow>
</ChildContent>
<FooterTemplate>
<RadzenButton Text="Save" Click="SaveChanges"></RadzenButton>
</FooterTemplate>
</RadzenPanel>
@code {
private string _Token = string.Empty;
private string _Prefix = string.Empty;
private string _ServerIds = string.Empty;
@@ -64,8 +63,8 @@
await Application.CurrentApplication.ApplicationEnvironmentVariables.SaveToFile();
DialogService.Close(true);
}
private void ServerIDsValueChanged(string obj)
{
_ServerIds = obj;

View File

@@ -9,28 +9,31 @@ namespace DiscordBotWebUI.DiscordBot;
public class DiscordBotStartup
{
public event EventHandler<string>? Log;
private Func<ModuleRequirement, Task> RequireInstallModule { get; }
public DiscordBotStartup(Func<ModuleRequirement, Task> requireInstallModule)
{
RequireInstallModule = requireInstallModule;
}
private void WriteLog(string message)
{
Log?.Invoke(this, message);
}
private readonly Func<ModuleRequirement, Task> RequireInstallModule;
public async Task<bool> LoadComponents()
public DiscordBotStartup(Func<ModuleRequirement, Task> requirementHandler)
{
this.RequireInstallModule = requirementHandler;
}
public async Task CreateApplication()
{
await Application.CreateApplication(RequireInstallModule);
}
public bool LoadComponents()
{
Application.Logger.SetOutFunction(WriteLog);
if (!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("ServerID") ||
!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("token") ||
!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("prefix"))
return false;
return Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("ServerID") &&
Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("token") &&
Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("prefix");
return true;
}
public async Task PrepareBot()

View File

@@ -1,4 +1,8 @@
using DiscordBotCore;
using DiscordBotCore.Others.Exceptions;
using DiscordBotWebUI.Components;
using DiscordBotWebUI.Components.Pages.Setup;
using DiscordBotWebUI.DiscordBot;
using Radzen;
var builder = WebApplication.CreateBuilder(args);
@@ -7,6 +11,7 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.Services.AddRadzenComponents();
var app = builder.Build();
// Configure the HTTP request pipeline.