about summary refs log tree commit diff
path: root/OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBot.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-10-10 21:24:28 +0200
committerEmma [it/its]@Rory& <root@rory.gay>2023-10-10 21:44:30 +0200
commit06be7bed0c640c21503f75ce834a6fde2fc905ba (patch)
treeeff4ef92267dd79ce308ec2df348ae6ef50e8ecc /OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBot.cs
downloadOsuFederatedBeatmapApi-06be7bed0c640c21503f75ce834a6fde2fc905ba.tar.xz
Initial commit
Diffstat (limited to 'OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBot.cs')
-rw-r--r--OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBot.cs92
1 files changed, 92 insertions, 0 deletions
diff --git a/OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBot.cs b/OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBot.cs
new file mode 100644
index 0000000..2b39d93
--- /dev/null
+++ b/OsuFederatedBeatmapApi/Services/FederatedBeatmapApiBot.cs
@@ -0,0 +1,92 @@
+using ArcaneLibs.Extensions;
+using LibMatrix.EventTypes.Spec;
+using LibMatrix.EventTypes.Spec.State;
+using LibMatrix.Helpers;
+using LibMatrix.Homeservers;
+using LibMatrix.Services;
+using LibMatrix.Utilities.Bot.Interfaces;
+
+namespace OsuFederatedBeatmapApi.Services;
+
+public class FederatedBeatmapApiBot(AuthenticatedHomeserverGeneric hs,
+    ILogger<FederatedBeatmapApiBot> logger,
+    FederatedBeatmapApiBotConfiguration configuration,
+    HomeserverResolverService hsResolver,
+    FederatedBeatmapApiBotAccountDataService accountDataService) : IHostedService {
+    private readonly IEnumerable<ICommand> _commands;
+
+    private Task _listenerTask;
+
+    /// <summary>Triggered when the application host is ready to start the service.</summary>
+    /// <param name="cancellationToken">Indicates that the start process has been aborted.</param>
+    public async Task StartAsync(CancellationToken cancellationToken) {
+        _listenerTask = Run(cancellationToken);
+        logger.LogInformation("Bot started!");
+    }
+
+    private async Task Run(CancellationToken cancellationToken) {
+        Directory.GetFiles("bot_data/cache").ToList().ForEach(File.Delete);
+
+        var syncHelper = new SyncHelper(hs);
+
+        List<string> admins = new();
+
+#pragma warning disable CS4014 // We don't care if this doesn't wait
+        Task.Run(async () => {
+            while (!cancellationToken.IsCancellationRequested) {
+                var controlRoomMembers = accountDataService.ControlRoom.GetMembersAsync();
+                await foreach (var member in controlRoomMembers) {
+                    if ((member.TypedContent as RoomMemberEventContent)?
+                        .Membership == "join") admins.Add(member.UserId);
+                }
+
+                await Task.Delay(TimeSpan.FromSeconds(30), cancellationToken);
+            }
+        }, cancellationToken);
+#pragma warning restore CS4014
+
+        syncHelper.InviteReceivedHandlers.Add(async Task (args) => {
+            var inviteEvent =
+                args.Value.InviteState.Events.FirstOrDefault(x =>
+                    x.Type == "m.room.member" && x.StateKey == hs.UserId);
+            logger.LogInformation("Got invite to {RoomId} by {Sender} with reason: {Reason}", args.Key, inviteEvent!.Sender,
+                (inviteEvent.TypedContent as RoomMemberEventContent)!.Reason);
+            if (inviteEvent.Sender.EndsWith(":rory.gay") || inviteEvent!.Sender.EndsWith(":conduit.rory.gay") || admins.Contains(inviteEvent.Sender)) {
+                try {
+                    var senderProfile = await hs.GetProfileAsync(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);
+                }
+            }
+        });
+
+        syncHelper.TimelineEventHandlers.Add(async @event => {
+            var room = hs.GetRoom(@event.RoomId);
+            try {
+                logger.LogInformation(
+                    "Got timeline event in {}: {}", @event.RoomId, @event.ToJson(indent: true, ignoreNull: true));
+
+                if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventContent message }) { }
+            }
+            catch (Exception e) {
+                logger.LogError("{}", e.ToString());
+                await accountDataService.ControlRoom.SendMessageEventAsync(
+                    MessageFormatter.FormatException($"Exception handling event {MessageFormatter.HtmlFormatMention(room.RoomId)}", e));
+                await accountDataService.LogRoom.SendMessageEventAsync(
+                    MessageFormatter.FormatException($"Exception handling event {MessageFormatter.HtmlFormatMention(room.RoomId)}", e));
+                await using var stream = new MemoryStream(e.ToString().AsBytes().ToArray());
+                await accountDataService.ControlRoom.SendFileAsync("error.log.cs", stream);
+                await accountDataService.LogRoom.SendFileAsync("error.log.cs", stream);
+            }
+        });
+    }
+
+    /// <summary>Triggered when the application host is performing a graceful shutdown.</summary>
+    /// <param name="cancellationToken">Indicates that the shutdown process should no longer be graceful.</param>
+    public async Task StopAsync(CancellationToken cancellationToken) {
+        logger.LogInformation("Shutting down bot!");
+    }
+}