public static bool ParseCommand(string input, CommandMap map, out IEnumerable <Command> commands, out int endPos) { int startPosition = 0; int endPosition = 0; int inputLength = input.Length; bool isEscaped = false; commands = null; endPos = 0; if (input == "") { return(false); } while (endPosition < inputLength) { char currentChar = input[endPosition++]; if (isEscaped) { isEscaped = false; } else if (currentChar == '\\') { isEscaped = true; } bool isWhitespace = IsWhiteSpace(currentChar); if ((!isEscaped && isWhitespace) || endPosition >= inputLength) { int length = (isWhitespace ? endPosition - 1 : endPosition) - startPosition; string temp = input.Substring(startPosition, length); if (temp == "") { startPosition = endPosition; } else { var newMap = map.GetItem(temp); if (newMap != null) { map = newMap; endPos = endPosition; } else { break; } startPosition = endPosition; } } } commands = map.GetCommands(); //Work our way backwards to find a command that matches our input return(commands != null); }
public static bool ParseCommand(string input, CommandMap map, out IEnumerable<Command> commands, out int endPos) { int startPosition = 0; int endPosition = 0; int inputLength = input.Length; bool isEscaped = false; commands = null; endPos = 0; if (input == "") return false; while (endPosition < inputLength) { char currentChar = input[endPosition++]; if (isEscaped) isEscaped = false; else if (currentChar == '\\') isEscaped = true; bool isWhitespace = IsWhiteSpace(currentChar); if ((!isEscaped && isWhitespace) || endPosition >= inputLength) { int length = (isWhitespace ? endPosition - 1 : endPosition) - startPosition; string temp = input.Substring(startPosition, length); if (temp == "") startPosition = endPosition; else { var newMap = map.GetItem(temp); if (newMap != null) { map = newMap; endPos = endPosition; } else break; startPosition = endPosition; } } } commands = map.GetCommands(); //Work our way backwards to find a command that matches our input return commands != null; }
void IService.Install(DiscordClient client) { Client = client; if (Config.HelpMode != HelpMode.Disabled) { CreateCommand("help") .Parameter("command", ParameterType.Multiple) .Hide() .Description("Returns information about commands.") .Do(async e => { Channel replyChannel = Config.HelpMode == HelpMode.Public ? e.Channel : await e.User.CreatePMChannel().ConfigureAwait(false); if (e.Args.Length > 0) //Show command help { var map = _map.GetItem(string.Join(" ", e.Args)); if (map != null) { await ShowCommandHelp(map, e.User, e.Channel, replyChannel).ConfigureAwait(false); } else { await replyChannel.SendMessage("Unable to display help: Unknown command.").ConfigureAwait(false); } } else //Show general help { await ShowGeneralHelp(e.User, e.Channel, replyChannel).ConfigureAwait(false); } }); } client.MessageReceived += async(s, e) => { if (_allCommands.Count == 0) { return; } if (Config.IsSelfBot) { if (e.Message.User == null || e.Message.User.Id != Client.CurrentUser.Id) { return; // Will only listen to Self } } else if (e.Message.User == null || e.Message.User.Id == Client.CurrentUser.Id) { return; // Normal expected behavior for bots } string msg = e.Message.RawText; if (msg.Length == 0) { return; } string cmdMsg = null; //Check for command char if (Config.PrefixChar.HasValue) { if (msg[0] == Config.PrefixChar.Value) { cmdMsg = msg.Substring(1); } } //Check for mention if (cmdMsg == null && Config.AllowMentionPrefix) { string mention = client.CurrentUser.Mention; if (msg.StartsWith(mention) && msg.Length > mention.Length) { cmdMsg = msg.Substring(mention.Length + 1); } else { mention = $"@{client.CurrentUser.Name}"; if (msg.StartsWith(mention) && msg.Length > mention.Length) { cmdMsg = msg.Substring(mention.Length + 1); } } string mention2 = client.CurrentUser.NicknameMention; if (mention2 != null) { if (msg.StartsWith(mention2) && msg.Length > mention2.Length) { cmdMsg = msg.Substring(mention2.Length + 1); } else { mention2 = $"@{client.CurrentUser.Name}"; if (msg.StartsWith(mention2) && msg.Length > mention2.Length) { cmdMsg = msg.Substring(mention2.Length + 1); } } } } //Check using custom activator if (cmdMsg == null && Config.CustomPrefixHandler != null) { int index = Config.CustomPrefixHandler(e.Message); if (index >= 0) { cmdMsg = msg.Substring(index); } } if (cmdMsg == null) { return; } //Parse command IEnumerable <Command> commands; int argPos; CommandParser.ParseCommand(cmdMsg, _map, out commands, out argPos); if (commands == null) { CommandEventArgs errorArgs = new CommandEventArgs(e.Message, null, null); OnCommandError(CommandErrorType.UnknownCommand, errorArgs); return; } else { foreach (var command in commands) { //Parse arguments string[] args; var error = CommandParser.ParseArgs(cmdMsg, argPos, command, out args); if (error != null) { if (error == CommandErrorType.BadArgCount) { continue; } else { var errorArgs = new CommandEventArgs(e.Message, command, null); OnCommandError(error.Value, errorArgs); return; } } var eventArgs = new CommandEventArgs(e.Message, command, args); // Check permissions string errorText; if (!command.CanRun(eventArgs.User, eventArgs.Channel, out errorText)) { OnCommandError(CommandErrorType.BadPermissions, eventArgs, errorText != null ? new Exception(errorText) : null); return; } // Run the command try { OnCommand(eventArgs); await command.Run(eventArgs).ConfigureAwait(false); } catch (Exception ex) { OnCommandError(CommandErrorType.Exception, eventArgs, ex); DiscordBotLog.AppendLog(DiscordBotLog.BuildCommandExceptionMessage(ex, eventArgs)); } return; } var errorArgs2 = new CommandEventArgs(e.Message, null, null); OnCommandError(CommandErrorType.BadArgCount, errorArgs2); } }; }
void IService.Install(DiscordClient client) { Client = client; Config.Lock(); if (Config.HelpMode != HelpMode.Disable) { CreateCommand("help") .Parameter("command", ParameterType.Multiple) .Hide() .Description("Returns information about commands.") .Do(async e => { Channel replyChannel = Config.HelpMode == HelpMode.Public ? e.Channel : await e.User.CreatePMChannel().ConfigureAwait(false); if (e.Args.Length > 0) //Show command help { var map = _map.GetItem(string.Join(" ", e.Args)); if (map != null) { await ShowCommandHelp(map, e.User, e.Channel, replyChannel).ConfigureAwait(false); } else { await replyChannel.SendMessage("Unable to display help: Unknown command.").ConfigureAwait(false); } } else //Show general help { await ShowGeneralHelp(e.User, e.Channel, replyChannel); } }); } client.MessageReceived += async(s, e) => { if (_allCommands.Count == 0) { return; } if (e.Message.User == null || e.Message.User.Id == Client.CurrentUser.Id) { return; } string msg = e.Message.RawText; if (msg.Length == 0) { return; } //Check for command char if one is provided var chars = Config.CommandChars; if (chars.Length > 0) { if (!chars.Contains(msg[0])) { return; } msg = msg.Substring(1); } //Parse command IEnumerable <Command> commands; int argPos; CommandParser.ParseCommand(msg, _map, out commands, out argPos); if (commands == null) { CommandEventArgs errorArgs = new CommandEventArgs(e.Message, null, null); OnCommandError(CommandErrorType.UnknownCommand, errorArgs); return; } else { foreach (var command in commands) { //Parse arguments string[] args; var error = CommandParser.ParseArgs(msg, argPos, command, out args); if (error != null) { if (error == CommandErrorType.BadArgCount) { continue; } else { var errorArgs = new CommandEventArgs(e.Message, command, null); OnCommandError(error.Value, errorArgs); return; } } var eventArgs = new CommandEventArgs(e.Message, command, args); // Check permissions string errorText; if (!command.CanRun(eventArgs.User, eventArgs.Channel, out errorText)) { OnCommandError(CommandErrorType.BadPermissions, eventArgs, errorText != null ? new Exception(errorText) : null); return; } // Run the command try { OnCommand(eventArgs); await command.Run(eventArgs).ConfigureAwait(false); } catch (Exception ex) { OnCommandError(CommandErrorType.Exception, eventArgs, ex); } return; } var errorArgs2 = new CommandEventArgs(e.Message, null, null); OnCommandError(CommandErrorType.BadArgCount, errorArgs2); } }; }