Exemple #1
0
        public async Task Invoke(string[] args, CommandMessage message)
        {
            if (this.Permission > CommandsService.GetPermissions(message.Author))
            {
                throw new UserException("I'm sorry, you don't have permission to do that.");
            }

            object?owner;

            if (!this.Owner.TryGetTarget(out owner) || owner == null)
            {
                throw new Exception("Attempt to invoke command on null owner.");
            }

            List <object> parameters = new List <object>();

            ParameterInfo[] allParamInfos = this.Method.GetParameters();

            int argCount       = 0;
            int neededArgCount = allParamInfos.Length;

            for (int i = 0; i < allParamInfos.Length; i++)
            {
                ParameterInfo paramInfo = allParamInfos[i];

                if (paramInfo.ParameterType == typeof(CommandMessage))
                {
                    parameters.Add(message);
                    neededArgCount--;
                    continue;
                }
                else if (paramInfo.ParameterType == typeof(string[]))
                {
                    parameters.Add(args);
                    neededArgCount--;
                }
                else if (paramInfo.ParameterType == typeof(Attachment) && message.Message.Attachments.Count == 1)
                {
                    parameters.Add(message.Message.Attachments.FirstOrDefault());
                    neededArgCount--;
                }
                else
                {
                    if (argCount >= args.Length)
                    {
                        parameters.Add("Empty");
                        continue;
                    }

                    string arg;

                    if (neededArgCount == 1 && paramInfo.ParameterType == typeof(string))
                    {
                        arg       = string.Join(" ", args);
                        argCount += args.Length;
                    }
                    else
                    {
                        arg = args[argCount];
                        argCount++;
                    }

                    object param;

                    try
                    {
                        param = await this.Convert(message, arg, paramInfo.ParameterType, this.RequiresQuotes);
                    }
                    catch (UserException ex)
                    {
                        throw ex;
                    }
                    catch (Exception)
                    {
                        string hint = string.Empty;

                        if (paramInfo.ParameterType == typeof(string))
                        {
                            hint = "\n(Strings must have \"quotes\" around them)";
                        }

                        throw new ParameterException("I didn't understand the parameter: " + arg + ".\nWas that was supposed to be a " + HelpService.GetTypeName(paramInfo.ParameterType) + " for " + HelpService.GetParam(paramInfo, this.RequiresQuotes) + "?" + hint);
                    }

                    parameters.Add(param);
                }
            }

            if (parameters.Count != allParamInfos.Length || argCount != args.Length || neededArgCount > args.Length)
            {
                throw new ParameterException("Incorrect number of parameters. I was expecting " + neededArgCount + ", but you sent me " + args.Length + "!");
            }

            try
            {
                await this.Invoke(message, owner, parameters.ToArray());
            }
            catch (UserException ex)
            {
                await message.Channel.SendMessageAsync(ex.Message);
            }
        }