예제 #1
0
        /// <summary>
        /// Given a message, find corresponding command and run it.
        /// </summary>
        /// <param name="msg">The message that ran the command.</param>
        /// <param name="input">The input from the user.</param>
        public bool ExecuteUserInput(Matbot.Client.Message msg, ParsedInput p)
        {
            if (p == null)
            {
                return(false);
            }

            if (msg.User.BotRank == Client.UserRank.Gali)
            {
                bool c = true;
                if (p.Name != null)
                {
                    if (p.Name.Equals("register"))
                    {
                        c = false;
                    }
                }
                if (c)
                {
                    msg.Reply(p.RawInput);
                    return(true);
                }
            }

            if (p.IsCommand)
            {
                if (!Commands.ContainsKey(p.Name.ToLower()))
                {
                    return(false);
                }
                Command c = Commands[p.Name.ToLower()];
                if (c != null)
                {
                    c.ReformatInput(p);

                    List <object> parameters;
                    try
                    {
                        CmdVariation v = c.FindCommandVariationByParsedInput(p, out parameters);

                        c.ExecuteVariation(v, msg, parameters.ToArray());
                    }
                    catch (CorrectVariationNotFoundException ex)
                    {
                        msg.Reply(ex.Message);
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
예제 #2
0
파일: Command.cs 프로젝트: MatanRad/Matbot
 /// <summary>
 /// When given a user's input, reformat it before finding variations.
 /// </summary>
 /// <param name="input">The input to be reformatted.</param>
 public virtual void ReformatInput(ParsedInput input)
 {
 }
예제 #3
0
파일: Command.cs 프로젝트: MatanRad/Matbot
        /// <summary>
        /// Try to find a single CmdVariation from a user's ParsedInput
        /// </summary>
        public CmdVariation FindCommandVariationByParsedInput(ParsedInput input, out List <object> converted)
        {
            CmdVariation[] vars            = Variations;
            var            convertedPerVar = new List <List <object> >();

            converted = new List <object>();
            List <CmdVariation> conflicts = new List <CmdVariation>();

            string[] param = input.Parameters;

            foreach (CmdVariation v in vars)
            {
                if (v == null)
                {
                    continue;
                }
                if (v.Attributes.Count != param.Length)
                {
                    continue;
                }
                bool failed       = false;
                var  convertedVar = new List <object>();
                convertedPerVar.Add(convertedVar);

                for (int i = 0; i < v.Attributes.Count; i++)
                {
                    CmdAttribute a = v.Attributes[i];
                    try
                    {
                        object t = ClassConverter.ConvertToObj(param[i], a.AType);
                        convertedVar.Add(t);
                    }
                    catch (Exception e)
                    {
                        if (e is InvalidCastException || e is FormatException)
                        {
                            System.Diagnostics.Debug.WriteLine(e.GetType().Name);
                            failed = true;
                            convertedPerVar.RemoveAt(convertedPerVar.Count - 1);
                            break;
                        }
                        else
                        {
                            throw e;
                        }
                    }
                }

                if (!failed)
                {
                    conflicts.Add(v);
                }
            }

            if (conflicts.Count == 0)
            {
                throw new CorrectVariationNotFoundException(input);
            }
            if (conflicts.Count > 1)
            {
                bool throwError = true;
                if (this.paramTypePriorities != null)
                {
                    CmdVariation resolve = ResolveConflictViaPriority(conflicts);
                    if (resolve != null)
                    {
                        throwError = false;
                        converted  = convertedPerVar[conflicts.IndexOf(resolve)];
                        return(resolve);
                    }
                }

                if (throwError)
                {
                    throw new ConflictingVariationsException(input.Name, conflicts.ToArray(), input.RawInput, null);
                }
            }

            // Case where only 1 variation succeeds
            converted = convertedPerVar[0];
            return(conflicts[0]);
        }
예제 #4
0
        /// <summary>
        /// Given a message, find corresponding command and run it.
        /// </summary>
        /// <param name="msg">The message that ran the command.</param>
        /// <param name="input">The input from the user.</param>
        public bool ExecuteUserInput(Matbot.Client.Message msg, string input)
        {
            ParsedInput p = new ParsedInput(input);

            return(ExecuteUserInput(msg, p));
        }