Updated WebUI
This commit is contained in:
92
DiscordBotWebUI/DiscordBot/Commands/NormalCommands/Help.cs
Normal file
92
DiscordBotWebUI/DiscordBot/Commands/NormalCommands/Help.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using System.Collections.Generic;
|
||||
using Discord;
|
||||
using DiscordBotCore;
|
||||
using DiscordBotCore.Interfaces;
|
||||
using DiscordBotCore.Loaders;
|
||||
using DiscordBotCore.Others;
|
||||
|
||||
namespace DiscordBotWebUI.DiscordBot.Commands.NormalCommands;
|
||||
|
||||
/// <summary>
|
||||
/// The help command
|
||||
/// </summary>
|
||||
internal class Help: IDbCommand
|
||||
{
|
||||
/// <summary>
|
||||
/// Command name
|
||||
/// </summary>
|
||||
public string Command => "help";
|
||||
|
||||
public List<string> Aliases => null;
|
||||
|
||||
/// <summary>
|
||||
/// Command Description
|
||||
/// </summary>
|
||||
public string Description => "This command allows you to check all loaded commands";
|
||||
|
||||
/// <summary>
|
||||
/// Command usage
|
||||
/// </summary>
|
||||
public string Usage => "help <command>";
|
||||
|
||||
/// <summary>
|
||||
/// Check if the command require administrator to be executed
|
||||
/// </summary>
|
||||
public bool RequireAdmin => false;
|
||||
|
||||
/// <summary>
|
||||
/// The main body of the command
|
||||
/// </summary>
|
||||
/// <param name="context">The command context</param>
|
||||
public void ExecuteServer(DbCommandExecutingArguments args)
|
||||
{
|
||||
if (args.Arguments is not null)
|
||||
{
|
||||
var e = GenerateHelpCommand(args.Arguments[0]);
|
||||
if (e is null)
|
||||
args.Context.Channel.SendMessageAsync("Unknown Command " + args.Arguments[0]);
|
||||
else
|
||||
args.Context.Channel.SendMessageAsync(embed: e.Build());
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var embedBuilder = new EmbedBuilder();
|
||||
|
||||
var adminCommands = "";
|
||||
var normalCommands = "";
|
||||
|
||||
foreach (var cmd in PluginLoader.Commands)
|
||||
if (cmd.RequireAdmin)
|
||||
adminCommands += cmd.Command + " ";
|
||||
else
|
||||
normalCommands += cmd.Command + " ";
|
||||
|
||||
|
||||
if (adminCommands.Length > 0)
|
||||
embedBuilder.AddField("Admin Commands", adminCommands);
|
||||
if (normalCommands.Length > 0)
|
||||
embedBuilder.AddField("Normal Commands", normalCommands);
|
||||
args.Context.Channel.SendMessageAsync(embed: embedBuilder.Build());
|
||||
}
|
||||
|
||||
private EmbedBuilder GenerateHelpCommand(string command)
|
||||
{
|
||||
var embedBuilder = new EmbedBuilder();
|
||||
var cmd = PluginLoader.Commands.Find(p => p.Command == command ||
|
||||
p.Aliases is not null && p.Aliases.Contains(command)
|
||||
);
|
||||
if (cmd == null) return null;
|
||||
|
||||
string prefix = Application.CurrentApplication.ApplicationEnvironmentVariables.Get<string>("prefix");
|
||||
|
||||
embedBuilder.AddField("Usage", prefix + cmd.Usage);
|
||||
embedBuilder.AddField("Description", cmd.Description);
|
||||
if (cmd.Aliases is null)
|
||||
return embedBuilder;
|
||||
embedBuilder.AddField("Alias", cmd.Aliases.Count == 0 ? "-" : string.Join(", ", cmd.Aliases));
|
||||
|
||||
return embedBuilder;
|
||||
}
|
||||
}
|
||||
64
DiscordBotWebUI/DiscordBot/Commands/SlashCommands/Help.cs
Normal file
64
DiscordBotWebUI/DiscordBot/Commands/SlashCommands/Help.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
using DiscordBotCore.Interfaces;
|
||||
using DiscordBotCore.Loaders;
|
||||
using DiscordBotCore.Others;
|
||||
|
||||
namespace DiscordBotWebUI.DiscordBot.Commands.SlashCommands;
|
||||
|
||||
public class Help: IDbSlashCommand
|
||||
{
|
||||
public string Name => "help";
|
||||
public string Description => "This command allows you to check all loaded commands";
|
||||
public bool CanUseDm => true;
|
||||
|
||||
public bool HasInteraction => false;
|
||||
|
||||
public List<SlashCommandOptionBuilder> Options =>
|
||||
new()
|
||||
{
|
||||
new SlashCommandOptionBuilder()
|
||||
.WithName("command")
|
||||
.WithDescription("The command you want to get help for")
|
||||
.WithRequired(false)
|
||||
.WithType(ApplicationCommandOptionType.String)
|
||||
};
|
||||
|
||||
public async void ExecuteServer(SocketSlashCommand context)
|
||||
{
|
||||
EmbedBuilder embedBuilder = new();
|
||||
|
||||
embedBuilder.WithTitle("Help Command");
|
||||
|
||||
var random = new Random();
|
||||
Color c = new Color(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
|
||||
|
||||
embedBuilder.WithColor(c);
|
||||
var slashCommands = PluginLoader.SlashCommands;
|
||||
var options = context.Data.Options;
|
||||
|
||||
//Console.WriteLine("Options: " + options.Count);
|
||||
if (options is null || options.Count == 0)
|
||||
foreach (var slashCommand in slashCommands)
|
||||
embedBuilder.AddField(slashCommand.Name, slashCommand.Description);
|
||||
|
||||
if (options.Count > 0)
|
||||
{
|
||||
var commandName = options.First().Value;
|
||||
var slashCommand = slashCommands.FirstOrDefault(x => x.Name.TrimEnd() == commandName.ToString());
|
||||
if (slashCommand is null)
|
||||
{
|
||||
await context.RespondAsync("Unknown Command " + commandName);
|
||||
return;
|
||||
}
|
||||
|
||||
embedBuilder.AddField("DM Usable:", slashCommand.CanUseDm, true)
|
||||
.WithDescription(slashCommand.Description);
|
||||
}
|
||||
|
||||
await context.RespondAsync(embed: embedBuilder.Build());
|
||||
}
|
||||
}
|
||||
88
DiscordBotWebUI/DiscordBot/DiscordApplication.cs
Normal file
88
DiscordBotWebUI/DiscordBot/DiscordApplication.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using DiscordBotCore;
|
||||
using DiscordBotCore.Bot;
|
||||
using DiscordBotCore.Loaders;
|
||||
using DiscordBotCore.Others;
|
||||
|
||||
namespace DiscordBotWebUI.DiscordBot;
|
||||
|
||||
public class DiscordApplication
|
||||
{
|
||||
public bool IsRunning { get; private set; }
|
||||
private Action<string, LogType> LogWriter { get; set; }
|
||||
public DiscordApplication(Action<string, LogType> logWriter)
|
||||
{
|
||||
this.LogWriter = logWriter;
|
||||
IsRunning = false;
|
||||
}
|
||||
|
||||
private async Task<bool> LoadComponents()
|
||||
{
|
||||
if (!Application.IsRunning)
|
||||
{
|
||||
await Application.CreateApplication();
|
||||
}
|
||||
|
||||
Application.CurrentApplication.Logger.SetOutFunction(LogWriter);
|
||||
|
||||
return Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("ServerID") &&
|
||||
Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("token") &&
|
||||
Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("prefix");
|
||||
|
||||
}
|
||||
|
||||
public async Task<bool> Start()
|
||||
{
|
||||
if (!await LoadComponents())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var token = Application.CurrentApplication.ApplicationEnvironmentVariables.Get<string>("token");
|
||||
var prefix = Application.CurrentApplication.ApplicationEnvironmentVariables.Get<string>("prefix");
|
||||
|
||||
var coreApplication = new DiscordBotApplication(token, prefix);
|
||||
await coreApplication.StartAsync();
|
||||
|
||||
IsRunning = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task RefreshPlugins(bool quiet)
|
||||
{
|
||||
await LoadPlugins(quiet ? ["-q"] : null);
|
||||
await InitializeInternalActionManager();
|
||||
}
|
||||
|
||||
private async Task LoadPlugins(string[]? args)
|
||||
{
|
||||
var loader = new PluginLoader(Application.CurrentApplication.DiscordBotClient.Client);
|
||||
if (args is not null && args.Contains("-q"))
|
||||
{
|
||||
await loader.LoadPlugins();
|
||||
}
|
||||
|
||||
loader.OnCommandLoaded += (command) => {
|
||||
Application.CurrentApplication.Logger.Log($"Command {command.Command} loaded successfully", LogType.Info);
|
||||
};
|
||||
|
||||
loader.OnEventLoaded += (eEvent) => {
|
||||
Application.CurrentApplication.Logger.Log($"Event {eEvent.Name} loaded successfully", LogType.Info);
|
||||
};
|
||||
|
||||
loader.OnActionLoaded += (action) => {
|
||||
Application.CurrentApplication.Logger.Log($"Action {action.ActionName} loaded successfully", LogType.Info);
|
||||
};
|
||||
|
||||
loader.OnSlashCommandLoaded += (slashCommand) => {
|
||||
Application.CurrentApplication.Logger.Log($"Slash Command {slashCommand.Name} loaded successfully", LogType.Info);
|
||||
};
|
||||
|
||||
await loader.LoadPlugins();
|
||||
}
|
||||
|
||||
private async Task InitializeInternalActionManager()
|
||||
{
|
||||
await Application.CurrentApplication.InternalActionManager.Initialize();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user