diff --git a/LibMatrix.ExampleBot/Bot/Commands/CmdCommand.cs b/LibMatrix.ExampleBot/Bot/Commands/CmdCommand.cs
index 7b54b0c..ca10326 100644
--- a/LibMatrix.ExampleBot/Bot/Commands/CmdCommand.cs
+++ b/LibMatrix.ExampleBot/Bot/Commands/CmdCommand.cs
@@ -1,23 +1,23 @@
using LibMatrix.ExampleBot.Bot.Interfaces;
+using LibMatrix.StateEventTypes.Spec;
namespace LibMatrix.ExampleBot.Bot.Commands;
public class CmdCommand : ICommand {
- public string Name { get; } = "cmd";
- public string Description { get; } = "Runs a command on the host system";
+ public string Name => "cmd";
+ public string Description => "Runs a command on the host system";
- public async Task<bool> CanInvoke(CommandContext ctx) {
- return ctx.MessageEvent.Sender.EndsWith(":rory.gay");
+ public Task<bool> CanInvoke(CommandContext ctx) {
+ return Task.FromResult(ctx.MessageEvent.Sender.EndsWith(":rory.gay"));
}
public async Task Invoke(CommandContext ctx) {
- var cmd = "\"";
- foreach (var arg in ctx.Args) cmd += arg + " ";
+ var cmd = ctx.Args.Aggregate("\"", (current, arg) => current + arg + " ");
cmd = cmd.Trim();
cmd += "\"";
- await ctx.Room.SendMessageEventAsync("m.room.message", new() {
+ await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData {
Body = $"Command being executed: `{cmd}`"
});
@@ -34,7 +34,7 @@ public class CmdCommand : ICommand {
msg += output[0] + "\n";
output.RemoveAt(0);
if ((output.Count > 0 && (msg + output[0]).Length > 64000) || output.Count == 0) {
- await ctx.Room.SendMessageEventAsync("m.room.message", new() {
+ await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData {
FormattedBody = $"```ansi\n{msg}\n```",
// Body = Markdig.Markdown.ToHtml(msg),
Format = "org.matrix.custom.html"
@@ -43,4 +43,4 @@ public class CmdCommand : ICommand {
}
}
}
-}
\ No newline at end of file
+}
diff --git a/LibMatrix.ExampleBot/Bot/Commands/HelpCommand.cs b/LibMatrix.ExampleBot/Bot/Commands/HelpCommand.cs
index a259b3e..69766d1 100644
--- a/LibMatrix.ExampleBot/Bot/Commands/HelpCommand.cs
+++ b/LibMatrix.ExampleBot/Bot/Commands/HelpCommand.cs
@@ -1,28 +1,24 @@
using System.Text;
using LibMatrix.ExampleBot.Bot.Interfaces;
+using LibMatrix.StateEventTypes.Spec;
using Microsoft.Extensions.DependencyInjection;
-namespace LibMatrix.ExampleBot.Bot.Commands;
-
-public class HelpCommand : ICommand {
- private readonly IServiceProvider _services;
- public HelpCommand(IServiceProvider services) {
- _services = services;
- }
+namespace LibMatrix.ExampleBot.Bot.Commands;
+public class HelpCommand(IServiceProvider services) : ICommand {
public string Name { get; } = "help";
public string Description { get; } = "Displays this help message";
public async Task Invoke(CommandContext ctx) {
var sb = new StringBuilder();
sb.AppendLine("Available commands:");
- var commands = _services.GetServices<ICommand>().ToList();
+ var commands = services.GetServices<ICommand>().ToList();
foreach (var command in commands) {
sb.AppendLine($"- {command.Name}: {command.Description}");
}
- await ctx.Room.SendMessageEventAsync("m.room.message", new() {
- Body = sb.ToString(),
+ await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData {
+ Body = sb.ToString()
});
}
-}
\ No newline at end of file
+}
diff --git a/LibMatrix.ExampleBot/Bot/Commands/PingCommand.cs b/LibMatrix.ExampleBot/Bot/Commands/PingCommand.cs
index 664dc53..a7c65b5 100644
--- a/LibMatrix.ExampleBot/Bot/Commands/PingCommand.cs
+++ b/LibMatrix.ExampleBot/Bot/Commands/PingCommand.cs
@@ -1,17 +1,15 @@
using LibMatrix.ExampleBot.Bot.Interfaces;
+using LibMatrix.StateEventTypes.Spec;
-namespace LibMatrix.ExampleBot.Bot.Commands;
+namespace LibMatrix.ExampleBot.Bot.Commands;
public class PingCommand : ICommand {
- public PingCommand() {
- }
-
public string Name { get; } = "ping";
public string Description { get; } = "Pong!";
public async Task Invoke(CommandContext ctx) {
- await ctx.Room.SendMessageEventAsync("m.room.message", new() {
+ await ctx.Room.SendMessageEventAsync("m.room.message", new RoomMessageEventData {
Body = "pong!"
});
}
-}
\ No newline at end of file
+}
diff --git a/LibMatrix.ExampleBot/Bot/FileStorageProvider.cs b/LibMatrix.ExampleBot/Bot/FileStorageProvider.cs
index 249aba3..1e84ab7 100644
--- a/LibMatrix.ExampleBot/Bot/FileStorageProvider.cs
+++ b/LibMatrix.ExampleBot/Bot/FileStorageProvider.cs
@@ -23,13 +23,16 @@ public class FileStorageProvider : IStorageProvider {
}
}
- public async Task SaveObjectAsync<T>(string key, T value) => await File.WriteAllTextAsync(Path.Join(TargetPath, key), ObjectExtensions.ToJson(value));
+ public async Task SaveObjectAsync<T>(string key, T value) => await File.WriteAllTextAsync(Path.Join(TargetPath, key), value?.ToJson());
public async Task<T?> LoadObjectAsync<T>(string key) => JsonSerializer.Deserialize<T>(await File.ReadAllTextAsync(Path.Join(TargetPath, key)));
- public async Task<bool> ObjectExistsAsync(string key) => File.Exists(Path.Join(TargetPath, key));
+ public Task<bool> ObjectExistsAsync(string key) => Task.FromResult(File.Exists(Path.Join(TargetPath, key)));
- public async Task<List<string>> GetAllKeysAsync() => Directory.GetFiles(TargetPath).Select(Path.GetFileName).ToList();
+ public Task<List<string>> GetAllKeysAsync() => Task.FromResult(Directory.GetFiles(TargetPath).Select(Path.GetFileName).ToList());
- public async Task DeleteObjectAsync(string key) => File.Delete(Path.Join(TargetPath, key));
+ public Task DeleteObjectAsync(string key) {
+ File.Delete(Path.Join(TargetPath, key));
+ return Task.CompletedTask;
+ }
}
diff --git a/LibMatrix.ExampleBot/Bot/MRUBot.cs b/LibMatrix.ExampleBot/Bot/MRUBot.cs
index eecab84..cdeefe2 100644
--- a/LibMatrix.ExampleBot/Bot/MRUBot.cs
+++ b/LibMatrix.ExampleBot/Bot/MRUBot.cs
@@ -23,7 +23,7 @@ public class MRUBot : IHostedService {
_configuration = configuration;
_logger.LogInformation("Getting commands...");
_commands = services.GetServices<ICommand>();
- _logger.LogInformation($"Got {_commands.Count()} commands!");
+ _logger.LogInformation("Got {} commands!", _commands.Count());
}
/// <summary>Triggered when the application host is ready to start the service.</summary>
@@ -37,7 +37,7 @@ public class MRUBot : IHostedService {
_configuration.AccessToken);
}
catch (Exception e) {
- _logger.LogError(e.Message);
+ _logger.LogError("{}", e.Message);
throw;
}
@@ -57,36 +57,36 @@ public class MRUBot : IHostedService {
x.Type == "m.room.member" && x.StateKey == hs.WhoAmI.UserId);
_logger.LogInformation(
$"Got invite to {args.Key} by {inviteEvent.Sender} with reason: {(inviteEvent.TypedContent as RoomMemberEventData).Reason}");
- if (inviteEvent.Sender.EndsWith(":rory.gay") || inviteEvent.Sender == "@mxidupwitch:the-apothecary.club" ) {
+ if (inviteEvent.Sender.EndsWith(":rory.gay") || inviteEvent.Sender == "@mxidupwitch:the-apothecary.club") {
try {
var senderProfile = await hs.GetProfile(inviteEvent.Sender);
await (await hs.GetRoom(args.Key)).JoinAsync(reason: $"I was invited by {senderProfile.DisplayName ?? inviteEvent.Sender}!");
}
catch (Exception e) {
- _logger.LogError(e.ToString());
+ _logger.LogError("{}", e.ToString());
await (await hs.GetRoom(args.Key)).LeaveAsync(reason: "I was unable to join the room: " + e);
}
}
});
hs.SyncHelper.TimelineEventHandlers.Add(async @event => {
_logger.LogInformation(
- $"Got timeline event in {@event.RoomId}: {@event.ToJson(indent: false, ignoreNull: true)}");
+ "Got timeline event in {}: {}", @event.RoomId, @event.ToJson(indent: false, ignoreNull: true));
var room = await hs.GetRoom(@event.RoomId);
// _logger.LogInformation(eventResponse.ToJson(indent: false));
if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventData message }) {
if (message is { MessageType: "m.text" } && message.Body.StartsWith(_configuration.Prefix)) {
-
var command = _commands.FirstOrDefault(x => x.Name == message.Body.Split(' ')[0][_configuration.Prefix.Length..]);
if (command == null) {
await room.SendMessageEventAsync("m.room.message",
- new RoomMessageEventData() {
+ new RoomMessageEventData {
MessageType = "m.text",
Body = "Command not found!"
});
return;
}
- var ctx = new CommandContext() {
+
+ var ctx = new CommandContext {
Room = room,
MessageEvent = @event
};
@@ -95,7 +95,7 @@ public class MRUBot : IHostedService {
}
else {
await room.SendMessageEventAsync("m.room.message",
- new RoomMessageEventData() {
+ new RoomMessageEventData {
MessageType = "m.text",
Body = "You do not have permission to run this command!"
});
@@ -108,7 +108,8 @@ public class MRUBot : IHostedService {
/// <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) {
+ public Task StopAsync(CancellationToken cancellationToken) {
_logger.LogInformation("Shutting down bot!");
+ return Task.CompletedTask;
}
}
diff --git a/LibMatrix.ExampleBot/Program.cs b/LibMatrix.ExampleBot/Program.cs
index 93a5f27..63610b4 100644
--- a/LibMatrix.ExampleBot/Program.cs
+++ b/LibMatrix.ExampleBot/Program.cs
@@ -11,7 +11,7 @@ Console.WriteLine("Hello, World!");
var host = Host.CreateDefaultBuilder(args).ConfigureServices((_, services) => {
services.AddScoped<TieredStorageService>(x =>
- new(
+ new TieredStorageService(
cacheStorageProvider: new FileStorageProvider("bot_data/cache/"),
dataStorageProvider: new FileStorageProvider("bot_data/data/")
)
|