Exemplo n.º 1
0
        /// <summary>
        ///     Creates a command complete with its meta data.
        /// </summary>
        /// <param name="familiarName">
        ///     The name of the command the user inputs.
        /// </param>
        /// <param name="args">
        ///     The arguments of the command.
        /// </param>
        /// <param name="channel">
        ///     The channel, if applicable.
        /// </param>
        public static CommandDataModel CreateCommand(
            string familiarName, IList <string> args = null, string channel = null)
        {
            if (WarningList.Contains(familiarName))
            {
                throw new ArgumentException($"Command '{familiarName}' must be sent with text.");
            }

            if (!IsValidCommand(familiarName))
            {
                throw new ArgumentException($"Unknown command: {familiarName}.");
            }

            var model = GetCommandModelFromName(familiarName);

            // having an empty argument is the same as none for our purposes
            if (args != null && string.IsNullOrEmpty(args[0]))
            {
                args = null;
            }

            CommandOverride commandOverride;

            if (CommandOverrides.TryGetValue(familiarName, out commandOverride))
            {
                var overrideArg = commandOverride.ArgumentName;
                var position    = model.ArgumentNames.IndexOf(overrideArg);

                args = (args == null)
                    ? new List <string>()
                    : new List <string>(args);

                if (position != -1 && !(position > args.Count))
                {
                    args.Insert(position, commandOverride.ArgumentValue);
                }
                else
                {
                    args.Add(commandOverride.ArgumentValue);
                }
            }

            var toReturn = new CommandDataModel(model, args, channel);

            // with no arguments we needn't do any validation
            if (args == null && model.ArgumentNames == null)
            {
                return(toReturn);
            }

            var argsCount      = args?.Count ?? 0;
            var modelArgsCount = model.ArgumentNames?.Count ?? 0;

            var difference = argsCount - modelArgsCount;

            // if we have parity in counts we don't have any issues
            if (difference == 0)
            {
                return(toReturn);
            }

            // error out if we have more arguments than we should
            if (difference > 0 || model.ArgumentNames == null)
            {
                throw new ArgumentException($"{familiarName} takes {modelArgsCount} arguments, not {argsCount}.");
            }

            var missingArgument = model.ArgumentNames[difference + model.ArgumentNames.Count];

            // error out if we have less arguments, but not if we are only missing the channel argument provided by the active channel
            if (difference == -1)
            {
                if (missingArgument == Channel && channel != null && channel != "Home")
                {
                    return(toReturn);
                }
                if (missingArgument == StatusMessage || missingArgument == "reason")
                {
                    return(toReturn);
                }
            }

            throw new ArgumentException($"{familiarName} is missing the '{missingArgument}' argument");
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Creates a command complete with its meta data.
        /// </summary>
        /// <param name="familiarName">
        ///     The name of the command the user inputs.
        /// </param>
        /// <param name="args">
        ///     The arguments of the command.
        /// </param>
        /// <param name="channel">
        ///     The channel, if applicable.
        /// </param>
        public static CommandDataModel CreateCommand(
            string familiarName, IList<string> args = null, string channel = null)
        {
            if (WarningList.Contains(familiarName))
                throw new ArgumentException($"Command '{familiarName}' must be sent with text.");

            if (!IsValidCommand(familiarName))
                throw new ArgumentException($"Unknown command: {familiarName}.");

            var model = GetCommandModelFromName(familiarName);

            // having an empty argument is the same as none for our purposes
            if (args != null && string.IsNullOrEmpty(args[0]))
                args = null;

            CommandOverride commandOverride;
            if (CommandOverrides.TryGetValue(familiarName, out commandOverride))
            {
                var overrideArg = commandOverride.ArgumentName;
                var position = model.ArgumentNames.IndexOf(overrideArg);

                args = (args == null)
                    ? new List<string>()
                    : new List<string>(args);

                if (position != -1 && !(position > args.Count))
                    args.Insert(position, commandOverride.ArgumentValue);
                else
                    args.Add(commandOverride.ArgumentValue);
            }

            var toReturn = new CommandDataModel(model, args, channel);

            // with no arguments we needn't do any validation
            if (args == null && model.ArgumentNames == null) return toReturn;

            var argsCount = args?.Count ?? 0;
            var modelArgsCount = model.ArgumentNames?.Count ?? 0;

            var difference = argsCount - modelArgsCount;

            // if we have parity in counts we don't have any issues
            if (difference == 0) return toReturn;

            // error out if we have more arguments than we should
            if (difference > 0 || model.ArgumentNames == null)
            {
                throw new ArgumentException($"{familiarName} takes {modelArgsCount} arguments, not {argsCount}.");
            }

            var missingArgument = model.ArgumentNames[difference + model.ArgumentNames.Count];

            // error out if we have less arguments, but not if we are only missing the channel argument provided by the active channel
            if (difference == -1)
            {
                if (missingArgument == Channel && channel != null && channel != "Home")
                    return toReturn;
                if (missingArgument == StatusMessage || missingArgument == "reason")
                    return toReturn;
            }

            throw new ArgumentException($"{familiarName} is missing the '{missingArgument}' argument");
        }