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 System.Threading.Tasks;
using DeukBot4.MessageHandlers.CommandHandler.RequestStructure; using DeukBot4.MessageHandlers.CommandHandler.RequestStructure;
using DeukBot4.MessageHandlers.Permissions; using DeukBot4.MessageHandlers.Permissions;
@ -41,6 +42,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler
} }
public string Name { get; } public string Name { get; }
public List<string> Alternatives { get; } = new List<string>();
public PermissionLevel Permission { get; } public PermissionLevel Permission { get; }
public string ShortHelp { get; } public string ShortHelp { get; }
public string LongHelp { get; } public string LongHelp { get; }

View File

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

View File

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

View File

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