about summary refs log tree commit diff
path: root/ExampleBots/ModerationBot/Commands/JoinRoomCommand.cs
diff options
context:
space:
mode:
authorEmma [it/its]@Rory& <root@rory.gay>2023-11-23 05:42:33 +0100
committerEmma [it/its]@Rory& <root@rory.gay>2023-11-23 05:42:33 +0100
commit3e934eee892f69a8f78b94950993000522702769 (patch)
tree6aa0d3d26c9a07a7a3e097fe28abb785400bfbd6 /ExampleBots/ModerationBot/Commands/JoinRoomCommand.cs
parentAdd license retroactively, matching where the code originated from (MatrixRoo... (diff)
downloadLibMatrix-3e934eee892f69a8f78b94950993000522702769.tar.xz
Moderation bot work
Diffstat (limited to 'ExampleBots/ModerationBot/Commands/JoinRoomCommand.cs')
-rw-r--r--ExampleBots/ModerationBot/Commands/JoinRoomCommand.cs54
1 files changed, 54 insertions, 0 deletions
diff --git a/ExampleBots/ModerationBot/Commands/JoinRoomCommand.cs b/ExampleBots/ModerationBot/Commands/JoinRoomCommand.cs
new file mode 100644
index 0000000..19a2c54
--- /dev/null
+++ b/ExampleBots/ModerationBot/Commands/JoinRoomCommand.cs
@@ -0,0 +1,54 @@
+using System.Buffers.Text;
+using System.Security.Cryptography;
+using ArcaneLibs.Extensions;
+using LibMatrix;
+using LibMatrix.EventTypes.Spec;
+using LibMatrix.Helpers;
+using LibMatrix.Services;
+using LibMatrix.Utilities.Bot.Interfaces;
+using ModerationBot.AccountData;
+using ModerationBot.StateEventTypes;
+
+namespace ModerationBot.Commands;
+
+public class JoinRoomCommand(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand {
+    public string Name { get; } = "join";
+    public string Description { get; } = "Join arbitrary rooms";
+
+    public async Task<bool> CanInvoke(CommandContext ctx) {
+        //check if user is admin in control room
+        var botData = await ctx.Homeserver.GetAccountDataAsync<BotData>("gay.rory.moderation_bot_data");
+        var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom);
+        var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasPermission(ctx.MessageEvent.Sender, "m.room.ban");
+        if (!isAdmin) {
+            // await ctx.Reply("You do not have permission to use this command!");
+            await ctx.Homeserver.GetRoom(botData.LogRoom!).SendMessageEventAsync(
+                new RoomMessageEventContent(body: $"User {ctx.MessageEvent.Sender} tried to use command {Name} but does not have permission!", messageType: "m.text"));
+        }
+
+        return isAdmin;
+    }
+
+    public async Task Invoke(CommandContext ctx) {
+        
+        var botData = await ctx.Homeserver.GetAccountDataAsync<BotData>("gay.rory.moderation_bot_data");
+        var policyRoom = ctx.Homeserver.GetRoom(botData.DefaultPolicyRoom ?? botData.ControlRoom);
+        var logRoom = ctx.Homeserver.GetRoom(botData.LogRoom ?? botData.ControlRoom);
+
+        await logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Joining room {ctx.Args[0]} with reason: {string.Join(' ', ctx.Args[1..])}"));
+        var roomId = ctx.Args[0];
+        var servers = new List<string>() {ctx.Homeserver.ServerName};
+        if (roomId.StartsWith('[')) {
+            
+        }
+
+        if (roomId.StartsWith('#')) {
+            var res = await ctx.Homeserver.ResolveRoomAliasAsync(roomId);
+            roomId = res.RoomId;
+            servers.AddRange(servers);
+        }
+
+        await ctx.Homeserver.JoinRoomAsync(roomId, servers, string.Join(' ', ctx.Args[1..]));
+        await logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Resolved room {ctx.Args[0]} to {roomId} with servers: {string.Join(", ", servers)}"));
+    }
+}