Redesigned the DiscordBotCore by splitting it into multiple projects. Created a WebUI and preparing to remove the DiscordBot application
This commit is contained in:
32
WebUI/Controllers/HomeController.cs
Normal file
32
WebUI/Controllers/HomeController.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System.Diagnostics;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WebUI.Models;
|
||||
using ILogger = DiscordBotCore.Logging.ILogger;
|
||||
|
||||
namespace WebUI.Controllers;
|
||||
|
||||
public class HomeController : Controller
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
public HomeController(ILogger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public IActionResult Index()
|
||||
{
|
||||
_logger.Log("Index page loaded", this);
|
||||
return View();
|
||||
}
|
||||
|
||||
public IActionResult Privacy()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||
public IActionResult Error()
|
||||
{
|
||||
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
|
||||
}
|
||||
}
|
||||
38
WebUI/Controllers/PluginsController.cs
Normal file
38
WebUI/Controllers/PluginsController.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using DiscordBotCore.PluginManagement;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WebUI.Models;
|
||||
using ILogger = DiscordBotCore.Logging.ILogger;
|
||||
|
||||
namespace WebUI.Controllers;
|
||||
|
||||
public class PluginsController : Controller
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IPluginManager _pluginManager;
|
||||
|
||||
public PluginsController(ILogger logger, IPluginManager pluginManager)
|
||||
{
|
||||
_logger = logger;
|
||||
_pluginManager = pluginManager;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> OnlinePlugins()
|
||||
{
|
||||
_logger.Log("Getting plugins page", this);
|
||||
var plugins = await _pluginManager.GetPluginsList();
|
||||
|
||||
_logger.Log($"{plugins.Count} Plugins loaded", this);
|
||||
List<OnlinePluginViewModel> pluginViewModels = new List<OnlinePluginViewModel>();
|
||||
foreach (var plugin in plugins)
|
||||
{
|
||||
OnlinePluginViewModel pluginViewModel = new OnlinePluginViewModel();
|
||||
pluginViewModel.Name = plugin.PluginName;
|
||||
pluginViewModel.Description = plugin.PluginDescription;
|
||||
pluginViewModel.Author = plugin.PluginAuthor;
|
||||
pluginViewModel.Version = plugin.LatestVersion;
|
||||
pluginViewModel.DownloadUrl = plugin.PluginLink;
|
||||
}
|
||||
return View(pluginViewModels);
|
||||
}
|
||||
}
|
||||
70
WebUI/Controllers/SettingsController.cs
Normal file
70
WebUI/Controllers/SettingsController.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WebUI.Models;
|
||||
using IConfiguration = DiscordBotCore.Configuration.IConfiguration;
|
||||
using ILogger = DiscordBotCore.Logging.ILogger;
|
||||
|
||||
namespace WebUI.Controllers;
|
||||
|
||||
public class SettingsController : Controller
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IConfiguration _configuration;
|
||||
public SettingsController(ILogger logger, IConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
_logger.Log("Getting settings page", this);
|
||||
SettingsViewModel model = new SettingsViewModel
|
||||
{
|
||||
Token = _configuration.Get<string>("token", string.Empty),
|
||||
Prefix = _configuration.Get<string>("prefix", string.Empty),
|
||||
ServerIds = _configuration.Get<List<ulong>>("serverIds", new List<ulong>()),
|
||||
};
|
||||
|
||||
return View(model);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> SaveSettings([FromForm] SettingsViewModel model)
|
||||
{
|
||||
_logger.Log("Saving settings", this);
|
||||
|
||||
if (string.IsNullOrEmpty(model.Token))
|
||||
{
|
||||
ModelState.AddModelError("Token", "Token cannot be empty");
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(model.Prefix))
|
||||
{
|
||||
ModelState.AddModelError("Prefix", "Prefix cannot be empty");
|
||||
}
|
||||
|
||||
if (model.ServerIds == null || model.ServerIds.Count == 0)
|
||||
{
|
||||
ModelState.AddModelError("ServerIds", "At least one Server ID must be provided");
|
||||
}
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return View("Index", model);
|
||||
}
|
||||
|
||||
_configuration.Set("token", model.Token);
|
||||
_configuration.Set("prefix", model.Prefix);
|
||||
_configuration.Set("serverIds", model.ServerIds);
|
||||
|
||||
await _configuration.SaveToFile();
|
||||
_logger.Log("Settings saved successfully", this);
|
||||
|
||||
TempData["Notification"] = "Settings saved successfully!";
|
||||
|
||||
return RedirectToAction("Index", "Home");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user