Added Help Command and allowed empty collection instead of null for aliases in IDbCommand

This commit is contained in:
2025-04-28 12:08:24 +03:00
parent 7106a928d6
commit 16b6e42a97
4 changed files with 84 additions and 5 deletions

View File

@@ -15,7 +15,7 @@ public interface IDbCommand
/// <summary>
/// Command aliases. Users may use this to execute the command
/// </summary>
List<string>? Aliases { get; }
List<string> Aliases { get; }
/// <summary>
/// Command description

View File

@@ -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)

View File

@@ -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
/// <summary>
/// The main Boot constructor
/// </summary>
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()

View File

@@ -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<string> Aliases => [];
public string Description => "Help command for the bot.";
public string Usage => "help <command>";
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;
}
}