internal CommandBuilder(CommandService service, Command command, string prefix = "", string category = "", IEnumerable<IPermissionChecker> initialChecks = null) { _service = service; _command = command; _command.Category = category; _params = new List<CommandParameter>(); if (initialChecks != null) _checks = new List<IPermissionChecker>(initialChecks); else _checks = new List<IPermissionChecker>(); _prefix = prefix; _aliases = new List<string>(); _allowRequiredParams = true; _areParamsClosed = false; }
internal CommandBuilder(CommandService service, string text, string prefix = "", string category = "", IEnumerable<IPermissionChecker> initialChecks = null, bool defaultNsfwFlag = false, bool defaultMusicFlag = false) { _service = service; _prefix = prefix; _command = new Command(AppendPrefix(prefix, text)) { NsfwFlag = defaultNsfwFlag, MusicFlag = defaultMusicFlag }; _command.Category = category; if (initialChecks != null) _checks = new List<IPermissionChecker>(initialChecks); else _checks = new List<IPermissionChecker>(); _params = new List<CommandParameter>(); _aliases = new List<string>(); _allowRequiredParams = true; _areParamsClosed = false; }
public CommandBuilder CreateCommand(string cmd) { var command = new Command(CommandBuilder.AppendPrefix(_prefix, cmd)); command.NsfwFlag = _defaultNsfwFlag; command.MusicFlag = _defaultMusicFlag; return new CommandBuilder(_service, command, _prefix, _category, _checks); }
//TODO: Check support for escaping public static CommandErrorType? ParseArgs(string input, int startPos, Command command, out string[] args) { CommandParserPart currentPart = CommandParserPart.None; int startPosition = startPos; int endPosition = startPos; int inputLength = input.Length; bool isEscaped = false; var expectedArgs = command._parameters; List<string> argList = new List<string>(); CommandParameter parameter = null; args = null; if (input == "") return CommandErrorType.InvalidInput; while (endPosition < inputLength) { if (startPosition == endPosition && (parameter == null || parameter.Type != ParameterType.Multiple)) //Is first char of a new arg { if (argList.Count >= expectedArgs.Length) return CommandErrorType.BadArgCount; //Too many args parameter = expectedArgs[argList.Count]; if (parameter.Type == ParameterType.Unparsed) { argList.Add(input.Substring(startPosition)); break; } } char currentChar = input[endPosition++]; if (isEscaped) isEscaped = false; else if (currentChar == '\\') isEscaped = true; switch (currentPart) { case CommandParserPart.None: if ((!isEscaped && currentChar == '\"')) { currentPart = CommandParserPart.DoubleQuotedParameter; startPosition = endPosition; } else if ((!isEscaped && currentChar == '\'')) { currentPart = CommandParserPart.QuotedParameter; startPosition = endPosition; } else if ((!isEscaped && currentChar == ' ') || endPosition >= inputLength) { int length = (currentChar == ' ' ? endPosition - 1 : endPosition) - startPosition; string temp = input.Substring(startPosition, length); if (temp == "") startPosition = endPosition; else { currentPart = CommandParserPart.None; argList.Add(temp); startPosition = endPosition; } } break; case CommandParserPart.QuotedParameter: if ((!isEscaped && currentChar == '\'')) { string temp = input.Substring(startPosition, endPosition - startPosition - 1); currentPart = CommandParserPart.None; argList.Add(temp); startPosition = endPosition; } else if (endPosition >= inputLength) return CommandErrorType.InvalidInput; break; case CommandParserPart.DoubleQuotedParameter: if ((!isEscaped && currentChar == '\"')) { string temp = input.Substring(startPosition, endPosition - startPosition - 1); currentPart = CommandParserPart.None; argList.Add(temp); startPosition = endPosition; } else if (endPosition >= inputLength) return CommandErrorType.InvalidInput; break; } } //Too few args for (int i = argList.Count; i < expectedArgs.Length; i++) { var param = expectedArgs[i]; switch (param.Type) { case ParameterType.Required: return CommandErrorType.BadArgCount; case ParameterType.Optional: break; // Optionals shouldn't be added when they're not there! case ParameterType.Unparsed: argList.Add(""); break; } } /*if (argList.Count > expectedArgs.Length) { if (expectedArgs.Length == 0 || expectedArgs[expectedArgs.Length - 1].Type != ParameterType.Multiple) return CommandErrorType.BadArgCount; }*/ args = argList.ToArray(); return null; }
internal void AddCommand(Command command) { _allCommands.Add(command); //Get category CommandMap category; string categoryName = command.Category ?? ""; if (!_categories.TryGetValue(categoryName, out category)) { category = new CommandMap(null, "", ""); _categories.Add(categoryName, category); } //Add main command category.AddCommand(command.Text, command); _map.AddCommand(command.Text, command); //Add aliases foreach (var alias in command.Aliases) { category.AddCommand(alias, command); _map.AddCommand(alias, command); } }
private void ShowCommandHelpInternal(Command command, User user, Channel channel, StringBuilder output) { output.Append('`'); output.Append(command.Text); foreach (var param in command.Parameters) { switch (param.Type) { case ParameterType.Required: output.Append($" <{param.Name}>"); break; case ParameterType.Optional: output.Append($" [{param.Name}]"); break; case ParameterType.Multiple: output.Append(" [...]"); break; case ParameterType.Unparsed: output.Append($" [{param.Name}]"); // " [--]" break; } } output.Append('`'); output.AppendLine($": {command.Description ?? "No description set for this command."}"); if (command.Aliases.Any()) output.AppendLine($"**Aliases:** `" + string.Join("`, `", command.Aliases) + '`'); if (command.NsfwFlag || command.MusicFlag) { string flags ="**Flags:** "; if (command.MusicFlag) flags += "Music "; if (command.NsfwFlag) flags += "NSFW "; flags.TrimEnd(' '); output.AppendLine(flags); } }
public Task ShowCommandHelp(Command command, User user, Channel channel, Channel replyChannel = null) { StringBuilder output = new StringBuilder(); string error; if (!command.CanRun(user, channel, out error)) output.AppendLine(error ?? DefaultPermissionError); else ShowCommandHelpInternal(command, user, channel, output); return _client.SendMessage(replyChannel ?? channel, output.ToString()); }
public CommandEventArgs(Message message, Command command, string[] args) { Message = message; Command = command; _args = args; }
public void AddCommand(string text, Command command, bool isAlias) { AddCommand(0, text.Split(' '), command, isAlias); }
private void AddCommand(int index, string[] parts, Command command, bool isAlias) { if (!command.IsHidden) _isVisible = true; if (index != parts.Length) { CommandMap nextGroup; string name = parts[index].ToLowerInvariant(); string fullName = string.Join(" ", parts, 0, index + 1); if (!_items.TryGetValue(name, out nextGroup)) { nextGroup = new CommandMap(this, name, fullName); _items.Add(name, nextGroup); _hasSubGroups = true; } nextGroup.AddCommand(index + 1, parts, command, isAlias); } else { _commands.Add(command); if (!isAlias) _hasNonAliases = true; } }
public void AddCommand(string text, Command command) { AddCommand(0, text.Split(' '), command); }
private void AddCommand(int index, string[] parts, Command command) { if (!command.IsHidden && _isHidden) _isHidden = false; if (index != parts.Length) { CommandMap nextGroup; string name = parts[index].ToLowerInvariant(); string fullName = string.Join(" ", parts, 0, index + 1); if (!_items.TryGetValue(name, out nextGroup)) { nextGroup = new CommandMap(this, name, fullName); _items.Add(name, nextGroup); } nextGroup.AddCommand(index + 1, parts, command); } else _commands.Add(command); }