about summary refs log tree commit diff
path: root/ExampleBots/PluralContactBotPoC/Bot/Commands
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-15 09:50:45 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-15 09:50:45 +0200
commit6bd02248ccfbcb46960a6f39eaad23888d190eb5 (patch)
tree110578f31b6f9f70a7a1edab32fb3a34d6ad4f1a /ExampleBots/PluralContactBotPoC/Bot/Commands
parentMedia moderator PoC works, abstract command handling to library (diff)
downloadLibMatrix-6bd02248ccfbcb46960a6f39eaad23888d190eb5.tar.xz
Some refactoring
Diffstat (limited to '')
-rw-r--r--ExampleBots/PluralContactBotPoC/Bot/Commands/CreateSystemCommand.cs57
1 files changed, 57 insertions, 0 deletions
diff --git a/ExampleBots/PluralContactBotPoC/Bot/Commands/CreateSystemCommand.cs b/ExampleBots/PluralContactBotPoC/Bot/Commands/CreateSystemCommand.cs
new file mode 100644
index 0000000..5da4f5e
--- /dev/null
+++ b/ExampleBots/PluralContactBotPoC/Bot/Commands/CreateSystemCommand.cs
@@ -0,0 +1,57 @@
+using LibMatrix;
+using LibMatrix.Helpers;
+using LibMatrix.Services;
+using LibMatrix.StateEventTypes.Spec;
+using MediaModeratorPoC.Bot.Interfaces;
+using PluralContactBotPoC.Bot.AccountData;
+using PluralContactBotPoC.Bot.StateEventTypes;
+
+namespace PluralContactBotPoC.Bot.Commands;
+
+public class CreateSystemCommand(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver) : ICommand {
+    public string Name { get; } = "createsystem";
+    public string Description { get; } = "Create a new system";
+
+    public async Task<bool> CanInvoke(CommandContext ctx) {
+        return true;
+    }
+
+    public async Task Invoke(CommandContext ctx) {
+        if (ctx.Args.Length != 1) {
+            await ctx.Reply("m.notice", MessageFormatter.FormatError("Only one argument is allowed: system name!"));
+            return;
+        }
+
+        var sysName = ctx.Args[0];
+        try {
+            try {
+                await ctx.Homeserver.GetAccountData<BotData>("gay.rory.plural_contact_bot.system_data");
+                await ctx.Reply("m.notice", MessageFormatter.FormatError($"System {sysName} already exists!"));
+            }
+            catch (MatrixException e) {
+                if (e is { ErrorCode: "M_NOT_FOUND" }) {
+                    var sysData = new SystemData() {
+                        ControlRoom = ctx.Room.RoomId,
+                        Members = new(),
+                    };
+
+                    var state = ctx.Room.GetMembersAsync();
+                    await foreach (var member in state) {
+                        sysData.Members.Add(member.UserId);
+                    }
+
+                    await ctx.Room.SendStateEventAsync("m.room.name", new RoomNameEventContent() {
+                        Name = sysName + " control room"
+                    });
+
+                    return;
+                }
+
+                throw;
+            }
+        }
+        catch (Exception e) {
+            await ctx.Reply("m.notice", MessageFormatter.FormatException("Something went wrong!", e));
+        }
+    }
+}