@@ -20,16 +20,51 @@ public TextCommandHandler(IReadOnlyCollection<MethodInfo> controllerMethods, Upd
2020
2121 private MethodInfo ? GetMethodInfo ( IReadOnlyCollection < MethodInfo > controllerMethods , Update update )
2222 {
23- if ( update . Type != Telegram . Bot . Types . Enums . UpdateType . Message
24- || update . Message == null
25- || string . IsNullOrWhiteSpace ( update . Message . Text ) )
23+ if ( ! IsValidUpdate ( update ) )
2624 {
2725 return null ;
2826 }
29- var message = update . Message ;
30- string [ ] parts = message . Text . Split ( ' ' ) ;
31- List < string > arguments = new List < string > ( ) ;
32- string currentArgument = string . Empty ;
27+
28+ var parts = GetCommandParts ( update . Message ! . Text ! ) ;
29+ if ( parts . Length == 0 )
30+ {
31+ return null ;
32+ }
33+
34+ var command = parts [ 0 ] ;
35+ if ( ! IsCommandValid ( command ) )
36+ {
37+ return null ;
38+ }
39+
40+ AddArguments ( parts ) ;
41+
42+ var methods = FindMatchingMethods ( controllerMethods , command ) ;
43+ if ( methods . Count == 1 )
44+ {
45+ return methods [ 0 ] ;
46+ }
47+ else if ( methods . Count > 1 )
48+ {
49+ throw new AmbiguousMatchException ( "Multiple methods found with the same command and arguments: " + command ) ;
50+ }
51+
52+ return null ;
53+ }
54+
55+ private bool IsValidUpdate ( Update update )
56+ {
57+ return update . Type == Telegram . Bot . Types . Enums . UpdateType . Message
58+ && update . Message != null
59+ && ! string . IsNullOrWhiteSpace ( update . Message . Text ) ;
60+ }
61+
62+ private string [ ] GetCommandParts ( string text )
63+ {
64+ var parts = text . Split ( ' ' ) ;
65+ var arguments = new List < string > ( ) ;
66+ var currentArgument = string . Empty ;
67+
3368 foreach ( var part in parts )
3469 {
3570 if ( part . StartsWith ( '"' ) && part . EndsWith ( '"' ) )
@@ -55,28 +90,34 @@ public TextCommandHandler(IReadOnlyCollection<MethodInfo> controllerMethods, Upd
5590 arguments . Add ( part ) ;
5691 }
5792 }
58- parts = arguments
93+
94+ return arguments
5995 . Select ( p => p . Trim ( '"' ) )
6096 . Where ( p => ! string . IsNullOrWhiteSpace ( p ) )
6197 . ToArray ( ) ;
62- if ( parts . Length == 0 )
63- {
64- return null ;
65- }
66- string command = parts [ 0 ] ;
67- if ( ! command . StartsWith ( '/' ) )
68- {
69- return null ;
70- }
98+ }
99+
100+ private bool IsCommandValid ( string command )
101+ {
102+ return command . StartsWith ( '/' ) ;
103+ }
104+
105+ private void AddArguments ( string [ ] parts )
106+ {
71107 if ( parts . Length > 1 )
72108 {
73109 _args . AddRange ( parts [ 1 ..] ) ;
74110 }
111+ }
112+
113+ private List < MethodInfo > FindMatchingMethods ( IReadOnlyCollection < MethodInfo > controllerMethods , string command )
114+ {
115+ var methods = new List < MethodInfo > ( ) ;
75116
76- List < MethodInfo > methods = new List < MethodInfo > ( ) ;
77117 foreach ( var method in controllerMethods )
78118 {
79119 var attributes = method . GetCustomAttributes ( typeof ( TextCommandAttribute ) , false ) ;
120+
80121 foreach ( var attribute in attributes )
81122 {
82123 if ( attribute is TextCommandAttribute botCommandAttribute )
@@ -85,8 +126,8 @@ public TextCommandHandler(IReadOnlyCollection<MethodInfo> controllerMethods, Upd
85126 {
86127 if ( _args . Count == method . GetParameters ( ) . Length )
87128 {
88- bool converted = ObjectHelpers . TryConvertParameters ( method , _args . ToArray ( ) ) ;
89- if ( converted )
129+ var args = _args . ToArray ( ) ;
130+ if ( ObjectHelpers . TryConvertParameters ( method , args ) )
90131 {
91132 methods . Add ( method ) ;
92133 }
@@ -95,21 +136,17 @@ public TextCommandHandler(IReadOnlyCollection<MethodInfo> controllerMethods, Upd
95136 }
96137 }
97138 }
139+
98140 if ( methods . Count == 1 )
99141 {
100142 var args = _args . ToArray ( ) ;
101143 ObjectHelpers . TryConvertParameters ( methods [ 0 ] , args ) ;
102144 _args . Clear ( ) ;
103145 _args . AddRange ( args ) ;
104- return methods [ 0 ] ;
105146 }
106- else if ( methods . Count > 1 )
107- {
108- throw new AmbiguousMatchException ( "Multiple methods found with the same command and arguments: " + command ) ;
109- }
110- return null ;
111- }
112147
148+ return methods ;
149+ }
113150
114151 public object [ ] ? GetArguments ( )
115152 {
0 commit comments