diff --git a/PluginCore.Events.cs b/PluginCore.Events.cs index f2692ca..a452ef9 100644 --- a/PluginCore.Events.cs +++ b/PluginCore.Events.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Text; using Decal.Adapter; using Decal.Adapter.Wrappers; @@ -111,5 +112,95 @@ private void CharacterFilter_Death(object sender, DeathEventArgs e) Util.LogError(ex); } } + + [BaseEvent("CommandLineText")] + void Core_CommandLineText(object sender, ChatParserInterceptEventArgs e) + { + try + { + string text = e.Text; + + if (text.Length <= 0) + { + return; + } + + // Split into tokens for easier processing + string[] tokens = text.Split(' '); + string command = tokens[0].ToLower(); + + if (command.StartsWith("@towncrier") || + command.StartsWith("@tc") || + command.StartsWith("/towncrier") || + command.StartsWith("/tc")) + { + ProcessCommand(tokens); + } + } + catch (Exception ex) + { + Util.LogError(ex); + } + } + + void ProcessCommand(string[] tokens) + { + if (tokens.Length == 0) + { + return; + } + + // Show help if no args are passed or help is passed + if (tokens.Length < 2 || tokens[1].ToLower() == "help") + { + PrintCommandLineHelp(); + + return; + } + + if (tokens[1].ToLower() == "trigger") + { + if (tokens.Length < 4) + { + PrintCommandLineHelp(); + } + + string name = tokens[2]; + + // Try to find the webhook first by name and warn if not found + List matched = Webhooks.FindAll(webhook => webhook.Name == name); + + if (matched.Count == 0) + { + Util.WriteToChat("Webhook with name '" + name + "' not found."); + + return; + } + + // Slice the array so we can concatenate the message portion + string[] rest = new string[tokens.Length - 3]; + Array.Copy(tokens, 3, rest, 0, tokens.Length - 3); + string message = string.Join(" ", rest); + + if (message.Length == 0) + { + Util.WriteToChat("Can't trigger webhook '" + name + "' with an empty message."); + PrintCommandLineHelp(); + } + + Util.WriteToChat("Triggering webhook '" + name + "' with message '" + message + "'"); + + TriggerWebhook(name, message); + } + else + { + PrintCommandLineHelp(); + } + } + + void PrintCommandLineHelp() + { + Util.WriteToChat("Trigger webhooks via '@towncrier trigger ${webhookname} ${message}'. You can use Variables."); + } } } diff --git a/PluginCore.cs b/PluginCore.cs index 4edf20a..67dc55f 100644 --- a/PluginCore.cs +++ b/PluginCore.cs @@ -300,6 +300,23 @@ private void DisposeAllTimers() } } + private void TriggerWebhook(string name, string message) + { + try + { + List matched = Webhooks.FindAll(webhook => webhook.Name == name); + + foreach (Webhook webhook in matched) + { + webhook.Send(new WebhookMessage(message)); + } + } + catch (Exception ex) + { + Util.LogError(ex); + } + } + private void TriggerWebhooksForEvent(string evt, string eventMessage) { try diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 623d1e7..7591444 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.2.1")] -[assembly: AssemblyFileVersion("0.2.1")] +[assembly: AssemblyVersion("0.2.2")] +[assembly: AssemblyFileVersion("0.2.2")] diff --git a/WebhookMessage.cs b/WebhookMessage.cs index b3642f9..99d7eb0 100644 --- a/WebhookMessage.cs +++ b/WebhookMessage.cs @@ -8,6 +8,12 @@ class WebhookMessage public string MessageFormatString; public string EventMessage; + public WebhookMessage(string message) + { + MessageFormatString = message; + EventMessage = ""; + } + public WebhookMessage(string message, string eventMessage) { MessageFormatString = message;