11using Amino . Interactions . Attributes ;
22using Amino . Interactions . Objects ;
3+ using Newtonsoft . Json . Linq ;
34using ReflectionsTest ;
45using System ;
56using System . Collections . Generic ;
67using System . Linq ;
78using System . Reflection ;
89using System . Runtime . CompilerServices ;
910using System . Text ;
11+ using System . Text . Json ;
1012using System . Threading . Tasks ;
13+ using System . Xml ;
1114
1215namespace Amino . Interactions
1316{
@@ -28,24 +31,62 @@ public enum LogLevels
2831 private Amino . Client AminoClient ;
2932 public int InteractionCooldown = 2000 ;
3033 public string InteractionPrefix = "/" ;
34+
3135 public bool IgnoreSelf = true ;
3236 public LogLevels LogLevel = LogLevels . None ;
37+ public bool AutoHandleInteractions { get ; } = false ;
38+
3339
40+ public event Action < Interaction > InteractionCreated ;
41+ public event Action < LogMessage > Log ;
3442
3543
3644 public InteractionsClient ( Amino . Client client )
3745 {
3846 this . AminoClient = client ;
39- this . InteractionQueue = new Queue < Objects . Interaction > ( ) ;
47+
4048 this . InteractionModules = new Dictionary < string , Objects . InteractionModule > ( ) ;
41- _ = Task . Run ( async ( ) => { HandleInteractionQueue ( ) ; } ) ;
4249
50+ if ( AutoHandleInteractions )
51+ {
52+ this . InteractionQueue = new Queue < Objects . Interaction > ( ) ;
53+ _ = Task . Run ( async ( ) => { HandleInteractionQueue ( ) ; } ) ;
54+ }
55+
56+ AminoClient . onMessage += HandleMessageSocket ;
57+
58+ }
59+
60+
61+ private void HandleMessageSocket ( Amino . Objects . Message message )
62+ {
63+ Console . WriteLine ( message . content ) ;
64+ if ( message . content . StartsWith ( InteractionPrefix ) )
65+ {
66+ if ( InteractionModules . ContainsKey ( message . content . Substring ( InteractionPrefix . Length ) . Split ( " " ) [ 0 ] ) )
67+ {
68+ InteractionModule module = InteractionModules [ message . content . Substring ( InteractionPrefix . Length ) . Split ( " " ) [ 0 ] ] ;
69+ Interaction context = new Interaction ( ) ;
70+ context . Message = message ;
71+ context . InteractionParameters . AddRange ( message . content . Split ( " " ) [ 1 ..] ) ;
72+ context . InteractionChatId = message . chatId ;
73+ context . AminoClient = this . AminoClient ;
74+ context . InteractionId = Guid . NewGuid ( ) . ToString ( ) ;
75+ context . InteractionName = module . ModuleCommandName ;
76+ context . InteractionTimestamp = DateTimeOffset . UtcNow . ToUnixTimeSeconds ( ) ;
77+ context . InteractionBaseModule = module ;
78+
79+ if ( this . InteractionCreated != null ) { this . InteractionCreated . Invoke ( context ) ; }
80+
81+ }
82+ }
4383 }
4484
4585
4686 public Task RegisterModule < T > ( ) where T : InteractionBase
4787 {
48- var moduleType = typeof ( T ) ;
88+ Type moduleType = typeof ( T ) ;
89+
4990 var methods = moduleType . GetMethods ( BindingFlags . Public | BindingFlags . Instance )
5091 . Where ( m => m . GetCustomAttribute < Command > ( ) != null ) ;
5192
@@ -62,17 +103,40 @@ public Task RegisterModule<T>() where T : InteractionBase
62103 if ( commandAttribute . CommandDescription != null ) { module . ModuleCommandDescription = commandAttribute . CommandDescription ; }
63104 if ( enabledInDmsAttribute != null ) { module . ModuleCommandEnabledInDms = enabledInDmsAttribute . IsEnabledInDms ; }
64105 if ( permissionGroup != null ) { module . ModulePermissionGroup = permissionGroup . RequiredPermission ; }
106+
65107 foreach ( var parameter in parameters )
66108 {
67109 module . ModuleCommandParameters . Add ( ( parameter . Name , parameter . IsOptional ) ) ;
68110 }
69111
112+ module . ModuleInteractionMethod = async ( args ) =>
113+ {
114+ var finalArgs = new object [ method . GetParameters ( ) . Length ] ;
115+
116+ for ( int i = 0 ; i < args . Length ; i ++ )
117+ {
118+ finalArgs [ i ] = args [ i ] ;
119+ }
120+
121+ for ( int i = args . Length ; i < finalArgs . Length ; i ++ )
122+ {
123+ var parameter = method . GetParameters ( ) [ i ] ;
124+ finalArgs [ i ] = parameter . DefaultValue ;
125+ }
126+
127+ await ( Task ) method . Invoke ( Activator . CreateInstance ( moduleType ) , finalArgs ) ;
128+ } ;
129+
130+ InteractionBase moduleInstance = Activator . CreateInstance < T > ( ) ;
131+ module . ModuleInteractionBase = moduleInstance ;
132+
70133 this . InteractionModules . Add ( commandAttribute . CommandName , module ) ;
71134 }
72135 return Task . CompletedTask ;
73136 }
74137
75138
139+
76140 public Task RegisterModules ( Assembly entrypoint )
77141 {
78142 var moduleTypes = entrypoint . GetTypes ( ) . Where ( t => typeof ( InteractionBase ) . IsAssignableFrom ( t ) && ! t . IsAbstract ) ;
@@ -95,23 +159,72 @@ public Task RegisterModules(Assembly entrypoint)
95159 if ( commandAttribute . CommandDescription != null ) { module . ModuleCommandDescription = commandAttribute . CommandDescription ; }
96160 if ( enabledInDmsAttribute != null ) { module . ModuleCommandEnabledInDms = enabledInDmsAttribute . IsEnabledInDms ; }
97161 if ( permissionGroup != null ) { module . ModulePermissionGroup = permissionGroup . RequiredPermission ; }
162+
98163 foreach ( var parameter in parameters )
99164 {
100165 module . ModuleCommandParameters . Add ( ( parameter . Name , parameter . IsOptional ) ) ;
101166 }
102167
168+ module . ModuleInteractionMethod = async ( args ) =>
169+ {
170+ var finalArgs = new object [ method . GetParameters ( ) . Length ] ;
171+
172+ for ( int i = 0 ; i < args . Length ; i ++ )
173+ {
174+ finalArgs [ i ] = args [ i ] ;
175+ }
176+
177+ for ( int i = args . Length ; i < finalArgs . Length ; i ++ )
178+ {
179+ var parameter = method . GetParameters ( ) [ i ] ;
180+ finalArgs [ i ] = parameter . DefaultValue ;
181+ }
182+
183+ await ( Task ) method . Invoke ( Activator . CreateInstance ( moduleType ) , finalArgs ) ;
184+ } ;
185+
186+
187+ InteractionBase moduleInstance = ( InteractionBase ) Activator . CreateInstance ( moduleType ) ;
188+ if ( typeof ( InteractionBase ) . IsAssignableFrom ( moduleType ) )
189+ {
190+ module . ModuleInteractionBase = moduleInstance ;
191+ }
192+
193+
194+
103195 this . InteractionModules . Add ( commandAttribute . CommandName , module ) ;
104196 }
105197 }
106198 return Task . CompletedTask ;
107199 }
108200
109- public bool HandleInteraction ( Objects . Interaction interaction )
201+ public void HandleInteraction ( Objects . Interaction interactionContext )
110202 {
111- return true ;
203+ List < object > args = new List < object > ( ) { interactionContext } ;
204+
205+ for ( int i = 1 ; i < interactionContext . InteractionBaseModule . ModuleCommandParameters . Count ; i ++ )
206+ {
207+ if ( i > interactionContext . InteractionParameters . Count ) { break ; }
208+ var interactionParameter = interactionContext . InteractionParameters [ i - 1 ] ;
209+ var moduleParameter = interactionContext . InteractionBaseModule . ModuleCommandParameters [ i ] ;
210+
211+ switch ( moduleParameter . Item1 . ToLower ( ) )
212+ {
213+ case "string" :
214+ args . Add ( interactionParameter ) ;
215+ break ;
216+ case "string[]" :
217+ string [ ] textParams = new string [ ] { string . Join ( " " , interactionContext . InteractionParameters . Skip ( i - 1 ) ) } ;
218+ args . Add ( textParams ) ;
219+ break ;
220+ }
221+ }
222+ interactionContext . InteractionBaseModule . ModuleInteractionMethod . Invoke ( args . ToArray ( ) ) ;
112223 }
113224
114225
226+
227+
115228 private async Task HandleInteractionQueue ( )
116229 {
117230 while ( true )
0 commit comments