[ChatBoxExtensions] Updated for the latest API.

This commit is contained in:
kafeijao 2023-04-26 19:38:28 +01:00
parent 62eb612d73
commit 37ad5a8f9c
No known key found for this signature in database
GPG key ID: E99978723E454B4C
9 changed files with 88 additions and 40 deletions

View file

@ -0,0 +1,46 @@
namespace NAK.Melons.ChatBoxExtensions.Integrations;
public static class Commands {
private const string Character = "/";
private static readonly List<Command> CommandList = new();
internal static void InitializeCommandHandlers() {
Kafe.ChatBox.API.OnMessageSent += (source, msg, notification, displayMsg) => HandleSentCommand(msg, notification, displayMsg);
Kafe.ChatBox.API.OnMessageReceived += (source, sender, msg, notification, displayMsg) => HandleReceivedCommand(sender, msg, notification, displayMsg);
}
internal static void RegisterCommand(string prefix, Action<string, bool, bool> onCommandSent = null, Action<string, string, bool, bool> onCommandReceived = null) {
var cmd = new Command { Prefix = prefix, OnCommandSent = onCommandSent, OnCommandReceived = onCommandReceived };
CommandList.Add(cmd);
}
internal static void UnregisterCommand(string prefix) {
CommandList.RemoveAll(cmd => cmd.Prefix == prefix);
}
private class Command {
internal string Prefix;
// Command Sent (message)
internal Action<string, bool, bool> OnCommandSent;
// Command Sent (sender guid, message)
internal Action<string, string, bool, bool> OnCommandReceived;
}
private static void HandleSentCommand(string message, bool notification, bool displayMsg) {
if (!message.StartsWith(Character)) return;
foreach (var command in CommandList.Where(command => message.StartsWith(Character + command.Prefix))) {
command.OnCommandSent?.Invoke(message, notification, displayMsg);
}
}
private static void HandleReceivedCommand(string sender, string message, bool notification, bool displayMsg) {
if (!message.StartsWith(Character)) return;
foreach (var command in CommandList.Where(command => message.StartsWith(Character + command.Prefix))) {
command.OnCommandReceived?.Invoke(sender, message, notification, displayMsg);
}
}
}