From 16b6e42a97cd03c2398edbc553e0b94f46976e7f Mon Sep 17 00:00:00 2001 From: Andrei Tudor Date: Mon, 28 Apr 2025 12:08:24 +0300 Subject: [PATCH] Added Help Command and allowed empty collection instead of null for aliases in IDbCommand --- .../Interfaces/IDbCommand.cs | 2 +- DiscordBotCore/Bot/CommandHandler.cs | 2 - DiscordBotCore/Bot/DiscordBotApplication.cs | 8 +- DiscordBotCore/Commands/HelpCommand.cs | 77 +++++++++++++++++++ 4 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 DiscordBotCore/Commands/HelpCommand.cs diff --git a/DiscordBotCore.PluginCore/Interfaces/IDbCommand.cs b/DiscordBotCore.PluginCore/Interfaces/IDbCommand.cs index 1761b82..b0f3c93 100644 --- a/DiscordBotCore.PluginCore/Interfaces/IDbCommand.cs +++ b/DiscordBotCore.PluginCore/Interfaces/IDbCommand.cs @@ -15,7 +15,7 @@ public interface IDbCommand /// /// Command aliases. Users may use this to execute the command /// - List? Aliases { get; } + List Aliases { get; } /// /// Command description diff --git a/DiscordBotCore/Bot/CommandHandler.cs b/DiscordBotCore/Bot/CommandHandler.cs index f681042..8524af0 100644 --- a/DiscordBotCore/Bot/CommandHandler.cs +++ b/DiscordBotCore/Bot/CommandHandler.cs @@ -112,7 +112,6 @@ internal class CommandHandler : ICommandHandler .FirstOrDefault(plug => plug.Command == message.Content.Substring(mentionPrefix.Length + 1) .Split(' ')[0] || - plug.Aliases is not null && plug.Aliases.Contains(message.CleanContent .Substring(mentionPrefix.Length + 1) .Split(' ')[0] @@ -127,7 +126,6 @@ internal class CommandHandler : ICommandHandler plugin = _pluginLoader.Commands! .FirstOrDefault(p => p.Command == message.Content.Split(' ')[0].Substring(_botPrefix.Length) || - p.Aliases is not null && p.Aliases.Contains( message.Content.Split(' ')[0] .Substring(_botPrefix.Length) diff --git a/DiscordBotCore/Bot/DiscordBotApplication.cs b/DiscordBotCore/Bot/DiscordBotApplication.cs index 6880d8d..4ca394f 100644 --- a/DiscordBotCore/Bot/DiscordBotApplication.cs +++ b/DiscordBotCore/Bot/DiscordBotApplication.cs @@ -12,6 +12,8 @@ namespace DiscordBotCore.Bot; public class DiscordBotApplication : IDiscordBotApplication { + internal static IPluginLoader _InternalPluginLoader; + private static readonly string _DefaultPrefix = ";"; private CommandHandler _CommandServiceHandler; @@ -27,11 +29,13 @@ public class DiscordBotApplication : IDiscordBotApplication /// /// The main Boot constructor /// - public DiscordBotApplication(ILogger logger, IConfiguration configuration, IPluginLoader pluginLoaderOld) + public DiscordBotApplication(ILogger logger, IConfiguration configuration, IPluginLoader pluginLoader) { this._Logger = logger; this._Configuration = configuration; - this._pluginLoader = pluginLoaderOld; + this._pluginLoader = pluginLoader; + + _InternalPluginLoader = pluginLoader; } public async Task StopAsync() diff --git a/DiscordBotCore/Commands/HelpCommand.cs b/DiscordBotCore/Commands/HelpCommand.cs new file mode 100644 index 0000000..9cae45c --- /dev/null +++ b/DiscordBotCore/Commands/HelpCommand.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +using Discord; +using DiscordBotCore.Bot; +using DiscordBotCore.PluginCore.Helpers.Execution.DbCommand; +using DiscordBotCore.PluginCore.Interfaces; + +namespace DiscordBotCore.Commands; + +public class HelpCommand : IDbCommand +{ + public string Command => "help"; + public List Aliases => []; + public string Description => "Help command for the bot."; + public string Usage => "help "; + public bool RequireAdmin => false; + + public async Task ExecuteServer(IDbCommandExecutingArgument args) + { + if (args.Arguments is not null) + { + string searchedCommand = args.Arguments[0]; + IDbCommand? command = DiscordBotApplication._InternalPluginLoader.Commands.FirstOrDefault(c => c.Command.Equals(searchedCommand, StringComparison.OrdinalIgnoreCase)); + + if (command is null) + { + await args.Context.Channel.SendMessageAsync($"Command `{searchedCommand}` not found."); + return; + } + + EmbedBuilder helpEmbed = GenerateHelpCommand(command); + await args.Context.Channel.SendMessageAsync(embed: helpEmbed.Build()); + return; + } + + if (DiscordBotApplication._InternalPluginLoader.Commands.Count == 0) + { + await args.Context.Channel.SendMessageAsync("No commands found."); + return; + } + + var embedBuilder = new EmbedBuilder(); + + var adminCommands = ""; + var normalCommands = ""; + + foreach (var cmd in DiscordBotApplication._InternalPluginLoader.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); + await args.Context.Channel.SendMessageAsync(embed: embedBuilder.Build()); + } + + private EmbedBuilder GenerateHelpCommand(IDbCommand command) + { + EmbedBuilder builder = new(); + builder.WithTitle($"Command: {command.Command}"); + builder.WithDescription(command.Description); + builder.WithColor(Color.Blue); + builder.AddField("Usage", command.Usage); + string aliases = ""; + foreach (var alias in command.Aliases) + aliases += alias + " "; + builder.AddField("Aliases", aliases.Length > 0 ? aliases : "None"); + return builder; + } +} \ No newline at end of file