about summary refs log tree commit diff
path: root/ExampleBots
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-29 19:38:00 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-29 19:38:00 +0200
commit46df5b8e335754f1582fc4d41d9546808ed8ee66 (patch)
tree29fed832bed495f1bd233c37275cb560c19f34cf /ExampleBots
parentAdd more stuff, add unit tests (diff)
downloadLibMatrix-46df5b8e335754f1582fc4d41d9546808ed8ee66.tar.xz
Unit tests, small refactors
Diffstat (limited to 'ExampleBots')
-rw-r--r--ExampleBots/LibMatrix.ExampleBot/Bot/MRUBot.cs2
-rw-r--r--ExampleBots/MediaModeratorPoC/Bot/Commands/BanMediaCommand.cs2
-rw-r--r--ExampleBots/MediaModeratorPoC/Bot/MediaModBot.cs20
-rw-r--r--ExampleBots/MediaModeratorPoC/Bot/StateEventTypes/MediaPolicyStateEventData.cs3
-rw-r--r--ExampleBots/PluralContactBotPoC/Bot/PluralContactBot.cs2
5 files changed, 18 insertions, 11 deletions
diff --git a/ExampleBots/LibMatrix.ExampleBot/Bot/MRUBot.cs b/ExampleBots/LibMatrix.ExampleBot/Bot/MRUBot.cs
index f04ec3a..0211f74 100644
--- a/ExampleBots/LibMatrix.ExampleBot/Bot/MRUBot.cs
+++ b/ExampleBots/LibMatrix.ExampleBot/Bot/MRUBot.cs
@@ -57,7 +57,7 @@ public class MRUBot : IHostedService {
         hs.SyncHelper.InviteReceivedHandlers.Add(async Task (args) => {
             var inviteEvent =
                 args.Value.InviteState.Events.FirstOrDefault(x =>
-                    x.Type == "m.room.member" && x.StateKey == hs.WhoAmI.UserId);
+                    x.Type == "m.room.member" && x.StateKey == hs.UserId);
             _logger.LogInformation(
                 $"Got invite to {args.Key} by {inviteEvent.Sender} with reason: {(inviteEvent.TypedContent as RoomMemberEventContent).Reason}");
             if (inviteEvent.Sender.EndsWith(":rory.gay") || inviteEvent.Sender == "@mxidupwitch:the-apothecary.club") {
diff --git a/ExampleBots/MediaModeratorPoC/Bot/Commands/BanMediaCommand.cs b/ExampleBots/MediaModeratorPoC/Bot/Commands/BanMediaCommand.cs
index d633f89..fd6866c 100644
--- a/ExampleBots/MediaModeratorPoC/Bot/Commands/BanMediaCommand.cs
+++ b/ExampleBots/MediaModeratorPoC/Bot/Commands/BanMediaCommand.cs
@@ -43,7 +43,7 @@ public class BanMediaCommand(IServiceProvider services, HomeserverProviderServic
                         messageType: "m.text"));
 
                 //get replied message
-                var repliedMessage = await ctx.Room.GetEvent<StateEventResponse>(messageContent.RelatesTo!.InReplyTo!.EventId);
+                var repliedMessage = await ctx.Room.GetEventAsync<StateEventResponse>(messageContent.RelatesTo!.InReplyTo!.EventId);
 
                 //check if recommendation is in list
                 if (ctx.Args.Length < 2) {
diff --git a/ExampleBots/MediaModeratorPoC/Bot/MediaModBot.cs b/ExampleBots/MediaModeratorPoC/Bot/MediaModBot.cs
index e6ba269..f9bbcf3 100644
--- a/ExampleBots/MediaModeratorPoC/Bot/MediaModBot.cs
+++ b/ExampleBots/MediaModeratorPoC/Bot/MediaModBot.cs
@@ -109,17 +109,17 @@ public class MediaModBot(AuthenticatedHomeserverGeneric hs, ILogger<MediaModBot>
         hs.SyncHelper.InviteReceivedHandlers.Add(async Task (args) => {
             var inviteEvent =
                 args.Value.InviteState.Events.FirstOrDefault(x =>
-                    x.Type == "m.room.member" && x.StateKey == hs.WhoAmI.UserId);
+                    x.Type == "m.room.member" && x.StateKey == hs.UserId);
             logger.LogInformation(
                 $"Got invite to {args.Key} by {inviteEvent.Sender} with reason: {(inviteEvent.TypedContent as RoomMemberEventContent).Reason}");
             if (inviteEvent.Sender.EndsWith(":rory.gay") || inviteEvent.Sender.EndsWith(":conduit.rory.gay")) {
                 try {
                     var senderProfile = await hs.GetProfileAsync(inviteEvent.Sender);
-                    await (hs.GetRoom(args.Key)).JoinAsync(reason: $"I was invited by {senderProfile.DisplayName ?? inviteEvent.Sender}!");
+                    await hs.GetRoom(args.Key).JoinAsync(reason: $"I was invited by {senderProfile.DisplayName ?? inviteEvent.Sender}!");
                 }
                 catch (Exception e) {
                     logger.LogError("{}", e.ToString());
-                    await (hs.GetRoom(args.Key)).LeaveAsync(reason: "I was unable to join the room: " + e);
+                    await hs.GetRoom(args.Key).LeaveAsync(reason: "I was unable to join the room: " + e);
                 }
             }
         });
@@ -161,16 +161,16 @@ public class MediaModBot(AuthenticatedHomeserverGeneric hs, ILogger<MediaModBot>
                             case "warn": {
                                 await room.SendMessageEventAsync(
                                     new RoomMessageEventContent(
-                                        body: $"Please be careful when posting this image: {matchedpolicyData.Reason}",
+                                        body: $"Please be careful when posting this image: {matchedpolicyData.Reason ?? "No reason specified"}",
                                         messageType: "m.text") {
                                         Format = "org.matrix.custom.html",
                                         FormattedBody =
-                                            $"<font color=\"#FFFF00\">Please be careful when posting this image: {matchedpolicyData.Reason}</a></font>"
+                                            $"<font color=\"#FFFF00\">Please be careful when posting this image: {matchedpolicyData.Reason ?? "No reason specified"}</a></font>"
                                     });
                                 break;
                             }
                             case "redact": {
-                                await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason);
+                                await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason ?? "No reason specified");
                                 break;
                             }
                             case "spoiler": {
@@ -220,9 +220,15 @@ public class MediaModBot(AuthenticatedHomeserverGeneric hs, ILogger<MediaModBot>
                                 await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason);
                                 //change powerlevel to -1
                                 var currentPls = await room.GetPowerLevelsAsync();
+                                if(currentPls is null) {
+                                    logger.LogWarning("Unable to get power levels for {room}", room.RoomId);
+                                    await _logRoom.SendMessageEventAsync(
+                                        MessageFormatter.FormatError($"Unable to get power levels for {MessageFormatter.HtmlFormatMention(room.RoomId)}"));
+                                    return;
+                                }
+                                currentPls.Users ??= new();
                                 currentPls.Users[@event.Sender] = -1;
                                 await room.SendStateEventAsync("m.room.power_levels", currentPls);
-
                                 break;
                             }
                             case "kick": {
diff --git a/ExampleBots/MediaModeratorPoC/Bot/StateEventTypes/MediaPolicyStateEventData.cs b/ExampleBots/MediaModeratorPoC/Bot/StateEventTypes/MediaPolicyStateEventData.cs
index 6686a37..0096c78 100644
--- a/ExampleBots/MediaModeratorPoC/Bot/StateEventTypes/MediaPolicyStateEventData.cs
+++ b/ExampleBots/MediaModeratorPoC/Bot/StateEventTypes/MediaPolicyStateEventData.cs
@@ -4,7 +4,8 @@ using LibMatrix.Interfaces;
 
 namespace MediaModeratorPoC.Bot.StateEventTypes;
 
-[MatrixEvent(EventName = "gay.rory.media_moderator_poc.rule.homeserver")]
+[
+    MatrixEvent(EventName = "gay.rory.media_moderator_poc.rule.homeserver")]
 [MatrixEvent(EventName = "gay.rory.media_moderator_poc.rule.media")]
 public class MediaPolicyEventContent : EventContent {
     /// <summary>
diff --git a/ExampleBots/PluralContactBotPoC/Bot/PluralContactBot.cs b/ExampleBots/PluralContactBotPoC/Bot/PluralContactBot.cs
index c3cebe2..0bd2bbf 100644
--- a/ExampleBots/PluralContactBotPoC/Bot/PluralContactBot.cs
+++ b/ExampleBots/PluralContactBotPoC/Bot/PluralContactBot.cs
@@ -41,7 +41,7 @@ public class PluralContactBot(AuthenticatedHomeserverGeneric hs, ILogger<PluralC
         hs.SyncHelper.InviteReceivedHandlers.Add(async Task (args) => {
             var inviteEvent =
                 args.Value.InviteState.Events.FirstOrDefault(x =>
-                    x.Type == "m.room.member" && x.StateKey == hs.WhoAmI.UserId);
+                    x.Type == "m.room.member" && x.StateKey == hs.UserId);
             logger.LogInformation("Got invite to {} by {} with reason: {}", args.Key, inviteEvent.Sender, (inviteEvent.TypedContent as RoomMemberEventContent).Reason);
 
             try {