Updated the plugin webpage

This commit is contained in:
2024-12-14 19:11:50 +02:00
parent dee4793176
commit a12aa66660
9 changed files with 159 additions and 70 deletions

View File

@@ -1,6 +1,7 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using DiscordBotCore.Interfaces.API; using DiscordBotCore.Interfaces.API;
using DiscordBotCore.Others; using DiscordBotCore.Others;
using DiscordBotCore.Plugin;
namespace DiscordBotCore.API.Endpoints.PluginManagement; namespace DiscordBotCore.API.Endpoints.PluginManagement;
@@ -11,7 +12,9 @@ public class PluginListEndpoint : IEndpoint
public async Task<ApiResponse> HandleRequest(string? jsonRequest) public async Task<ApiResponse> HandleRequest(string? jsonRequest)
{ {
var onlineInfos = await Application.CurrentApplication.PluginManager.GetPluginsList(); var onlineInfos = await Application.CurrentApplication.PluginManager.GetPluginsList();
var response = await JsonManager.ConvertToJsonString(onlineInfos);
var response = await JsonManager.ConvertToJson(onlineInfos, [nameof(PluginOnlineInfo.Name), nameof(PluginOnlineInfo.Author), nameof(PluginOnlineInfo.Version)]);
return ApiResponse.From(response, true); return ApiResponse.From(response, true);
} }
} }

View File

@@ -1,7 +1,11 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Reflection;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace DiscordBotCore.Others; namespace DiscordBotCore.Others;
@@ -9,6 +13,37 @@ namespace DiscordBotCore.Others;
public static class JsonManager public static class JsonManager
{ {
public static async Task<string> ConvertToJson<T>(List<T> data, string[] propertyNamesToUse)
{
if (data == null) throw new ArgumentNullException(nameof(data));
if (propertyNamesToUse == null) throw new ArgumentNullException(nameof(propertyNamesToUse));
// Use reflection to filter properties dynamically
var filteredData = data.Select(item =>
{
if (item == null) return null;
var type = typeof(T);
var propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
// Create a dictionary with specified properties and their values
var selectedProperties = propertyInfos
.Where(p => propertyNamesToUse.Contains(p.Name))
.ToDictionary(p => p.Name, p => p.GetValue(item));
return selectedProperties;
}).ToList();
// Serialize the filtered data to JSON
var options = new JsonSerializerOptions
{
WriteIndented = true, // For pretty-print JSON; remove if not needed
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
return await Task.FromResult(JsonSerializer.Serialize(filteredData, options));
}
public static async Task<string> ConvertToJsonString<T>(T Data) public static async Task<string> ConvertToJsonString<T>(T Data)
{ {
var str = new MemoryStream(); var str = new MemoryStream();

View File

@@ -0,0 +1,22 @@
@using DiscordBotWebUI.Models
<RadzenCard>
<RadzenDataGrid Data="PluginModels" AllowPaging="true" AllowColumnResize="true" PageSize="5">
<Columns>
<RadzenDataGridColumn Property="@nameof(PluginModel.PluginName)" Title="Plugin Name"/>
<RadzenDataGridColumn Property="@nameof(PluginModel.PluginAuthor)" Title="Author"/>
<RadzenDataGridColumn Property="@nameof(PluginModel.PluginDescription)" Title="Version"/>
<RadzenDataGridColumn Title="Download">
<Template Context="item">
<RadzenButton Click="() => OnPluginDownloadClick!(item.PluginName)" Text="Download"/>
</Template>
</RadzenDataGridColumn>
</Columns>
</RadzenDataGrid>
</RadzenCard>
@code {
[Parameter]
public required List<PluginModel> PluginModels { get; set; }
[Parameter]
public required Action<string> OnPluginDownloadClick { get; set; }
}

View File

@@ -0,0 +1,66 @@
@inject DialogService DialogService
<div style="display: flex; justify-content: center; align-items: center; height: 100vh; background-color: transparent;">
<RadzenCard Style="padding: 2rem; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.2); width: 50%; max-width: 600px;">
<RadzenStack Orientation="Orientation.Vertical" Gap="20px" Style="color: white;">
<RadzenLabel Text="Enter your Discord bot token below:" />
<RadzenTextBox
id="token"
Name="token"
Placeholder="Enter your token here ..."
Style="width: 100%;"
bind-Value="@TokenString" />
<RadzenLabel Text="Specify the bot's prefix:" />
<RadzenTextBox
id="prefix"
Name="prefix"
Placeholder="Enter your prefix here ..."
Style="width: 100%;"
bind-Value="@PrefixString" />
<RadzenLabel Text="Enter server IDs (separated by semicolons):" />
<RadzenTextBox
id="server"
Name="server"
Placeholder="Enter server IDs here ..."
Style="width: 100%;"
bind-Value="@ServerIdsString" />
<RadzenButton Text="Save Changes" Click="SaveButtonClick" Style="margin-top: 1rem;" />
</RadzenStack>
</RadzenCard>
</div>
@code {
private string TokenString { get; set; }
private string PrefixString { get; set; }
private string ServerIdsString { get; set; }
[Parameter]
public required Action SaveSettings { get; set; }
private async void SaveButtonClick()
{
var response = await DialogService.Confirm("Saving this requires a bot restart.\nRestart now?", "Save Settings", new ConfirmOptions()
{
CloseDialogOnEsc = false,
ShowClose = false,
CloseDialogOnOverlayClick = false,
OkButtonText = "Restart Now",
CancelButtonText = "Don't Save"
});
if (!response.HasValue)
{
return;
}
if (!response.Value)
{
return;
}
SaveSettings.Invoke();
}
}

View File

@@ -1,5 +1,5 @@
@page "/" @page "/"
<PageTitle>Home page</PageTitle>
@code { @code {
} }

View File

@@ -1,5 +1,24 @@
@page "/plugins" @page "/plugins"
@using DiscordBotWebUI.Components.CustomTags
@using DiscordBotWebUI.Models
<Marketplace PluginModels="_PluginModels" OnPluginDownloadClick="OnModelSelected"/>
@code { @code {
private List<PluginModel> _PluginModels;
private async void OnModelSelected(string itemName)
{
Console.WriteLine(itemName);
}
protected override async Task OnInitializedAsync()
{
_PluginModels = new List<PluginModel>()
{
new PluginModel() {PluginName = "Test", PluginAuthor = "Andrei", PluginDescription = "Interesting plugin"}
};
}
} }

View File

@@ -1,65 +1,5 @@
@page "/settings" @page "/settings"
@inject DialogService DialogService; @using DiscordBotWebUI.Components.CustomTags
<PageTitle>Settings</PageTitle>
<div style="display: flex; justify-content: center; align-items: center; height: 100vh; background-color: transparent;"> <SettingsComponent/>
<RadzenCard Style="padding: 2rem; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.2); width: 50%; max-width: 600px;">
<RadzenStack Orientation="Orientation.Vertical" Gap="20px" Style="color: white;">
<RadzenLabel Text="Enter your Discord bot token below:" />
<RadzenTextBox
id="token"
Name="token"
Placeholder="Enter your token here ..."
Style="width: 100%;"
bind-Value="@TokenString" />
<RadzenLabel Text="Specify the bot's prefix:" />
<RadzenTextBox
id="prefix"
Name="prefix"
Placeholder="Enter your prefix here ..."
Style="width: 100%;"
bind-Value="@PrefixString" />
<RadzenLabel Text="Enter server IDs (separated by semicolons):" />
<RadzenTextBox
id="server"
Name="server"
Placeholder="Enter server IDs here ..."
Style="width: 100%;"
bind-Value="@ServerIdsString" />
<RadzenButton Text="Save Changes" Click="SaveButtonClick" Style="margin-top: 1rem;" />
</RadzenStack>
</RadzenCard>
</div>
@code {
private string TokenString { get; set; }
private string PrefixString { get; set; }
private string ServerIdsString { get; set; }
private async void SaveButtonClick()
{
var response = await DialogService.Confirm("Saving this requires a bot restart.\nRestart now?", "Save Settings", new ConfirmOptions()
{
CloseDialogOnEsc = false,
ShowClose = false,
CloseDialogOnOverlayClick = false,
OkButtonText = "Restart Now",
CancelButtonText = "Don't Save"
});
if (!response.HasValue)
{
return;
}
if (!response.Value)
{
return;
}
//TODO: Restart bot request
}
}

View File

@@ -7,10 +7,6 @@
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DiscordBotCore\DiscordBotCore.csproj" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Radzen.Blazor" Version="5.1.3" /> <PackageReference Include="Radzen.Blazor" Version="5.1.3" />
</ItemGroup> </ItemGroup>
@@ -43,7 +39,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Components\CustomTags\" /> <Folder Include="ServerCommunication\" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -0,0 +1,8 @@
namespace DiscordBotWebUI.Models;
public class PluginModel
{
public string PluginName { get; set; }
public string PluginAuthor { get; set; }
public string PluginDescription { get; set; }
}