Better handling of commands with alternative command names, hide duplicates from help

This commit is contained in:
Deukhoofd 2018-05-06 19:15:42 +02:00
parent b4d795f843
commit ac79b1e4de
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
4 changed files with 32 additions and 18 deletions

View File

@ -1,4 +1,5 @@
using System.Reflection;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using DeukBot4.MessageHandlers.CommandHandler.RequestStructure;
using DeukBot4.MessageHandlers.Permissions;
@ -41,6 +42,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler
}
public string Name { get; }
public List<string> Alternatives { get; } = new List<string>();
public PermissionLevel Permission { get; }
public string ShortHelp { get; }
public string LongHelp { get; }

View File

@ -19,7 +19,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler
{
// grab all command attributes, cast them properly
var commandAttributes = methodInfo.GetCustomAttributes(typeof(CommandAttribute), true)
.Select(x => x as CommandAttribute);
.Select(x => x as CommandAttribute).ToArray();
// get the help attribute if it exists
CommandHelpAttribute helpAttribute = null;
@ -40,24 +40,28 @@ namespace DeukBot4.MessageHandlers.CommandHandler
var matchParametersExactly =
methodInfo.GetCustomAttributes(typeof(RequireParameterMatchAttribute), true).Any();
foreach (var commandAttribute in commandAttributes)
var firstCommand = commandAttributes.First();
Command command;
if (helpAttribute == null)
{
if (commandAttribute == null)
continue;
if (helpAttribute == null)
{
commands.Add(new Command(commandAttribute.Command, commandAttribute.Permission, parameters,
forbidPm, matchParametersExactly, methodInfo, this));
}
else
{
commands.Add(new Command(commandAttribute.Command, commandAttribute.Permission,
helpAttribute.ShortHelp, helpAttribute.LongHelp, parameters, forbidPm,
matchParametersExactly, methodInfo, this));
}
command = (new Command(firstCommand.Command, firstCommand.Permission, parameters,
forbidPm, matchParametersExactly, methodInfo, this));
}
else
{
command = (new Command(firstCommand.Command, firstCommand.Permission,
helpAttribute.ShortHelp, helpAttribute.LongHelp, parameters, forbidPm,
matchParametersExactly, methodInfo, this));
}
for (var i = 1; i < commandAttributes.Length; i++)
{
var cmd = commandAttributes[i];
if (cmd == null)
continue;
command.Alternatives.Add(cmd.Command);
}
commands.Add(command);
}
return commands.ToArray();
}

View File

@ -29,6 +29,10 @@ namespace DeukBot4.MessageHandlers.CommandHandler
foreach (var command in commands)
{
Commands.Add(command.Name.ToLowerInvariant(), command);
foreach (var commandAlternative in command.Alternatives)
{
Commands.Add(commandAlternative.ToLowerInvariant(), command);
}
}
Logger.Log(

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using DeukBot4.MessageHandlers.Permissions;
using Discord;
@ -17,6 +18,8 @@ namespace DeukBot4.MessageHandlers.CommandHandler
continue;
if (!command.Value.HasHelp)
continue;
if (command.Value.Alternatives.Contains(command.Key))
continue;
if (!dic.TryGetValue(command.Value.CommandContainer.Name, out var entry))
{
dic.Add(command.Value.CommandContainer.Name, new Dictionary<string, string>());
@ -25,6 +28,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler
entry.Add(command.Value.Name, command.Value.ShortHelp);
}
var sb = new StringBuilder();
Console.WriteLine(dic.Count);
foreach (var entry in dic)
{
sb.Append($"**{entry.Key}**\n");