Exemplo n.º 1
0
        private async Task <CommandRegistrationFindResult> FindCommandRegistrationAsync(SocketUserMessage userMessage)
        {
            var prefix = _defaultCommandPrefix;

            if (userMessage.Channel is ITextChannel guildChannel)
            {
                var guildConfig = await _guildConfigProvider.GetConfigAsync(guildChannel.GuildId);

                if (!string.IsNullOrEmpty(guildConfig?.CustomCommandPrefix))
                {
                    prefix = guildConfig.CustomCommandPrefix;
                }
            }

            // Check prefix
            if (!userMessage.Content.StartsWith(prefix))
            {
                return(null);
            }

            // Check if the message contains a command invoker
            var invoker = _commandParser.ParseInvoker(userMessage.Content, prefix);

            if (string.IsNullOrEmpty(invoker))
            {
                return(null);
            }

            lock (_commandsMappingLock)
            {
                // Try to find command registrations for this invoker
                List <CommandInfo> commandRegistrations;
                if (!_commandsMapping.TryGetValue(invoker.ToLowerInvariant(), out commandRegistrations))
                {
                    return(null);
                }

                var match = _commandParser.Match(userMessage.Content, prefix, commandRegistrations);
                if (match == null)
                {
                    return(null);
                }

                var(registration, usage) = match.Value;
                return(new CommandRegistrationFindResult(prefix, registration, usage));
            }
        }