From 163e2a94f600ffe0f982e3f605264ff2f2fe312b Mon Sep 17 00:00:00 2001 From: "Emma [it/its]@Rory&" Date: Fri, 9 Feb 2024 16:33:14 +0100 Subject: Apply syntax style to LibMatrix side projects --- .../LibMatrix.Utilities.Bot/AppServiceConfiguration.cs | 2 +- .../LibMatrix.Utilities.Bot/BotCommandInstaller.cs | 3 ++- .../LibMatrix.Utilities.Bot/Commands/HelpCommand.cs | 8 +++----- .../LibMatrix.Utilities.Bot/Commands/PingCommand.cs | 6 ++---- .../LibMatrix.Utilities.Bot/FileStorageProvider.cs | 6 ++---- .../Interfaces/CommandContext.cs | 2 +- .../LibMatrix.Utilities.Bot/Interfaces/ICommand.cs | 6 ++---- .../LibMatrix.Utilities.Bot.csproj | 8 ++++---- .../LibMatrixBotConfiguration.cs | 2 +- .../Services/CommandListenerHostedService.cs | 18 ++++++++---------- 10 files changed, 26 insertions(+), 35 deletions(-) (limited to 'Utilities/LibMatrix.Utilities.Bot') diff --git a/Utilities/LibMatrix.Utilities.Bot/AppServiceConfiguration.cs b/Utilities/LibMatrix.Utilities.Bot/AppServiceConfiguration.cs index d8d1094..afda89e 100644 --- a/Utilities/LibMatrix.Utilities.Bot/AppServiceConfiguration.cs +++ b/Utilities/LibMatrix.Utilities.Bot/AppServiceConfiguration.cs @@ -84,4 +84,4 @@ public class AppServiceConfiguration { return yaml; } -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs index 25a8d92..eb67424 100644 --- a/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs +++ b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs @@ -34,6 +34,7 @@ public static class BotCommandInstaller { services.AddHostedService(); // services.AddSingleton(); } + return services; } -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/Commands/HelpCommand.cs b/Utilities/LibMatrix.Utilities.Bot/Commands/HelpCommand.cs index 4fe1038..9937b3c 100644 --- a/Utilities/LibMatrix.Utilities.Bot/Commands/HelpCommand.cs +++ b/Utilities/LibMatrix.Utilities.Bot/Commands/HelpCommand.cs @@ -13,10 +13,8 @@ public class HelpCommand(IServiceProvider services) : ICommand { var sb = new StringBuilder(); sb.AppendLine("Available commands:"); var commands = services.GetServices().ToList(); - foreach (var command in commands) { - sb.AppendLine($"- {command.Name}: {command.Description}"); - } + foreach (var command in commands) sb.AppendLine($"- {command.Name}: {command.Description}"); - await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent(messageType: "m.notice", body: sb.ToString())); + await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent("m.notice", sb.ToString())); } -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs b/Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs index 16712ea..b5fb868 100644 --- a/Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs +++ b/Utilities/LibMatrix.Utilities.Bot/Commands/PingCommand.cs @@ -7,7 +7,5 @@ public class PingCommand : ICommand { public string Name { get; } = "ping"; public string Description { get; } = "Pong!"; - public async Task Invoke(CommandContext ctx) { - await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent(body: "pong!")); - } -} + public async Task Invoke(CommandContext ctx) => await ctx.Room.SendMessageEventAsync(new RoomMessageEventContent(body: "pong!")); +} \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/FileStorageProvider.cs b/Utilities/LibMatrix.Utilities.Bot/FileStorageProvider.cs index b66fbf5..b762937 100644 --- a/Utilities/LibMatrix.Utilities.Bot/FileStorageProvider.cs +++ b/Utilities/LibMatrix.Utilities.Bot/FileStorageProvider.cs @@ -14,9 +14,7 @@ public class FileStorageProvider : IStorageProvider { public FileStorageProvider(string targetPath) { Console.WriteLine($"Initialised FileStorageProvider with path {targetPath}"); TargetPath = targetPath; - if (!Directory.Exists(targetPath)) { - Directory.CreateDirectory(targetPath); - } + if (!Directory.Exists(targetPath)) Directory.CreateDirectory(targetPath); } public async Task SaveObjectAsync(string key, T value) => await File.WriteAllTextAsync(Path.Join(TargetPath, key), value?.ToJson()); @@ -31,4 +29,4 @@ public class FileStorageProvider : IStorageProvider { File.Delete(Path.Join(TargetPath, key)); return Task.CompletedTask; } -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/Interfaces/CommandContext.cs b/Utilities/LibMatrix.Utilities.Bot/Interfaces/CommandContext.cs index 94ea846..e65f86d 100644 --- a/Utilities/LibMatrix.Utilities.Bot/Interfaces/CommandContext.cs +++ b/Utilities/LibMatrix.Utilities.Bot/Interfaces/CommandContext.cs @@ -19,4 +19,4 @@ public class CommandContext { public required AuthenticatedHomeserverGeneric Homeserver { get; set; } public async Task Reply(RoomMessageEventContent content) => await Room.SendMessageEventAsync(content); -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/Interfaces/ICommand.cs b/Utilities/LibMatrix.Utilities.Bot/Interfaces/ICommand.cs index 82439e8..453a8fe 100644 --- a/Utilities/LibMatrix.Utilities.Bot/Interfaces/ICommand.cs +++ b/Utilities/LibMatrix.Utilities.Bot/Interfaces/ICommand.cs @@ -4,9 +4,7 @@ public interface ICommand { public string Name { get; } public string Description { get; } - public Task CanInvoke(CommandContext ctx) { - return Task.FromResult(true); - } + public Task CanInvoke(CommandContext ctx) => Task.FromResult(true); public Task Invoke(CommandContext ctx); -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/LibMatrix.Utilities.Bot.csproj b/Utilities/LibMatrix.Utilities.Bot/LibMatrix.Utilities.Bot.csproj index e16c00c..89ea5af 100644 --- a/Utilities/LibMatrix.Utilities.Bot/LibMatrix.Utilities.Bot.csproj +++ b/Utilities/LibMatrix.Utilities.Bot/LibMatrix.Utilities.Bot.csproj @@ -8,13 +8,13 @@ - + - - - + + + diff --git a/Utilities/LibMatrix.Utilities.Bot/LibMatrixBotConfiguration.cs b/Utilities/LibMatrix.Utilities.Bot/LibMatrixBotConfiguration.cs index 27ce06b..d607637 100644 --- a/Utilities/LibMatrix.Utilities.Bot/LibMatrixBotConfiguration.cs +++ b/Utilities/LibMatrix.Utilities.Bot/LibMatrixBotConfiguration.cs @@ -8,4 +8,4 @@ public class LibMatrixBotConfiguration { public string AccessToken { get; set; } = ""; public string Prefix { get; set; } = "?"; public string? LogRoom { get; set; } -} +} \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs index 9949631..11ee740 100644 --- a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs +++ b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs @@ -18,7 +18,7 @@ public class CommandListenerHostedService : IHostedService { public CommandListenerHostedService(AuthenticatedHomeserverGeneric hs, ILogger logger, IServiceProvider services, LibMatrixBotConfiguration config) { - logger.LogInformation("{} instantiated!", this.GetType().Name); + logger.LogInformation("{} instantiated!", GetType().Name); _hs = hs; _logger = logger; _config = config; @@ -44,7 +44,7 @@ public class CommandListenerHostedService : IHostedService { try { var room = _hs.GetRoom(@event.RoomId); // _logger.LogInformation(eventResponse.ToJson(indent: false)); - if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventContent message }) { + if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventContent message }) if (message is { MessageType: "m.text" }) { var messageContentWithoutReply = message.Body.Split('\n', StringSplitOptions.RemoveEmptyEntries).SkipWhile(x => x.StartsWith(">")).Aggregate((x, y) => $"{x}\n{y}"); @@ -52,7 +52,7 @@ public class CommandListenerHostedService : IHostedService { var command = _commands.FirstOrDefault(x => x.Name == messageContentWithoutReply.Split(' ')[0][_config.Prefix.Length..]); if (command == null) { await room.SendMessageEventAsync( - new RoomMessageEventContent(messageType: "m.notice", body: "Command not found!")); + new RoomMessageEventContent("m.notice", "Command not found!")); return; } @@ -62,7 +62,7 @@ public class CommandListenerHostedService : IHostedService { Homeserver = _hs }; - if (await command.CanInvoke(ctx)) { + if (await command.CanInvoke(ctx)) try { await command.Invoke(ctx); } @@ -70,14 +70,11 @@ public class CommandListenerHostedService : IHostedService { await room.SendMessageEventAsync( MessageFormatter.FormatException("An error occurred during the execution of this command", e)); } - } - else { + else await room.SendMessageEventAsync( - new RoomMessageEventContent(messageType: "m.notice", body: "You do not have permission to run this command!")); - } + new RoomMessageEventContent("m.notice", "You do not have permission to run this command!")); } } - } } catch (Exception e) { _logger.LogError(e, "Error in command listener!"); @@ -94,6 +91,7 @@ public class CommandListenerHostedService : IHostedService { _logger.LogError("Could not shut down command listener task because it was null!"); return; } + await _listenerTask.WaitAsync(cancellationToken); } -} +} \ No newline at end of file -- cgit 1.4.1