helpful help + forcing people learn regex

This commit is contained in:
rfi
2023-11-27 19:45:34 +07:00
parent e3acdede67
commit e8fc578a1b
3 changed files with 28 additions and 7 deletions

View File

@@ -88,8 +88,19 @@ namespace AscNet.GameServer.Commands
{
Command? cmd = CommandFactory.CreateCommand(command, session, args, false);
if (cmd is not null)
helpText += $"{command}\n\t└─{cmd.Help}\n";
{
List<PropertyInfo> argsProperties = cmd.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Where(x => x.GetCustomAttribute(typeof(ArgumentAttribute)) is not null).ToList();
helpText += $"{command} {string.Join(" ", argsProperties.Select(x => $"<{x.Name}>"))}\n└─{cmd.Help}\n";
foreach (var argProp in argsProperties)
{
ArgumentAttribute attr = (ArgumentAttribute)argProp.GetCustomAttribute(typeof(ArgumentAttribute))!;
helpText += string.Format($"└─{argProp.Name} \"{attr.Pattern}\"{{0}}\n", string.IsNullOrEmpty(attr.Description) ? string.Empty : $", {attr.Description}");
}
}
}
throw new CommandMessageCallbackException(helpText);
}
}
@@ -129,4 +140,10 @@ namespace AscNet.GameServer.Commands
return (Command)Activator.CreateInstance(command, new object[] { session, args, validate })!;
}
}
public class CommandMessageCallbackException : Exception
{
public CommandMessageCallbackException(string message)
: base(message) { }
}
}

View File

@@ -170,6 +170,10 @@ namespace AscNet.GameServer.Handlers
cmd?.Execute();
}
catch (CommandMessageCallbackException ex)
{
notifyWorldChat.ChatMessages.Add(MakeLuciaMessage(ex.Message));
}
catch (Exception)
{
notifyWorldChat.ChatMessages.Add(MakeLuciaMessage($"Command {cmdStrings.First().Split('/').Last()} failed to execute!"));