patch on discord bot UI

This commit is contained in:
2023-04-08 15:54:39 +03:00
parent 54a68d635d
commit 244209093e
12 changed files with 249 additions and 19 deletions

View File

@@ -18,24 +18,80 @@ namespace DiscordBotUI.Controllers
Config.Data["prefix"]?.Length != 1)
return RedirectToAction("Settings", "Home");
bool isReady = true;
if (DiscordBot.DiscordBot.Instance is null)
{
new DiscordBot.DiscordBot(Config.Data["token"], Config.Data["prefix"]);
await DiscordBot.DiscordBot.Instance.Start();
await DiscordBot.DiscordBot.Instance.LoadPlugins();
isReady=false;
await Task.Run(async() => {
new DiscordBot.DiscordBot(Config.Data["token"], Config.Data["prefix"]);
await DiscordBot.DiscordBot.Instance.Start();
await DiscordBot.DiscordBot.Instance.LoadPlugins();
new PluginManager.Items.ConsoleCommandsHandler(DiscordBot.DiscordBot.Instance._boot.client);
}).ContinueWith((t) => {
isReady = true;
});
}
while (!isReady)
await Task.Delay(100);
await Task.Delay(2000);
BotModel model = new BotModel();
model.StartStatus = DiscordBot.DiscordBot.Instance._boot.client.ConnectionState.ToString();
model.BotName = DiscordBot.DiscordBot.Instance._boot.client.CurrentUser.Username;
model.PluginsLoaded = PluginManager.Loaders.PluginLoader.PluginsLoaded;
model.SlashCommands = PluginManager.Loaders.PluginLoader.SlashCommands;
model.Events = PluginManager.Loaders.PluginLoader.Events;
model.Commands = PluginManager.Loaders.PluginLoader.Commands;
model.SlashCommands = PluginManager.Loaders.PluginLoader.SlashCommands ?? new List<PluginManager.Interfaces.DBSlashCommand>();
model.Events = PluginManager.Loaders.PluginLoader.Events ?? new List<PluginManager.Interfaces.DBEvent>();
model.Commands = PluginManager.Loaders.PluginLoader.Commands ?? new List<PluginManager.Interfaces.DBCommand>();
return View(model);
}
public async Task<IActionResult> PluginsPage()
{
if(DiscordBot.DiscordBot.Instance is null) {
return RedirectToAction("Start");
}
PluginsPageModel model = new PluginsPageModel();
if (PluginManager.Loaders.PluginLoader.Commands != null)
model.InstalledCommands = PluginManager.Loaders.PluginLoader.Commands.Select(x => x.Command).ToList();
else model.InstalledCommands = new List<string>();
if (PluginManager.Loaders.PluginLoader.Events != null)
model.InstalledEvents = PluginManager.Loaders.PluginLoader.Events.Select(x => x.Name).ToList();
else model.InstalledEvents = new List<string>();
if (PluginManager.Loaders.PluginLoader.SlashCommands != null)
model.InstalledSlashCommands = PluginManager.Loaders.PluginLoader.SlashCommands.Select(x => x.Name).ToList();
else model.InstalledSlashCommands = new List<string>();
model.PluginsManager = new("https://raw.githubusercontent.com/Wizzy69/installer/discord-bot-files/Plugins.txt");
List<string[]> plugins = await model.PluginsManager.ListAvailablePlugins();
if (plugins == null) return RedirectToAction("Start");
model.Plugins = plugins;
return View(model);
}
[HttpGet]
public async Task<int> InstallPlugin(string pluginName)
{
try
{
if(!DiscordBot.DiscordBot.Instance._boot.isReady)
return 1;
await PluginManager.Items.ConsoleCommandsHandler.ExecuteCommad("dwplug " + pluginName);
return 0;
}
catch (Exception ex)
{
Logger.LogError(ex);
Console.WriteLine(ex.ToString());
return 1;
}
}
}
}

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using PluginManager.Interfaces;
namespace DiscordBotUI.Models.Bot
{
public class PluginsPageModel
{
public List<string> InstalledCommands {get;set;}
public List<string> InstalledEvents {get;set;}
public List<string> InstalledSlashCommands {get;set;}
public List<string[]> Plugins {get;set;}
public PluginManager.Online.PluginsManager PluginsManager {get;set;}
}
}

View File

@@ -0,0 +1,72 @@
@model DiscordBotUI.Models.Bot.PluginsPageModel
<html>
<link rel="stylesheet" href="~/css/bot/plugins.css" asp-append-version="true" />
<table class="text-white">
<thead>
<tr>
<th>Plugin Name</th>
<th>Plugin Description</th>
<th>Plugin Type</th>
<th>Plugin Version</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Plugins)
{
@if (item.Contains("+") || item.Contains("-") || item[0].Length < 2 || item[0] == "Name") continue;
<tr>
<td>@item[0]</td>
<td>@item[1]</td>
<td>@item[2]</td>
<td>@item[3]</td>
@if (Model.InstalledCommands.Contains(item[0]) || Model.InstalledEvents.Contains(item[0]) ||
Model.InstalledSlashCommands.Contains(item[0]))
{
<td>Installed</td>
}
else
{
<td><input type="button" value="Install" onclick="install(this)" name="@item[0]" /></td>
}
<td>
<div class="demo-container" style="display: none;">
<div class="progress-bar">
<div class="progress-bar-value"></div>
</div>
</div>
</td>
</tr>
}
</tbody>
</table>
</html>
</html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<script type="text/javascript">
function install(button) {
var pluginName = button.name;
var progress = button.parentElement.nextElementSibling.firstElementChild;
progress.style.display = "";
$.ajax({
url: "/Bot/InstallPlugin",
type: "GET",
data: { pluginName: pluginName },
success: function (data) {
if (data == 0)
button.value = "Installed";
button.disabled = true;
progress.style.display = "none";
},
error: function (xhr, status, error) {
alert("Error: " + error);
}
});
}
</script>

View File

@@ -1,10 +1,12 @@
@model DiscordBotUI.Models.Bot.BotModel
<link rel="stylesheet" href="~/css/bot/start.css" asp-append-version="true" />
<label>Bot Name: @Model.BotName</label>
<label class="text-white">Bot Name: @Model.BotName</label>
<br />
<label>Connection Status: @Model.StartStatus</label>
<label class="text-white">Connection Status: @Model.StartStatus</label>
<br />
<label>Number of plugins loaded: @Model.PluginsLoaded</label>
<label class="text-white">Number of plugins loaded: @Model.PluginsLoaded</label>
<style>
* {
box-sizing: border-box;
@@ -45,7 +47,7 @@
}
</style>
<div class="row">
<div class="row text-white">
<div class="column">
<h2>Commands</h2>
<table>
@@ -57,7 +59,7 @@
</tr>
@foreach(var cmd in Model.Commands)
{
<tr>
<tr class="text-white bg-gradient" style="border: 0px; background-color: transparent;">
<td>@cmd.Command</td>
<td>@cmd.Description</td>
<td>@cmd.Usage</td>
@@ -81,7 +83,7 @@
</tr>
@foreach(var eve in Model.Events)
{
<tr>
<tr class="text-white bg-gradient" style="border: 0px; background-color: transparent;">
<td>@eve.Name</td>
<td>@eve.Description</td>
</tr>
@@ -99,7 +101,7 @@
</tr>
@foreach(var scmd in Model.SlashCommands)
{
<tr>
<tr class="text-white bg-gradient" style="border: 0px; background-color: transparent;">
<td>@scmd.Name</td>
<td>@scmd.Description</td>
@if (scmd.canUseDM)
@@ -113,4 +115,4 @@
}
</table>
</div>
</div>
</div>

View File

@@ -23,6 +23,9 @@
<li class="nav-item">
<a class="nav-link text-light" asp-area="" asp-controller="Bot" asp-action="Start">Start Bot</a>
</li>
<li class="nav-item">
<a class="nav-link text-light" asp-area="" asp-controller="Bot" asp-action="PluginsPage">Plugins</a>
</li>
</ul>
</div>
</div>

View File

@@ -0,0 +1,44 @@
html {
height: 100%;
}
body {
margin: 0;
padding: 25px;
font-family: sans-serif;
background: linear-gradient(#141e30, #243b55);
}
.demo-container {
width: 300px;
margin: auto;
}
.progress-bar {
height: 4px;
background-color: rgba(5, 114, 206, 0.2);
width: 100%;
overflow: hidden;
}
.progress-bar-value {
width: 100%;
height: 100%;
background-color: rgb(5, 114, 206);
animation: indeterminateAnimation 1s infinite linear;
transform-origin: 0% 50%;
}
@keyframes indeterminateAnimation {
0% {
transform: translateX(0) scaleX(0);
}
40% {
transform: translateX(0) scaleX(0.4);
}
100% {
transform: translateX(100%) scaleX(0.5);
}
}

View File

@@ -0,0 +1,10 @@
html {
height: 100%;
}
body {
margin: 0;
padding: 0;
font-family: sans-serif;
background: linear-gradient(#141e30, #243b55);
}