Finished routing with automatic help text generation

Changelog: added
This commit is contained in:
Michael Chen 2022-01-16 15:58:35 +01:00
parent ede4efa4e3
commit 9406aaa050
No known key found for this signature in database
GPG Key ID: 1CBC7AA5671437BB
5 changed files with 214 additions and 166 deletions

View File

@ -0,0 +1,43 @@
namespace MinecraftDiscordBot;
public class ChunkWaiter<T> : IChunkWaiter {
public int ID { get; }
private readonly CancellationToken _ct;
public ChunkWaiter(int id, Func<string, T> resultParser, CancellationToken ct) {
ID = id;
this.resultParser = resultParser;
_ct = ct;
}
private readonly TaskCompletionSource<T> tcs = new();
private readonly Func<string, T> resultParser;
public Task<T> Task => tcs.Task.WaitAsync(_ct);
public bool Finished { get; private set; } = false;
public bool IsCancellationRequested => _ct.IsCancellationRequested;
private string?[]? _chunks = null;
private int _receivedChunks = 0;
private bool _success = true;
private readonly object _syncRoot = new();
public void AddChunk(int chunkId, int totalChunks, string value) {
lock (_syncRoot) {
if (_chunks is null) _chunks = new string[totalChunks];
else if (_chunks.Length != totalChunks) {
Program.LogErrorAsync(Program.WebSocketSource, new InvalidOperationException("Different numbers of chunks in same message ID!"));
return;
}
ref string? chunk = ref _chunks[chunkId - 1]; // Lua 1-indexed
if (chunk is not null) {
Program.LogErrorAsync(Program.WebSocketSource, new InvalidOperationException($"Chunk with ID {chunkId} was already received!"));
return;
}
chunk = value;
}
if (++_receivedChunks == totalChunks) FinalizeResult(_chunks);
}
private void FinalizeResult(string?[] _chunks) {
var resultString = string.Concat(_chunks);
if (_success) tcs.SetResult(resultParser(resultString));
else tcs.SetException(new ReplyException(resultString));
Finished = true;
}
public void SetUnsuccessful() => _success = false;
}

View File

@ -1,30 +1,47 @@
using Discord;
using Discord.WebSocket;
using System.Reflection;
using System.Text;
namespace MinecraftDiscordBot;
public abstract class CommandRouter<T> : ICommandHandler<T> {
private readonly Dictionary<string, HandleCommandDelegate<T>> _handlers = new();
public abstract class CommandRouter : ICommandHandler<ResponseType> {
private readonly Dictionary<string, HandlerStruct> _handlers = new();
public abstract string HelpTextPrefix { get; }
public CommandRouter() {
foreach (var method in GetType().GetMethods())
if (GetHandlerAttribute(method) is CommandHandlerAttribute handler)
if (GetHandlerAttribute(method) is CommandHandlerAttribute attribute)
try {
_handlers.Add(handler.CommandName, method.CreateDelegate<HandleCommandDelegate<T>>(this));
_handlers.Add(attribute.CommandName, new(method.CreateDelegate<HandleCommandDelegate<ResponseType>>(this), attribute));
} catch (Exception) {
Program.LogWarning("CommandRouter", $"Could not add delegate for method {handler.CommandName} in function {method.ReturnType} {method.Name}(...)!");
Program.LogWarning("CommandRouter", $"Could not add delegate for method {attribute.CommandName} in function {method.ReturnType} {method.Name}(...)!");
throw;
}
}
[CommandHandler("help", HelpText = "Show this help information!")]
public virtual Task<ResponseType> GetHelpText(SocketUserMessage message, string[] parameters, CancellationToken ct)
=> Task.FromResult(ResponseType.AsString(GenerateHelp()));
private static CommandHandlerAttribute? GetHandlerAttribute(MethodInfo method)
=> method.GetCustomAttributes(typeof(CommandHandlerAttribute), true).OfType<CommandHandlerAttribute>().FirstOrDefault();
public abstract Task<T> RootAnswer(SocketUserMessage message, CancellationToken ct);
public abstract Task<T> FallbackHandler(SocketUserMessage message, string method, string[] parameters, CancellationToken ct);
public Task<T> HandleCommand(SocketUserMessage message, string[] parameters, CancellationToken ct)
public abstract Task<ResponseType> RootAnswer(SocketUserMessage message, CancellationToken ct);
public abstract Task<ResponseType> FallbackHandler(SocketUserMessage message, string method, string[] parameters, CancellationToken ct);
public Task<ResponseType> HandleCommand(SocketUserMessage message, string[] parameters, CancellationToken ct)
=> parameters is { Length: 0 }
? RootAnswer(message, ct)
: _handlers.TryGetValue(parameters[0], out var handler)
? handler(message, parameters[1..], ct)
? handler.Delegate(message, parameters[1..], ct)
: FallbackHandler(message, parameters[0], parameters[1..], ct);
private string GenerateHelp() {
var sb = new StringBuilder();
sb.Append("Command usage:");
foreach (var (name, handler) in _handlers) {
sb.Append($"\n{HelpTextPrefix}{name}");
if (handler.Attribute.HelpText is string help)
sb.Append($": {help}");
}
return sb.ToString();
}
}
public record struct HandlerStruct(HandleCommandDelegate<ResponseType> Delegate, CommandHandlerAttribute Attribute);

View File

@ -15,13 +15,111 @@ public delegate Task HandleCommandDelegate(SocketUserMessage message, string[] p
public sealed class CommandHandlerAttribute : Attribute {
public CommandHandlerAttribute(string commandName) => CommandName = commandName;
public string CommandName { get; }
public string? HelpText { get; init; }
}
public class ConnectedComputer : CommandRouter<ResponseType> {
public class RefinedStorageService : CommandRouter {
private readonly ITaskWaitSource _taskSource;
public override string HelpTextPrefix => "!rs ";
public RefinedStorageService(ITaskWaitSource taskSource) : base() => _taskSource = taskSource;
public override Task<ResponseType> FallbackHandler(SocketUserMessage message, string method, string[] parameters, CancellationToken ct)
=> Task.FromResult(ResponseType.AsString($"The RS system has no command '{method}'!"));
public override Task<ResponseType> RootAnswer(SocketUserMessage message, CancellationToken ct)
=> Task.FromResult(ResponseType.AsString("The RS system is online!"));
private async Task<T> Method<T>(string methodName, Func<string, T> parser, CancellationToken ct) {
var waiter = _taskSource.GetWaiter(parser, ct);
await _taskSource.Send(new RequestMessage(waiter.ID, methodName));
return await waiter.Task;
}
private const string CmdEnergyUsage = "energyusage";
private const string CmdEnergyStorage = "energystorage";
private const string CmdListItems = "listitems";
private const string CmdItemName = "itemname";
private const string CmdListFluids = "listfluids";
public async Task<int> GetEnergyUsageAsync(CancellationToken ct) => await Method(CmdEnergyUsage, int.Parse, ct);
public async Task<int> GetEnergyStorageAsync(CancellationToken ct) => await Method(CmdEnergyStorage, int.Parse, ct);
public async Task<IEnumerable<Item>> ListItemsAsync(CancellationToken ct) => await Method(CmdListItems, ConnectedComputer.Deserialize<IEnumerable<Item>>(), ct);
public async Task<IEnumerable<Fluid>> ListFluidsAsync(CancellationToken ct) => await Method(CmdListFluids, ConnectedComputer.Deserialize<IEnumerable<Fluid>>(), ct);
private Task<IEnumerable<Item>> FilterItems(SocketUserMessage message, IEnumerable<string> filters, CancellationToken ct)
=> FilterItems(message, filters.Select(ItemFilter.Parse), ct);
private async Task<IEnumerable<Item>> FilterItems(SocketUserMessage message, IEnumerable<ItemFilter> filters, CancellationToken ct) {
var items = Items?.ToList().AsEnumerable();
if (items is null) items = (await RefreshItemList(ct)).ToList();
foreach (var filter in filters)
items = items.Where(filter.MatchItem);
return items.ToList();
}
private async Task<List<Item>> RefreshItemList(CancellationToken ct) {
var response = await ListItemsAsync(ct);
lock (_itemLock) {
Items = response.OrderByDescending(i => i.Amount).ToList();
return Items;
}
}
private List<Item>? Items;
private readonly object _itemLock = new();
[CommandHandler(CmdEnergyStorage, HelpText = "Get the amount of energy stored in the RS system.")]
public async Task<ResponseType> HandleEnergyStorage(SocketUserMessage message, string[] parameters, CancellationToken ct)
=> ResponseType.AsString($"Refined Storage system stores {await GetEnergyStorageAsync(ct)} RF/t");
[CommandHandler(CmdEnergyUsage, HelpText = "Get the amount of energy used by the RS system.")]
public async Task<ResponseType> HandleEnergyUsage(SocketUserMessage message, string[] parameters, CancellationToken ct)
=> ResponseType.AsString($"Refined Storage system currently uses {await GetEnergyUsageAsync(ct)} RF/t");
[CommandHandler(CmdItemName, HelpText = "Filter items by name.")]
public async Task<ResponseType> HandleItemName(SocketUserMessage message, string[] parameters, CancellationToken ct) {
if (parameters.Length < 2) return ResponseType.AsString($"Usage: {CmdItemName} filters...");
else {
var items = await FilterItems(message, parameters[1..], ct);
var sb = new StringBuilder();
sb.AppendLine("Did you mean:");
sb.AppendJoin("\n", items.Select(i => i.ToString()));
return ResponseType.AsString(sb.ToString());
}
}
[CommandHandler(CmdListFluids, HelpText = "Gets a list of fluids that are currently stored in the RS system.")]
public async Task<ResponseType> HandleFluidListing(SocketUserMessage message, string[] parameters, CancellationToken ct) {
var sb = new StringBuilder();
sb.Append("The Refined Storage system stores those fluids:");
var fluids = await ListFluidsAsync(ct);
foreach (var fluid in fluids.OrderByDescending(i => i.Amount))
if (fluid.Amount > 10000) sb.AppendFormat("\n{0:n2} B of {1}", fluid.Amount / 1000.0f, fluid.DisplayName);
else sb.AppendFormat("\n{0:n0} mB of {1}", fluid.Amount, fluid.DisplayName);
return ResponseType.AsString(sb.ToString());
}
[CommandHandler(CmdListItems, HelpText = "Gets a list of items that are currently stored in the RS system.")]
public async Task<ResponseType> HandleItemListing(SocketUserMessage message, string[] parameters, CancellationToken ct) {
var sb = new StringBuilder();
sb.Append("The Refined Storage system currently stores these items:");
var items = await RefreshItemList(ct);
lock (_itemLock) {
int taken = 0;
foreach (var item in items) {
if (sb.Length > 500) break;
sb.AppendFormat("\n{0:n0}x {1}", item.Amount, item.DisplayName);
taken++;
}
if (items.Count > taken) sb.AppendFormat("\nand {0} more items.", items.Skip(taken).Sum(i => i.Amount));
}
return ResponseType.AsString(sb.ToString());
}
}
public class ConnectedComputer : CommandRouter, ITaskWaitSource {
protected readonly IWebSocketConnection _socket;
public override string HelpTextPrefix => "!";
public ConnectedComputer(IWebSocketConnection socket) : base() {
socket.OnMessage = OnMessage;
_socket = socket;
_rs = new RefinedStorageService(this);
}
private void OnMessage(string message) {
@ -39,64 +137,15 @@ public class ConnectedComputer : CommandRouter<ResponseType> {
lock (_syncRoot)
_waits.Remove(waiter.ID);
}
public Task Send(string message) => _socket.Send(message);
protected Task Send(Message message) => Send(JsonConvert.SerializeObject(message));
public Task Send(Message message) => Send(JsonConvert.SerializeObject(message));
private readonly object _syncRoot = new();
private readonly Dictionary<int, IChunkWaiter> _waits = new();
private readonly Random _rnd = new();
public IWebSocketConnectionInfo ConnectionInfo => _socket.ConnectionInfo;
protected interface IChunkWaiter {
bool Finished { get; }
int ID { get; }
bool IsCancellationRequested { get; }
void AddChunk(int chunkId, int totalChunks, string value);
void SetUnsuccessful();
}
protected class ChunkWaiter<T> : IChunkWaiter {
public int ID { get; }
private readonly CancellationToken _ct;
public ChunkWaiter(int id, Func<string, T> resultParser, CancellationToken ct) {
ID = id;
this.resultParser = resultParser;
_ct = ct;
}
private readonly TaskCompletionSource<T> tcs = new();
private readonly Func<string, T> resultParser;
public Task<T> Task => tcs.Task.WaitAsync(_ct);
public bool Finished { get; private set; } = false;
public bool IsCancellationRequested => _ct.IsCancellationRequested;
private string?[]? _chunks = null;
private int _receivedChunks = 0;
private bool _success = true;
private readonly object _syncRoot = new();
public void AddChunk(int chunkId, int totalChunks, string value) {
lock (_syncRoot) {
if (_chunks is null) _chunks = new string[totalChunks];
else if (_chunks.Length != totalChunks) {
Program.LogErrorAsync(Program.WebSocketSource, new InvalidOperationException("Different numbers of chunks in same message ID!"));
return;
}
ref string? chunk = ref _chunks[chunkId - 1]; // Lua 1-indexed
if (chunk is not null) {
Program.LogErrorAsync(Program.WebSocketSource, new InvalidOperationException($"Chunk with ID {chunkId} was already received!"));
return;
}
chunk = value;
}
if (++_receivedChunks == totalChunks) FinalizeResult(_chunks);
}
private void FinalizeResult(string?[] _chunks) {
var resultString = string.Concat(_chunks);
if (_success) tcs.SetResult(resultParser(resultString));
else tcs.SetException(new ReplyException(resultString));
Finished = true;
}
public void SetUnsuccessful() => _success = false;
}
protected int GetFreeId() {
private int GetFreeId() {
var attempts = 0;
while (true) {
var id = _rnd.Next();
@ -106,7 +155,7 @@ public class ConnectedComputer : CommandRouter<ResponseType> {
}
}
protected ChunkWaiter<T> GetWaiter<T>(Func<string, T> resultParser, CancellationToken ct) {
public ChunkWaiter<T> GetWaiter<T>(Func<string, T> resultParser, CancellationToken ct) {
ChunkWaiter<T> waiter;
lock (_syncRoot) {
waiter = new ChunkWaiter<T>(GetFreeId(), resultParser, ct);
@ -115,112 +164,24 @@ public class ConnectedComputer : CommandRouter<ResponseType> {
return waiter;
}
protected static Func<string, T> Deserialize<T>() => msg
=> JsonConvert.DeserializeObject<T>(msg) ?? throw new InvalidProgramException("Empty response!");
public const string Role = "rs";
private const string CmdEnergyUsage = "energyusage";
private const string CmdEnergyStorage = "energystorage";
private const string CmdListItems = "listitems";
private const string CmdItemName = "itemname";
private const string CmdListFluids = "listfluids";
public async Task<int> GetEnergyUsageAsync(CancellationToken ct) {
var waiter = GetWaiter(int.Parse, ct);
await Send(new RequestMessage(waiter.ID, CmdEnergyUsage));
return await waiter.Task;
}
public async Task<int> GetEnergyStorageAsync(CancellationToken ct) {
var waiter = GetWaiter(int.Parse, ct);
await Send(new RequestMessage(waiter.ID, CmdEnergyStorage));
return await waiter.Task;
}
public async Task<IEnumerable<Item>> ListItemsAsync(CancellationToken ct) {
var waiter = GetWaiter(Deserialize<IEnumerable<Item>>(), ct);
await Send(new RequestMessage(waiter.ID, CmdListItems));
return await waiter.Task;
}
public async Task<IEnumerable<Fluid>> ListFluidsAsync(CancellationToken ct) {
var waiter = GetWaiter(Deserialize<IEnumerable<Fluid>>(), ct);
await Send(new RequestMessage(waiter.ID, CmdListFluids));
return await waiter.Task;
}
[CommandHandler(CmdEnergyStorage)]
public async Task<ResponseType> HandleEnergyStorage(SocketUserMessage message, string[] parameters, CancellationToken ct)
=> ResponseType.AsString($"Refined Storage system stores {await GetEnergyStorageAsync(ct)} RF/t");
[CommandHandler(CmdEnergyUsage)]
public async Task<ResponseType> HandleEnergyUsage(SocketUserMessage message, string[] parameters, CancellationToken ct)
=> ResponseType.AsString($"Refined Storage system currently uses {await GetEnergyUsageAsync(ct)} RF/t");
[CommandHandler(CmdItemName)]
public async Task<ResponseType> HandleItemName(SocketUserMessage message, string[] parameters, CancellationToken ct) {
if (parameters.Length < 2) return ResponseType.AsString($"Usage: {CmdItemName} filters...");
else {
var items = await FilterItems(message, parameters[1..], ct);
var sb = new StringBuilder();
sb.AppendLine("Did you mean:");
sb.AppendJoin("\n", items.Select(i => i.ToString()));
return ResponseType.AsString(sb.ToString());
}
}
private Task<IEnumerable<Item>> FilterItems(SocketUserMessage message, IEnumerable<string> filters, CancellationToken ct)
=> FilterItems(message, filters.Select(ItemFilter.Parse), ct);
private async Task<IEnumerable<Item>> FilterItems(SocketUserMessage message, IEnumerable<ItemFilter> filters, CancellationToken ct) {
var items = Items?.ToList().AsEnumerable();
if (items is null) items = (await RefreshItemList(ct)).ToList();
foreach (var filter in filters)
items = items.Where(filter.MatchItem);
return items.ToList();
}
[CommandHandler(CmdListFluids)]
public async Task<ResponseType> HandleFluidListing(SocketUserMessage message, string[] parameters, CancellationToken ct) {
var sb = new StringBuilder();
sb.Append("The Refined Storage system stores those fluids:");
var fluids = await ListFluidsAsync(ct);
foreach (var fluid in fluids.OrderByDescending(i => i.Amount))
if (fluid.Amount > 10000) sb.AppendFormat("\n{0:n2} B of {1}", fluid.Amount / 1000.0f, fluid.DisplayName);
else sb.AppendFormat("\n{0:n0} mB of {1}", fluid.Amount, fluid.DisplayName);
return ResponseType.AsString(sb.ToString());
}
private List<Item>? Items;
private readonly object _itemLock = new();
[CommandHandler(CmdListItems)]
public async Task<ResponseType> HandleItemListing(SocketUserMessage message, string[] parameters, CancellationToken ct) {
var sb = new StringBuilder();
sb.Append("The Refined Storage system currently stores these items:");
var items = await RefreshItemList(ct);
lock (_itemLock) {
int taken = 0;
foreach (var item in items) {
if (sb.Length > 500) break;
sb.AppendFormat("\n{0:n0}x {1}", item.Amount, item.DisplayName);
taken++;
}
if (items.Count > taken) sb.AppendFormat("\nand {0} more items.", items.Skip(taken).Sum(i => i.Amount));
}
return ResponseType.AsString(sb.ToString());
}
private async Task<List<Item>> RefreshItemList(CancellationToken ct) {
var response = await ListItemsAsync(ct);
lock (_itemLock) {
Items = response.OrderByDescending(i => i.Amount).ToList();
return Items;
}
}
private readonly ICommandHandler<ResponseType> _rs;
[CommandHandler("rs", HelpText ="Provides some commands for interacting with the Refined Storage system.")]
public Task<ResponseType> RefinedStorageHandler(SocketUserMessage message, string[] parameters, CancellationToken ct)
=> _rs.HandleCommand(message, parameters, ct);
public static Func<string, T> Deserialize<T>() => msg
=> JsonConvert.DeserializeObject<T>(msg) ?? throw new InvalidProgramException("Empty response!");
public override Task<ResponseType> RootAnswer(SocketUserMessage message, CancellationToken ct)
=> Task.FromResult(ResponseType.AsString("The Minecraft server is connected!"));
public override Task<ResponseType> FallbackHandler(SocketUserMessage message, string method, string[] parameters, CancellationToken ct)
=> Task.FromResult(ResponseType.AsString($"What the fuck do you mean by '{method}'?"));
}
public interface ITaskWaitSource {
ChunkWaiter<T> GetWaiter<T>(Func<string, T> resultParser, CancellationToken ct);
Task Send(Message requestMessage);
}
[Serializable]
public class ReplyException : Exception {
public ReplyException() { }

View File

@ -0,0 +1,9 @@
namespace MinecraftDiscordBot;
public interface IChunkWaiter {
bool Finished { get; }
int ID { get; }
bool IsCancellationRequested { get; }
void AddChunk(int chunkId, int totalChunks, string value);
void SetUnsuccessful();
}

View File

@ -16,6 +16,7 @@ public class Program : IDisposable, ICommandHandler<ResponseType> {
public const string WebSocketSource = "WebSocket";
public const string BotSource = "Bot";
private static readonly object LogLock = new();
public const int ChoiceTimeout = 20 * 1000;
private readonly DiscordSocketClient _client = new(new() {
LogLevel = LogSeverity.Verbose,
GatewayIntents = GatewayIntents.AllUnprivileged & ~(GatewayIntents.GuildScheduledEvents | GatewayIntents.GuildInvites)
@ -47,6 +48,7 @@ public class Program : IDisposable, ICommandHandler<ResponseType> {
_config = config;
_client.Log += LogAsync;
_client.MessageReceived += (msg) => DiscordMessageReceived(msg);
_client.ReactionAdded += DiscordReactionAdded;
_wssv = new WebSocketServer($"ws://0.0.0.0:{config.Port}") {
RestartAfterListenError = true
};
@ -157,20 +159,36 @@ public class Program : IDisposable, ICommandHandler<ResponseType> {
// TODO: Relay Message to Chat Receiver
}
private static Task SendResponse(SocketUserMessage message, ResponseType response) => response switch {
private Task SendResponse(SocketUserMessage message, ResponseType response) => response switch {
ResponseType.IChoiceResponse res => HandleChoice(message, res),
ResponseType.StringResponse res => message.ReplyAsync(res.Message),
_ => message.ReplyAsync($"Whoops, someone forgot to implement '{response.GetType()}' responses?"),
};
private static async Task HandleChoice(SocketUserMessage message, ResponseType.IChoiceResponse res) {
private readonly ConcurrentDictionary<ulong, ResponseType.IChoiceResponse> _choiceWait = new();
private async Task DiscordReactionAdded(Cacheable<IUserMessage, ulong> message, Cacheable<IMessageChannel, ulong> channel, SocketReaction reaction) {
var msgObject = await message.GetOrDownloadAsync();
if (reaction.UserId == _client.CurrentUser.Id) return;
if (!_choiceWait.TryRemove(message.Id, out var choice)) { await LogInfoAsync(BotSource, "Reaction was added to message without choice object!"); return; }
await msgObject.DeleteAsync();
await LogInfoAsync(BotSource, $"Reaction {reaction.Emote.Name} was added to the choice by {reaction.UserId}!");
}
private async Task HandleChoice(SocketUserMessage message, ResponseType.IChoiceResponse res) {
var reply = await message.ReplyAsync($"{res.Query}\n{string.Join("\n", res.Options)}");
var reactions = new Emoji[] { new("0⃣"), new("1⃣"), new("2⃣"), new("3⃣"), new("4⃣"), new("5⃣"), new("6⃣"), new("7⃣"), new("8⃣"), new("9⃣") };
_choiceWait[reply.Id] = res;
var reactions = new Emoji[] { new("0⃣")/*, new("1⃣"), new("2⃣"), new("3⃣"), new("4⃣"), new("5⃣"), new("6⃣"), new("7⃣"), new("8⃣"), new("9⃣")*/ };
await reply.AddReactionsAsync(reactions);
_ = Task.Run(async () => {
await Task.Delay(ChoiceTimeout);
_ = _choiceWait.TryRemove(message.Id, out _);
await reply.ModifyAsync(i => i.Content = "You did not choose in time!");
await reply.RemoveAllReactionsAsync();
});
}
public async Task<ResponseType> HandleCommand(SocketUserMessage message, string[] parameters, CancellationToken ct) {
return ResponseType.FromChoice("Select an emoji:", new[] { "One", "Two", "Nine", "420" }, (choice) => message.ReplyAsync($"You chose: {choice}"));
if (Computer is ICommandHandler<ResponseType> handler)
try {
return await handler.HandleCommand(message, parameters, ct);