Esempio n. 1
0
        /// <summary>
        /// Parses commands that accept the provided <paramref name="input"/> string as the method name and arguments.
        /// </summary>
        /// <returns>A sorted parse result list based on priority and successful parameters.</returns>
        public async Task <IEnumerable <ParseResult> > ParseMethodsAsync(ICommandContextEx context, string input, bool validateInput = true)
        {
            var list = new List <ParseResult>();

            foreach (var module in (context.Guild == null ? CommandHandler.Modules[0] : CommandHandler.Modules[context.Guild.Id]))
            {
                var parseResults = ParseMethodsInternal(module, input, ParsingState.BASE);
                foreach (var parseResult in parseResults)
                {
                    if (parseResult != null && parseResult.Method != null && parseResult.InputMessage != null)
                    {
                        var    score        = new Tuple <int?, List <object[]> >(null, null);
                        string errorMessage = null;
                        try
                        {
                            var commandName = parseResult.InputMessage;
                            //if (parseResult.Method.MethodInfo?.Name == "_")
                            if (parseResult.Method.Accessibility.Has(CommandAccessLevel.Global))
                            {
                                var firstCommand = commandName.Split(' ').FirstOrDefault();
                                if (firstCommand != null && firstCommand.Length > 0)
                                {
                                    var firstAlias = parseResult.Method.Aliases.Where(a => a != "_").FirstOrDefault(e => e.Equals(firstCommand, StringComparison.CurrentCultureIgnoreCase));
                                    if (firstAlias == null)
                                    {
                                        // add underscore
                                        commandName = "_ " + commandName;
                                    }
                                }
                            }

                            score = await CommandScorer.GetMethodScoreAndParametersAsync(
                                context,
                                parseResult.Method.MethodInfo,
                                commandName
                                ).ConfigureAwait(false);
                        }
                        catch (Exception ex)
                        {
                            errorMessage = ex.Message;
                        }

                        // Validate command - part 1: if guild == null, verify that the command accepts non-guild sources
                        if (validateInput)
                        {
                            if (context.Guild == null && !(parseResult.Method.Source.Has(CommandSourceLevel.DM) || parseResult.Method.Source.Has(CommandSourceLevel.Group)))
                            {
                                continue;
                            }

                            // Validate command - part 2, check the command settings
                            try
                            {
                                if ((await(parseResult.Method.MethodInfo.GetCustomAttribute(typeof(DiscordCommandAttribute)) as DiscordCommandAttribute).VerifyAsync(context)).HasError)
                                {
                                    throw new Exception();
                                }
                            }
                            catch { continue; }
                        }

                        list.Add(new ParseResult()
                        {
                            InputMessage = parseResult.InputMessage,
                            Method       = parseResult.Method,
                            Module       = parseResult.Module,
                            Parameters   = score.Item2,
                            Score        = score.Item1 ?? -1,
                            Priority     = parseResult.Priority,
                            ErrorMessage = errorMessage
                        });
                    }
                }
            }

            return(GetSortedResults(list));
        }
Esempio n. 2
0
 public CommandMethodParser(CommandHandler commandHandler)
 {
     CommandHandler = commandHandler;
     CommandScorer  = new CommandScorer(CommandHandler.CommandConverter);
 }