mirror of
				https://gitlab.com/Deukhoofd/DeukBot4.git
				synced 2025-10-27 17:00:05 +00:00 
			
		
		
		
	Better handling of commands with alternative command names, hide duplicates from help
This commit is contained in:
		| @@ -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; } | ||||||
|   | |||||||
| @@ -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 (helpAttribute == null) | ||||||
|                 { |                 { | ||||||
|                     if (commandAttribute == null) |                     command = (new Command(firstCommand.Command, firstCommand.Permission, parameters, | ||||||
|                         continue; |                         forbidPm, matchParametersExactly, methodInfo, this)); | ||||||
|                     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)); |  | ||||||
|  |  | ||||||
|                     } |  | ||||||
|                 } |                 } | ||||||
|  |                 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(); |             return commands.ToArray(); | ||||||
|         } |         } | ||||||
|   | |||||||
| @@ -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( | ||||||
|   | |||||||
| @@ -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"); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user