/// <summary> /// Parses a read line from console. /// </summary> /// <param name="line">The line to be parsed.</param> /// <param name="client">The invoker client if any.</param> /// <param name="responseMedium">The response medium to command</param> public static bool TryParse(string line, MooNetClient client=null, ResponseMediums responseMedium = ResponseMediums.Console) { var output = string.Empty; line = line.Trim().ToLower(); if (line == String.Empty) // just return false, if message is empty. return false; if (line[0] != Config.Instance.CommandPrefix) // if line does not start with command-prefix { output = "Unknown command: " + line; if(client==null && responseMedium== ResponseMediums.Console) Logger.Info(output); // only output 'unknown command' message to console. return false; } line = line.Substring(1); // advance to actual command. var command = line.Split(' ')[0]; // get command var parameters = String.Empty; if (line.Contains(' ')) parameters = line.Substring(line.IndexOf(' ') + 1).ToLower().Trim(); // get parameters if any. foreach(var pair in Commands) { if (pair.Key.Command != command) continue; InvokeCommand(pair.Key, pair.Value, parameters, client, responseMedium); return true; } // if execution reaches here, it means command was not found. output = "Unknown command: " + line; // if invoked from console if (client == null) { Logger.Info(output); return true; } // if invoked by a client if (responseMedium == ResponseMediums.Channel) client.SendMessage(output); else if (responseMedium == ResponseMediums.Whisper) client.SendWhisper(output); return true; }
private static void InvokeCommand(CommandAttribute commandAttribute, Command command, string parameters, MooNetClient client = null, ResponseMediums responseMedium = ResponseMediums.Console) { var output = string.Empty; if (client != null && commandAttribute.ConsoleOnly) output = "You don't have enough privileges to run this command."; else output = command.Invoke(parameters, client); // invoke the command // if invoked from console if (client == null) { Logger.Info(output); return; } // if invoked by a client if (responseMedium == ResponseMediums.Channel) client.SendMessage(output); else if (responseMedium == ResponseMediums.Whisper) client.SendWhisper(output); }