CurrencyAlertClassic/KamiLib/ChatCommands/HelpCommands.cs

92 lines
2.6 KiB
C#
Raw Permalink Normal View History

2023-10-13 09:38:52 +00:00
using System.Collections.Generic;
using System.Linq;
using KamiLib.Interfaces;
using KamiLib.Localization;
namespace KamiLib.ChatCommands;
public class HelpCommands: IPluginCommand
{
public string CommandArgument => "help";
public IEnumerable<ISubCommand> SubCommands { get; } = new List<ISubCommand>
{
new SubCommand
{
CommandKeyword = null,
CommandAction = () =>
{
foreach (var command in KamiCommon.CommandManager.Commands)
{
PrintSubCommands(command);
}
},
GetHelpText = () => Strings.Command_ShowThisMessage
}
};
private static void PrintSubCommands(IPluginCommand command)
{
foreach (var subCommand in command.SubCommands.GroupBy(subCommand => subCommand.GetCommand()))
{
var selectedSubCommand = subCommand.First();
if (!selectedSubCommand.Hidden)
{
PrintHelpText(command, selectedSubCommand);
if (selectedSubCommand.GetAliases() is { } aliases)
{
foreach (var alias in aliases)
{
PrintAliasHelpText(command, alias, selectedSubCommand);
}
}
}
}
}
private static void PrintHelpText(IPluginCommand mainCommand, ISubCommand subCommand)
{
var commandString = $"/{KamiCommon.PluginName.ToLower()} ";
if (mainCommand.CommandArgument is not null)
{
commandString += mainCommand.CommandArgument + " ";
}
if (subCommand.GetCommand() is not null)
{
commandString += subCommand.GetCommand() + " ";
}
if (subCommand.HasParameterAction)
{
commandString += $"[{Strings.Common_Value}] ";
}
Chat.PrintHelp(commandString, subCommand.GetHelpText());
}
private static void PrintAliasHelpText(IPluginCommand mainCommand, string? alias, ISubCommand subCommand)
{
var commandString = $"/{KamiCommon.PluginName.ToLower()} ";
if (mainCommand.CommandArgument is not null)
{
commandString += mainCommand.CommandArgument + " ";
}
if (alias is not null)
{
commandString += alias + " ";
}
if (subCommand.HasParameterAction)
{
commandString += $"[{Strings.Common_Value}] ";
}
Chat.PrintHelp(commandString, subCommand.GetHelpText());
}
}