From a331eb2f118e0051c6c6744a20d6b0934c4d6d6f Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Wed, 28 Jun 2023 10:38:45 +0200 Subject: Update stuff --- MatrixRoomUtils.Bot/Bot/FileStorageProvider.cs | 4 ++++ MatrixRoomUtils.Bot/Bot/MRUBot.cs | 26 +++++++++++++------------- 2 files changed, 17 insertions(+), 13 deletions(-) (limited to 'MatrixRoomUtils.Bot/Bot') diff --git a/MatrixRoomUtils.Bot/Bot/FileStorageProvider.cs b/MatrixRoomUtils.Bot/Bot/FileStorageProvider.cs index 8d99828..0061c1f 100644 --- a/MatrixRoomUtils.Bot/Bot/FileStorageProvider.cs +++ b/MatrixRoomUtils.Bot/Bot/FileStorageProvider.cs @@ -1,10 +1,13 @@ using System.Text.Json; using MatrixRoomUtils.Core.Extensions; using MatrixRoomUtils.Core.Interfaces.Services; +using Microsoft.Extensions.Logging; namespace MatrixRoomUtils.Bot; public class FileStorageProvider : IStorageProvider { + private readonly ILogger _logger; + public string TargetPath { get; } /// @@ -12,6 +15,7 @@ public class FileStorageProvider : IStorageProvider { /// /// public FileStorageProvider(string targetPath) { + new Logger(new LoggerFactory()).LogInformation("test"); Console.WriteLine($"Initialised FileStorageProvider with path {targetPath}"); TargetPath = targetPath; if(!Directory.Exists(targetPath)) { diff --git a/MatrixRoomUtils.Bot/Bot/MRUBot.cs b/MatrixRoomUtils.Bot/Bot/MRUBot.cs index 81123e0..134019a 100644 --- a/MatrixRoomUtils.Bot/Bot/MRUBot.cs +++ b/MatrixRoomUtils.Bot/Bot/MRUBot.cs @@ -19,13 +19,13 @@ public class MRUBot : IHostedService { public MRUBot(HomeserverProviderService homeserverProviderService, ILogger logger, MRUBotConfiguration configuration, IServiceProvider services) { - Console.WriteLine("MRUBot hosted service instantiated!"); + logger.LogInformation("MRUBot hosted service instantiated!"); _homeserverProviderService = homeserverProviderService; _logger = logger; _configuration = configuration; - Console.WriteLine("Getting commands..."); + _logger.LogInformation("Getting commands..."); _commands = services.GetServices(); - Console.WriteLine($"Got {_commands.Count()} commands!"); + _logger.LogInformation($"Got {_commands.Count()} commands!"); } /// Triggered when the application host is ready to start the service. @@ -50,31 +50,31 @@ public class MRUBot : IHostedService { // foreach (var stateEvent in await room.GetStateAsync>("")) { // var _ = stateEvent.GetType; // } - // Console.WriteLine($"Got room state for {room.RoomId}!"); + // _logger.LogInformation($"Got room state for {room.RoomId}!"); // } - hs.SyncHelper.InviteReceived += async (_, args) => { + 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); - Console.WriteLine( + _logger.LogInformation( $"Got invite to {args.Key} by {inviteEvent.Sender} with reason: {(inviteEvent.TypedContent as RoomMemberEventData).Reason}"); if (inviteEvent.Sender == "@emma:rory.gay") { try { await (await hs.GetRoom(args.Key)).JoinAsync(reason: "I was invited by Emma (Rory&)!"); } catch (Exception e) { - Console.WriteLine(e); + _logger.LogError(e.ToString()); await (await hs.GetRoom(args.Key)).LeaveAsync(reason: "I was unable to join the room: " + e); } } - }; - hs.SyncHelper.TimelineEventReceived += async (_, @event) => { - Console.WriteLine( + }); + hs.SyncHelper.TimelineEventHandlers.Add(async @event => { + _logger.LogInformation( $"Got timeline event in {@event.RoomId}: {@event.ToJson(indent: false, ignoreNull: true)}"); var room = await hs.GetRoom(@event.RoomId); - // Console.WriteLine(eventResponse.ToJson(indent: false)); + // _logger.LogInformation(eventResponse.ToJson(indent: false)); if (@event is { Type: "m.room.message", TypedContent: MessageEventData message }) { if (message is { MessageType: "m.text" } && message.Body.StartsWith(_configuration.Prefix)) { @@ -103,13 +103,13 @@ public class MRUBot : IHostedService { } } } - }; + }); await hs.SyncHelper.RunSyncLoop(cancellationToken); } /// Triggered when the application host is performing a graceful shutdown. /// Indicates that the shutdown process should no longer be graceful. public async Task StopAsync(CancellationToken cancellationToken) { - Console.WriteLine("Shutting down bot!"); + _logger.LogInformation("Shutting down bot!"); } } \ No newline at end of file -- cgit 1.5.1