public override ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments, IEnumerable <CommandResultType> returnTypes) { switch (CommandRights) { case Admin: if (!info.IsAdmin.Value) { throw new CommandException("Command must be invoked by an admin!"); } break; case Public: if (info.TextMessage.Target != MessageTarget.Server && !info.IsAdmin.Value) { throw new CommandException("Command must be used in public mode!"); } break; case Private: if (info.TextMessage.Target != MessageTarget.Private && !info.IsAdmin.Value) { throw new CommandException("Command must be used in a private session!"); } break; } return(base.Execute(info, arguments, returnTypes)); }
public override ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments, IEnumerable <CommandResultType> returnTypes) { // Make arguments lazy, we only want to execute them once arguments = arguments.Select(c => new LazyCommand(c)); foreach (FunctionCommand f in Functions) { bool fits = false; try { // Find out if this overload works int i; f.FitArguments(info, arguments, returnTypes, out i); fits = true; } catch (CommandException) { // Do nothing, just move on to the next function } if (fits) { // Call this overload return(f.Execute(info, arguments, returnTypes)); } } throw new CommandException("No matching function could be found"); }
public override ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments, IEnumerable <CommandResultType> returnTypes) { string result; if (!arguments.Any()) { if (returnTypes.Contains(CommandResultType.Command)) { return(new CommandCommandResult(this)); } result = string.Empty; } else { var comResult = arguments.First().Execute(info, Enumerable.Empty <ICommand>(), new CommandResultType[] { CommandResultType.String }); result = ((StringCommandResult)comResult).Content; } var commandResults = XCommandSystem.FilterList(commands, result); if (commandResults.Skip(1).Any()) { throw new CommandException("Ambiguous command, possible names: " + string.Join(", ", commandResults.Select(g => g.Key)), CommandExceptionReason.AmbiguousCall); } var argSubList = arguments.Skip(1).ToArray(); return(commandResults.First().Value.Execute(info, argSubList, returnTypes)); }
public ICommandResult Execute(ExecutionInformation info, string command, IEnumerable <CommandResultType> returnTypes) { var ast = CommandParser.ParseCommandRequest(command); var cmd = AstToCommandResult(ast); return(cmd.Execute(info, Enumerable.Empty <ICommand>(), returnTypes)); }
public override ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments, IEnumerable <CommandResultType> returnTypes) { switch (CommandRights) { case Admin: if (!info.IsAdmin) { throw new CommandException("Command must be invoked by an admin!", CommandExceptionReason.MissingRights); } break; case Public: if (!info.ApiCall && info.TextMessage.Target != TextMessageTargetMode.Server && !info.IsAdmin) // TODO: Apicall special ?? { throw new CommandException("Command must be used in public mode!", CommandExceptionReason.MissingRights); } break; case Private: if (!info.ApiCall && info.TextMessage.Target != TextMessageTargetMode.Private && !info.IsAdmin) // TODO: Apicall special ?? { throw new CommandException("Command must be used in a private session!", CommandExceptionReason.MissingRights); } break; } return(base.Execute(info, arguments, returnTypes)); }
public static object Execute(ExecutionInformation info, string command, IReadOnlyList <Type> returnTypes) { var ast = CommandParser.ParseCommandRequest(command); var cmd = AstToCommandResult(ast); return(cmd.Execute(info, Array.Empty <ICommand>(), returnTypes)); }
public virtual ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes) { string result; if (arguments.Count == 0) { if (returnTypes.Contains(CommandResultType.Command)) { return(new CommandCommandResult(this)); } result = string.Empty; } else { var comResult = arguments[0].Execute(info, StaticList.Empty <ICommand>(), new[] { CommandResultType.String }); result = ((StringCommandResult)comResult).Content; } var commandResults = XCommandSystem.FilterList(commands, result).ToArray(); if (commandResults.Length > 1) { throw new CommandException("Ambiguous command, possible names: " + string.Join(", ", commandResults.Select(g => g.Key)), CommandExceptionReason.AmbiguousCall); } if (commandResults.Length == 0) { throw new CommandException("No matching command", CommandExceptionReason.AmbiguousCall); } var argSubList = arguments.TrySegment(1); return(commandResults[0].Value.Execute(info, argSubList, returnTypes)); }
public virtual ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes) { // Make arguments lazy, we only want to execute them once arguments = arguments.Select(c => new LazyCommand(c)).ToArray(); foreach (var f in Functions) { bool fits = false; try { // Find out if this overload works f.FitArguments(info, arguments, returnTypes, out var _); fits = true; } catch (CommandException) { // Do nothing, just move on to the next function } if (fits) { // Call this overload return(f.Execute(info, arguments, returnTypes)); } } throw new CommandException("No matching function could be found", CommandExceptionReason.FunctionNotFound); }
public static async Task <ICmdResult> Execute(ExecutionInformation info, string command) { var ast = CommandParser.ParseCommandRequest(command); var cmd = AstToCommandResult(ast); return(new ICmdResult(await cmd.Execute(info, Array.Empty <ICommand>()))); }
public virtual ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes) { var merged = new ICommand[internArguments.Count + arguments.Count]; internArguments.CopyTo(0, merged, 0); arguments.CopyTo(0, merged, internArguments.Count); return(internCommand.Execute(info, merged, returnTypes)); }
public override ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments, IEnumerable <CommandResultType> returnTypes) { if (!info.HasRights(requiredRights)) { throw new CommandException($"You cannot execute \"{InvokeName}\". You are missing the \"{RequiredRight}\" right.!", CommandExceptionReason.MissingRights); } return(base.Execute(info, arguments, returnTypes)); }
public override ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes) { if (!info.HasRights(requiredRights)) { throw new CommandException(string.Format(strings.error_missing_right, InvokeName, RequiredRight), CommandExceptionReason.MissingRights); } return(base.Execute(info, arguments, returnTypes)); }
public override ICommandResult Execute(ExecutionInformation info, IEnumerable<ICommand> arguments, IEnumerable<CommandResultType> returnTypes) { if (result == null) { result = innerCommand.Execute(info, arguments, returnTypes); return result; } // Check if we can return that type if (!returnTypes.Contains(result.ResultType)) throw new CommandException("The cached result can't be returned"); return result; }
public override ICommandResult Execute(ExecutionInformation info, IEnumerable<ICommand> arguments, IEnumerable<CommandResultType> returnTypes) { if (!arguments.Any()) return base.Execute(info, arguments, returnTypes); var result = arguments.First().Execute(info, Enumerable.Empty<ICommand>(), new CommandResultType[] { CommandResultType.Command, CommandResultType.String }); if (result.ResultType == CommandResultType.String) // Use cached result so we don't execute the first argument twice return base.Execute(info, new ICommand[] { new StringCommand(((StringCommandResult)result).Content) } .Concat(arguments.Skip(1)), returnTypes); return ((CommandCommandResult)result).Command.Execute(info, arguments.Skip(1), returnTypes); }
public virtual ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes) { if (result == null) { result = innerCommand.Execute(info, arguments, returnTypes); return(result); } // Check if we can return that type if (!returnTypes.Contains(result.ResultType)) { throw new CommandException("The cached result can't be returned", CommandExceptionReason.NoReturnMatch); } return(result); }
public override ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments, IEnumerable <CommandResultType> returnTypes) { if (result == null) { result = innerCommand.Execute(info, arguments, returnTypes); return(result); } // Check if we can return that type if (!returnTypes.Contains(result.ResultType)) { throw new CommandException("The cached result can't be returned"); } return(result); }
public string ExecuteCommand(ExecutionInformation info, string command) { var result = Execute(info, command); if (result.ResultType == CommandResultType.String) { return(result.ToString()); } if (result.ResultType == CommandResultType.Empty) { return(null); } throw new CommandException("Expected a string or nothing as result", CommandExceptionReason.NoReturnMatch); }
public override ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments, IEnumerable <CommandResultType> returnTypes) { if (!arguments.Any()) { return(base.Execute(info, arguments, returnTypes)); } var result = arguments.First().Execute(info, Enumerable.Empty <ICommand>(), new CommandResultType[] { CommandResultType.Command, CommandResultType.String }); if (result.ResultType == CommandResultType.String) { // Use cached result so we don't execute the first argument twice return(base.Execute(info, new ICommand[] { new StringCommand(((StringCommandResult)result).Content) } .Concat(arguments.Skip(1)), returnTypes)); } return(((CommandCommandResult)result).Command.Execute(info, arguments.Skip(1), returnTypes)); }
public override ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes) { if (arguments.Count == 0) { return(base.Execute(info, arguments, returnTypes)); } var result = arguments[0].Execute(info, StaticList.Empty <ICommand>(), new[] { CommandResultType.Command, CommandResultType.String }); if (result.ResultType == CommandResultType.String) { // Use cached result so we don't execute the first argument twice var passArgs = new ICommand[arguments.Count]; passArgs[0] = new StringCommand(((StringCommandResult)result).Content); arguments.CopyTo(1, passArgs, 1); return(base.Execute(info, passArgs, returnTypes)); } return(((CommandCommandResult)result).Command.Execute(info, arguments.TrySegment(1), returnTypes)); }
public override ICommandResult Execute(ExecutionInformation info, IEnumerable<ICommand> arguments, IEnumerable<CommandResultType> returnTypes) { string result; if (!arguments.Any()) { if (returnTypes.Contains(CommandResultType.Command)) return new CommandCommandResult(this); result = string.Empty; } else { var comResult = arguments.First().Execute(info, Enumerable.Empty<ICommand>(), new CommandResultType[] { CommandResultType.String }); result = ((StringCommandResult)comResult).Content; } var commandResults = XCommandSystem.FilterList(commands, result); if (commandResults.Skip(1).Any()) throw new CommandException("Ambiguous command, possible names: " + string.Join(", ", commandResults.Select(g => g.Key))); var argSubList = arguments.Skip(1).ToArray(); return commandResults.First().Value.Execute(info, argSubList, returnTypes); }
public static string ExecuteCommand(ExecutionInformation info, IReadOnlyList <ICommand> arguments) => CastResult(Execute(info, arguments, ReturnStringOrNothing));
public static string ExecuteCommand(ExecutionInformation info, string command) => CastResult(Execute(info, command, ReturnStringOrNothing));
public static object Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <Type> returnTypes) => info.GetModule <CommandManager>().RootGroup.Execute(info, arguments, returnTypes);
public override ICommandResult Execute(ExecutionInformation info, IEnumerable<ICommand> arguments, IEnumerable<CommandResultType> returnTypes) { return internCommand.Execute(info, internArguments.Concat(arguments), returnTypes); }
public ICommandResult Execute(ExecutionInformation info, string command) { return(Execute(info, command, new[] { CommandResultType.String, CommandResultType.Empty })); }
public virtual ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes) { // Make arguments lazy, we only want to execute them once arguments = arguments.Select(c => new LazyCommand(c)).ToArray(); object[] parameters = FitArguments(info, arguments, returnTypes, out int availableArguments); // Check if we were able to set enough arguments if (availableArguments < Math.Min(parameters.Length, RequiredParameters)) { if (returnTypes.Contains(CommandResultType.Command)) { return(arguments.Any() ? new CommandCommandResult(new AppliedCommand(this, arguments)) : new CommandCommandResult(this)); } throw new CommandException("Not enough arguments for function " + internCommand.Name, CommandExceptionReason.MissingParameter); } if (CommandReturn == typeof(ICommandResult)) { return((ICommandResult)ExecuteFunction(parameters)); } bool executed = false; object result = null; // Take first fitting command result foreach (var returnType in returnTypes) { switch (returnType) { case CommandResultType.Command: // Return a command if we can take more arguments if (CommandParameter.Any(p => p == typeof(string[])) || availableArguments < NormalParameters) { return(new CommandCommandResult(new AppliedCommand(this, arguments))); } break; case CommandResultType.Empty: if (!executed) { ExecuteFunction(parameters); } return(new EmptyCommandResult()); case CommandResultType.String: if (!executed) { result = ExecuteFunction(parameters); executed = true; } if (!string.IsNullOrEmpty(result?.ToString())) { return(new StringCommandResult(result.ToString())); } break; case CommandResultType.Json: if (!executed) { result = ExecuteFunction(parameters); executed = true; } if (result is JsonObject jsonResult) { return(new JsonCommandResult(jsonResult)); } break; } } // Try to return an empty string if (returnTypes.Contains(CommandResultType.String) && executed) { return(new StringCommandResult("")); } throw new CommandException("Couldn't find a proper command result for function " + internCommand.Name, CommandExceptionReason.NoReturnMatch); }
/// <summary>Execute this command.</summary> /// <param name="info">All global informations for this execution.</param> /// <param name="arguments"> /// The arguments for this command. /// They are evaluated lazy which means they will only be evaluated if needed. /// </param> /// <param name="returnTypes"> /// The possible return types that should be returned by this execution. /// They are ordered by priority so, if possible, the first return type should be picked, then the second and so on. /// </param> public abstract ICommandResult Execute(ExecutionInformation info, IEnumerable<ICommand> arguments, IEnumerable<CommandResultType> returnTypes);
public ICommandResult Execute(ExecutionInformation info, string command) { return(Execute(info, command, ReturnStringOrNothing)); }
/// <summary> /// Try to fit the given arguments to the underlying function. /// This function will throw an exception if the parameters can't be applied. /// The parameters that are extracted from the arguments will be returned if they can be applied successfully. /// </summary> /// <param name="info">The ExecutionInformation.</param> /// <param name="arguments">The arguments that are applied to this function.</param> /// <param name="returnTypes">The possible return types.</param> /// <param name="availableArguments">How many arguments could be set.</param> public object[] FitArguments(ExecutionInformation info, IEnumerable<ICommand> arguments, IEnumerable<CommandResultType> returnTypes, out int availableArguments) { object[] parameters = new object[CommandParameter.Length]; var argList = new Lazy<List<ICommand>>(() => arguments.ToList()); // availableArguments: Iterate through arguments // p: Iterate through parameters availableArguments = 0; for (int p = 0; p < parameters.Length; p++) { var arg = CommandParameter[p]; if (arg == typeof(ExecutionInformation)) parameters[p] = info; else if (arg == typeof(IEnumerable<ICommand>)) parameters[p] = arguments; else if (arg == typeof(IEnumerable<CommandResultType>)) parameters[p] = returnTypes; // Only add arguments if we still have some else if (availableArguments < argList.Value.Count) { if (arg.IsArray) // array { var typeArr = arg.GetElementType(); var args = Array.CreateInstance(typeArr, argList.Value.Count - availableArguments); try { for (int i = 0; i < args.Length; i++, availableArguments++) { var argResult = ((StringCommandResult)argList.Value[availableArguments].Execute(info, Enumerable.Empty<ICommand>(), new[] { CommandResultType.String })).Content; var convResult = ConvertParam(argResult, typeArr); args.SetValue(convResult, i); } } catch (FormatException ex) { throw new CommandException("Could not convert to " + arg.Name, ex); } catch (OverflowException ex) { throw new CommandException("The number is too big.", ex); } parameters[p] = args; } else // primitive value { var argResult = ((StringCommandResult)argList.Value[availableArguments].Execute(info, Enumerable.Empty<ICommand>(), new[] { CommandResultType.String })).Content; try { parameters[p] = ConvertParam(argResult, arg); } catch (FormatException ex) { throw new CommandException("Could not convert to " + UnwrapType(arg).Name, ex); } catch (OverflowException ex) { throw new CommandException("The number is too big.", ex); } availableArguments++; } } else parameters[p] = GetDefault(arg); } // Check if we were able to set enough arguments if (availableArguments < Math.Min(parameters.Length, RequiredParameters) && !returnTypes.Contains(CommandResultType.Command)) throw new CommandException("Not enough arguments for function " + internCommand.Name); return parameters; }
public override ICommandResult Execute(ExecutionInformation info, IEnumerable<ICommand> arguments, IEnumerable<CommandResultType> returnTypes) { // Make arguments lazy, we only want to execute them once arguments = arguments.Select(c => new LazyCommand(c)); int availableArguments; object[] parameters = FitArguments(info, arguments, returnTypes, out availableArguments); // Check if we were able to set enough arguments if (availableArguments < Math.Min(parameters.Length, RequiredParameters)) { if (returnTypes.Contains(CommandResultType.Command)) { if (!arguments.Any()) return new CommandCommandResult(this); return new CommandCommandResult(new AppliedCommand(this, arguments)); } throw new CommandException("Not enough arguments for function " + internCommand.Name); } if (CommandReturn == typeof(ICommandResult)) return (ICommandResult)ExecuteFunction(parameters); bool executed = false; object result = null; // Take first fitting command result foreach (var returnType in returnTypes) { switch (returnType) { case CommandResultType.Command: // Return a command if we can take more arguments if (CommandParameter.Any(p => p == typeof(string[])) || availableArguments < NormalParameters) return new CommandCommandResult(new AppliedCommand(this, arguments)); break; case CommandResultType.Empty: if (!executed) ExecuteFunction(parameters); return new EmptyCommandResult(); case CommandResultType.String: if (!executed) { result = ExecuteFunction(parameters); executed = true; } if (result != null && !string.IsNullOrEmpty(result.ToString())) return new StringCommandResult(result.ToString()); break; } } // Try to return an empty string if (returnTypes.Contains(CommandResultType.String) && executed) return new StringCommandResult(""); throw new CommandException("Couldn't find a proper command result for function " + internCommand.Name); }
public override ICommandResult Execute(ExecutionInformation info, IEnumerable<ICommand> arguments, IEnumerable<CommandResultType> returnTypes) { switch (CommandRights) { case Admin: if (!info.IsAdmin.Value) throw new CommandException("Command must be invoked by an admin!"); break; case Public: if (info.TextMessage.Target != MessageTarget.Server && !info.IsAdmin.Value) throw new CommandException("Command must be used in public mode!"); break; case Private: if (info.TextMessage.Target != MessageTarget.Private && !info.IsAdmin.Value) throw new CommandException("Command must be used in a private session!"); break; } return base.Execute(info, arguments, returnTypes); }
string CallCommand(string command) { var info = new ExecutionInformation(null, CreateTextMessage(), new Lazy<bool>(true)); return bot.CommandManager.CommandSystem.ExecuteCommand(info, command); }
public virtual ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes) { return(new StringCommandResult(content)); }
public override ICommandResult Execute(ExecutionInformation info, IEnumerable<ICommand> arguments, IEnumerable<CommandResultType> returnTypes) { return new StringCommandResult(content); }
public override ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments, IEnumerable <CommandResultType> returnTypes) { return(internCommand.Execute(info, internArguments.Concat(arguments), returnTypes)); }
public ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments) { return(Execute(info, arguments, ReturnStringOrNothing)); }
public ICommandResult Execute(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes) { return(RootCommand.Execute(info, arguments, returnTypes)); }
/// <summary> /// Try to fit the given arguments to the underlying function. /// This function will throw an exception if the parameters can't be applied. /// The parameters that are extracted from the arguments will be returned if they can be applied successfully. /// </summary> /// <param name="info">The ExecutionInformation.</param> /// <param name="arguments">The arguments that are applied to this function.</param> /// <param name="returnTypes">The possible return types.</param> /// <param name="availableArguments">How many arguments could be set.</param> public object[] FitArguments(ExecutionInformation info, IReadOnlyList <ICommand> arguments, IReadOnlyList <CommandResultType> returnTypes, out int availableArguments) { var parameters = new object[CommandParameter.Length]; // availableArguments: Iterate through arguments // p: Iterate through parameters availableArguments = 0; for (int p = 0; p < parameters.Length; p++) { var arg = CommandParameter[p]; if (arg == typeof(ExecutionInformation)) { parameters[p] = info; } else if (arg == typeof(IReadOnlyList <ICommand>)) { parameters[p] = arguments; } else if (arg == typeof(IReadOnlyList <CommandResultType>)) { parameters[p] = returnTypes; } // Only add arguments if we still have some else if (availableArguments < arguments.Count) { if (arg.IsArray) // array { var typeArr = arg.GetElementType(); var args = Array.CreateInstance(typeArr, arguments.Count - availableArguments); try { for (int i = 0; i < args.Length; i++, availableArguments++) { var argResult = ((StringCommandResult)arguments[availableArguments].Execute(info, StaticList.Empty <ICommand>(), new[] { CommandResultType.String })).Content; var convResult = ConvertParam(argResult, typeArr); args.SetValue(convResult, i); } } catch (FormatException ex) { throw new CommandException("Could not convert to " + arg.Name, ex, CommandExceptionReason.CommandError); } catch (OverflowException ex) { throw new CommandException("The number is too big.", ex, CommandExceptionReason.CommandError); } parameters[p] = args; } else // primitive value { var argResult = ((StringCommandResult)arguments[availableArguments].Execute(info, StaticList.Empty <ICommand>(), new[] { CommandResultType.String })).Content; try { parameters[p] = ConvertParam(argResult, arg); } catch (FormatException ex) { throw new CommandException("Could not convert to " + UnwrapType(arg).Name, ex, CommandExceptionReason.CommandError); } catch (OverflowException ex) { throw new CommandException("The number is too big.", ex, CommandExceptionReason.CommandError); } availableArguments++; } } else { parameters[p] = GetDefault(arg); } } // Check if we were able to set enough arguments if (availableArguments < Math.Min(parameters.Length, RequiredParameters) && !returnTypes.Contains(CommandResultType.Command)) { throw new CommandException("Not enough arguments for function " + internCommand.Name, CommandExceptionReason.MissingParameter); } return(parameters); }
public override ICommandResult Execute(ExecutionInformation info, IEnumerable<ICommand> arguments, IEnumerable<CommandResultType> returnTypes) { // Make arguments lazy, we only want to execute them once arguments = arguments.Select(c => new LazyCommand(c)); foreach (FunctionCommand f in Functions) { bool fits = false; try { // Find out if this overload works int i; f.FitArguments(info, arguments, returnTypes, out i); fits = true; } catch (CommandException) { // Do nothing, just move on to the next function } if (fits) { // Call this overload return f.Execute(info, arguments, returnTypes); } } throw new CommandException("No matching function could be found"); }
public ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments, IEnumerable <CommandResultType> returnTypes) { return(RootCommand.Execute(info, arguments, returnTypes)); }
public ICommandResult Execute(ExecutionInformation info, IEnumerable <ICommand> arguments) { return(Execute(info, arguments, new[] { CommandResultType.String, CommandResultType.Empty })); }