about summary refs log tree commit diff
path: root/MatrixContentFilter/Commands/ConfigureCommand.cs
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-10-04 19:51:44 +0200
committerRory& <root@rory.gay>2024-10-04 19:51:44 +0200
commitc8f7ef7c1d2bd705a5442c0dc591b8e5a50673a5 (patch)
tree9b951c6e2c120ec370ce8318238aadbdda880a89 /MatrixContentFilter/Commands/ConfigureCommand.cs
downloadMatrixContentFilter-master.tar.xz
Initial commit HEAD master
Diffstat (limited to 'MatrixContentFilter/Commands/ConfigureCommand.cs')
-rw-r--r--MatrixContentFilter/Commands/ConfigureCommand.cs38
1 files changed, 38 insertions, 0 deletions
diff --git a/MatrixContentFilter/Commands/ConfigureCommand.cs b/MatrixContentFilter/Commands/ConfigureCommand.cs
new file mode 100644
index 0000000..756fc14
--- /dev/null
+++ b/MatrixContentFilter/Commands/ConfigureCommand.cs
@@ -0,0 +1,38 @@
+using System.Text;
+using LibMatrix.EventTypes.Spec;
+using LibMatrix.Utilities.Bot.Commands;
+using LibMatrix.Utilities.Bot.Interfaces;
+using Microsoft.Extensions.DependencyInjection;
+
+namespace MatrixContentFilter.Commands;
+
+public class ConfigureCommand(IServiceProvider svcs) : ICommandGroup {
+    public string Name { get; } = "configure";
+    public string[]? Aliases { get; } = ["config", "cfg"];
+    public string Description { get; }
+    public bool Unlisted { get; } = true;
+
+    public async Task Invoke(CommandContext ctx) {
+        var commands = svcs.GetServices<ICommand>().Where(x => x.GetType().IsAssignableTo(typeof(ICommand<>).MakeGenericType(GetType()))).ToList();
+
+        if (ctx.Args.Length == 0) {
+            await ctx.Room.SendMessageEventAsync(HelpCommand.GenerateCommandList(commands).Build());
+        }
+        else {
+            var subcommand = ctx.Args[0];
+            var command = commands.FirstOrDefault(x => x.Name == subcommand || x.Aliases?.Contains(subcommand) == true);
+            if (command == null) {
+                await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent("m.notice", "Unknown subcommand"));
+                return;
+            }
+
+            await command.Invoke(new CommandContext {
+                Room = ctx.Room,
+                MessageEvent = ctx.MessageEvent,
+                CommandName = ctx.CommandName,
+                Args = ctx.Args.Skip(1).ToArray(),
+                Homeserver = ctx.Homeserver
+            });
+        }
+    }
+}
\ No newline at end of file