Added Help Command and allowed empty collection instead of null for aliases in IDbCommand
This commit is contained in:
@@ -15,7 +15,7 @@ public interface IDbCommand
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Command aliases. Users may use this to execute the command
|
/// Command aliases. Users may use this to execute the command
|
||||||
/// </summary>
|
/// </summary>
|
||||||
List<string>? Aliases { get; }
|
List<string> Aliases { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Command description
|
/// Command description
|
||||||
|
|||||||
@@ -112,7 +112,6 @@ internal class CommandHandler : ICommandHandler
|
|||||||
.FirstOrDefault(plug => plug.Command ==
|
.FirstOrDefault(plug => plug.Command ==
|
||||||
message.Content.Substring(mentionPrefix.Length + 1)
|
message.Content.Substring(mentionPrefix.Length + 1)
|
||||||
.Split(' ')[0] ||
|
.Split(' ')[0] ||
|
||||||
plug.Aliases is not null &&
|
|
||||||
plug.Aliases.Contains(message.CleanContent
|
plug.Aliases.Contains(message.CleanContent
|
||||||
.Substring(mentionPrefix.Length + 1)
|
.Substring(mentionPrefix.Length + 1)
|
||||||
.Split(' ')[0]
|
.Split(' ')[0]
|
||||||
@@ -127,7 +126,6 @@ internal class CommandHandler : ICommandHandler
|
|||||||
plugin = _pluginLoader.Commands!
|
plugin = _pluginLoader.Commands!
|
||||||
.FirstOrDefault(p => p.Command ==
|
.FirstOrDefault(p => p.Command ==
|
||||||
message.Content.Split(' ')[0].Substring(_botPrefix.Length) ||
|
message.Content.Split(' ')[0].Substring(_botPrefix.Length) ||
|
||||||
p.Aliases is not null &&
|
|
||||||
p.Aliases.Contains(
|
p.Aliases.Contains(
|
||||||
message.Content.Split(' ')[0]
|
message.Content.Split(' ')[0]
|
||||||
.Substring(_botPrefix.Length)
|
.Substring(_botPrefix.Length)
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ namespace DiscordBotCore.Bot;
|
|||||||
|
|
||||||
public class DiscordBotApplication : IDiscordBotApplication
|
public class DiscordBotApplication : IDiscordBotApplication
|
||||||
{
|
{
|
||||||
|
internal static IPluginLoader _InternalPluginLoader;
|
||||||
|
|
||||||
private static readonly string _DefaultPrefix = ";";
|
private static readonly string _DefaultPrefix = ";";
|
||||||
|
|
||||||
private CommandHandler _CommandServiceHandler;
|
private CommandHandler _CommandServiceHandler;
|
||||||
@@ -27,11 +29,13 @@ public class DiscordBotApplication : IDiscordBotApplication
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The main Boot constructor
|
/// The main Boot constructor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public DiscordBotApplication(ILogger logger, IConfiguration configuration, IPluginLoader pluginLoaderOld)
|
public DiscordBotApplication(ILogger logger, IConfiguration configuration, IPluginLoader pluginLoader)
|
||||||
{
|
{
|
||||||
this._Logger = logger;
|
this._Logger = logger;
|
||||||
this._Configuration = configuration;
|
this._Configuration = configuration;
|
||||||
this._pluginLoader = pluginLoaderOld;
|
this._pluginLoader = pluginLoader;
|
||||||
|
|
||||||
|
_InternalPluginLoader = pluginLoader;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task StopAsync()
|
public async Task StopAsync()
|
||||||
|
|||||||
77
DiscordBotCore/Commands/HelpCommand.cs
Normal file
77
DiscordBotCore/Commands/HelpCommand.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user