Skip to content

Commit ca772ed

Browse files
committed
finished v1
1 parent 632fe03 commit ca772ed

12 files changed

Lines changed: 316 additions & 14 deletions

Amino.NET.Interactions/Command.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Amino.Interactions.Attributes
8+
{
9+
public class Command : Attribute
10+
{
11+
public string CommandName { get; }
12+
public string CommandDescription { get; }
13+
public string CommunityId { get; }
14+
15+
public Command(string commandName, string commandDescription = null, string communityId = null)
16+
{
17+
this.CommandName = commandName;
18+
this.CommunityId = communityId;
19+
this.CommandDescription = commandDescription;
20+
}
21+
22+
23+
}
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Amino.Interactions.Attributes
8+
{
9+
public class EnabledInDms : Attribute
10+
{
11+
public bool IsEnabledInDms { get; } = true;
12+
13+
public EnabledInDms(bool isEnabledInDms)
14+
{
15+
IsEnabledInDms = isEnabledInDms;
16+
}
17+
}
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Amino.Interactions
9+
{
10+
public class ParameterInfo
11+
{
12+
public string Name { get; set; }
13+
public bool IsOptional { get; set; }
14+
}
15+
16+
public class FunctionAnalyzer
17+
{
18+
public ParameterInfo[] GetParameters(MethodInfo method)
19+
{
20+
return method.GetParameters()
21+
.Select(p => new ParameterInfo
22+
{
23+
Name = p.ParameterType.Name,
24+
IsOptional = p.HasDefaultValue
25+
})
26+
.ToArray();
27+
}
28+
}
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Amino.Interactions.Objects
8+
{
9+
public class Interaction
10+
{
11+
public string? InteractionChatId { get; set; }
12+
public string? InteractionName { get; set; }
13+
public Amino.Client AminoClient { get; set; }
14+
public Amino.Objects.Message? Message { get; set; }
15+
public long InteractionTimestamp { get; set; }
16+
public string? InteractionId { get; set; }
17+
public List<string> InteractionParameters { get; set; } = new List<string>();
18+
public InteractionModule InteractionBaseModule { get; set; }
19+
20+
}
21+
}
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Amino.Interactions.Objects;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -8,8 +9,12 @@ namespace Amino.Interactions
89
{
910
public class InteractionBase
1011
{
11-
public Objects.Interaction Context;
1212

13-
13+
14+
public Task Respond(Interaction context, string message, bool asReply)
15+
{
16+
return Task.CompletedTask;
17+
}
18+
1419
}
1520
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Amino.Interactions.Attributes;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Amino.Interactions.Objects
9+
{
10+
public class InteractionModule
11+
{
12+
public string ModuleCommandName { get; set; }
13+
public string ModuleCommandDescription { get; set; }
14+
public int? ModuleCommandCommunity { get; set; }
15+
public InteractionBase ModuleInteractionBase { get; set; }
16+
17+
public bool ModuleCommandEnabledInDms { get; set; } = true;
18+
public PermissionGroup.PermissionGroups ModulePermissionGroup { get; set; } = PermissionGroup.PermissionGroups.All;
19+
20+
public List<(string, bool)> ModuleCommandParameters { get; set; } = new List<(string, bool)>();
21+
22+
public delegate Task InteractionMethodDelegate(params object[] args);
23+
24+
public InteractionMethodDelegate ModuleInteractionMethod { get; set; }
25+
26+
}
27+
}

Amino.NET.Interactions/InteractionsClient.cs

Lines changed: 118 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
using Amino.Interactions.Attributes;
22
using Amino.Interactions.Objects;
3+
using Newtonsoft.Json.Linq;
34
using ReflectionsTest;
45
using System;
56
using System.Collections.Generic;
67
using System.Linq;
78
using System.Reflection;
89
using System.Runtime.CompilerServices;
910
using System.Text;
11+
using System.Text.Json;
1012
using System.Threading.Tasks;
13+
using System.Xml;
1114

1215
namespace 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)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Amino.Interactions.Objects
8+
{
9+
public class LogMessage
10+
{
11+
public string Message { get; set; }
12+
public long Timestamp { get; set; }
13+
public InteractionsClient.LogLevels LogLevel { get; set; }
14+
15+
}
16+
}

Amino.NET.Interactions/Objects/Interaction.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ namespace Amino.Interactions.Objects
88
{
99
public class Interaction
1010
{
11-
public string InteractionChatId { get; set; }
12-
public string InteractionName { get; set; }
11+
public string? InteractionChatId { get; set; }
12+
public string? InteractionName { get; set; }
1313
public Amino.Client AminoClient { get; set; }
14-
public Objects.InteractionModule BaseModule { get; set; }
15-
public Amino.Objects.Message Message { get; set; }
14+
public Amino.Objects.Message? Message { get; set; }
1615
public long InteractionTimestamp { get; set; }
17-
public string InteractionId { get; set; }
16+
public string? InteractionId { get; set; }
17+
public List<string> InteractionParameters { get; set; } = new List<string>();
18+
public InteractionModule InteractionBaseModule { get; set; }
1819

1920
}
2021
}

0 commit comments

Comments
 (0)