From aa0b06be5b2f9afa64b8f2a38f59b4f5cc570ee8 Mon Sep 17 00:00:00 2001 From: Madpeter Date: Sat, 24 Oct 2020 19:33:18 +0100 Subject: [PATCH] adds support for custom commands (see readme) --- BSBshared/BotTypes/BasicBot.cs | 87 +++++++++++++++++++++++++++++++- CLI/CliDocker.cs | 2 +- CLI/CliExitOnLogout.cs | 10 ++-- CLI/CliHardware.cs | 2 +- Core/bottypes/CommandsBot.cs | 40 ++++++++++++++- Core/bottypes/Discord/Inbound.cs | 18 +++++-- 6 files changed, 149 insertions(+), 10 deletions(-) diff --git a/BSBshared/BotTypes/BasicBot.cs b/BSBshared/BotTypes/BasicBot.cs index d26f789..07a37f6 100644 --- a/BSBshared/BotTypes/BasicBot.cs +++ b/BSBshared/BotTypes/BasicBot.cs @@ -1,10 +1,11 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; +using BetterSecondBotShared.IO; using BetterSecondBotShared.Json; using BetterSecondBotShared.logs; using BetterSecondBotShared.Static; +using Newtonsoft.Json; using OpenMetaverse; namespace BetterSecondBotShared.bottypes @@ -28,6 +29,85 @@ namespace BetterSecondBotShared.bottypes } } + protected Dictionary custom_commands = new Dictionary(); + protected bool loaded_custom_commands = false; + protected void LoadCustomCommands() + { + if (running_in_docker == true) + { + // search env for cmd_ + foreach (System.Collections.DictionaryEntry env in Environment.GetEnvironmentVariables()) + { + string name = env.Key.ToString(); + if (name.StartsWith("cmd_") == true) + { + string[] steps = env.Value.ToString().Split(new string[] { "{-}" }, StringSplitOptions.None); + name = name.Replace("cmd_", ""); + name = name.ToLowerInvariant(); + custom_commands.Add(name, steps); + } + } + } + else + { + // search for file called "commands.cmd" + JsonCommandsfile LoadedCommands = new JsonCommandsfile(); + LoadedCommands.CustomCommands = new string[] { "sayexample!!!say|||Hello{-}delay|||2500{-}say|||Bye" }; + string CommandsFile = "custom_commands.json"; + SimpleIO io = new SimpleIO(); + if (SimpleIO.FileType(CommandsFile, "json") == true) + { + if (io.Exists(CommandsFile) == true) + { + string json = io.ReadFile(CommandsFile); + if (json.Length > 0) + { + try + { + LoadedCommands = JsonConvert.DeserializeObject(json); + foreach(string loaded in LoadedCommands.CustomCommands) + { + string[] bits = loaded.Split(new string[] { "!!!" }, StringSplitOptions.None); + if (bits.Length == 2) + { + string[] steps = bits[1].Split(new string[] { "{-}" }, StringSplitOptions.None); + custom_commands.Add(bits[0].ToLowerInvariant(), steps); + } + } + } + catch + { + io.makeOld(CommandsFile); + io.WriteJsonCommands(LoadedCommands, CommandsFile); + } + } + else + { + io.WriteJsonCommands(LoadedCommands, CommandsFile); + } + } + else + { + io.WriteJsonCommands(LoadedCommands, CommandsFile); + } + } + else + { + io.WriteJsonCommands(LoadedCommands, CommandsFile); + } + } + if(custom_commands.Count > 0) + { + Info("Custom commands: " + custom_commands.Count.ToString()); + } + } + + protected bool running_in_docker = false; + public void AsDocker() + { + running_in_docker = true; + } + protected bool killMe; protected JsonConfig myconfig; protected string version = "NotSet V1.0.0.0"; @@ -162,6 +242,11 @@ namespace BetterSecondBotShared.bottypes protected string BasicBot_laststatus = ""; public virtual string GetStatus() { + if (loaded_custom_commands == false) + { + loaded_custom_commands = true; + LoadCustomCommands(); + } string reply = "Info: No client"; if (Client != null) { diff --git a/CLI/CliDocker.cs b/CLI/CliDocker.cs index 18f6fe5..bede28e 100644 --- a/CLI/CliDocker.cs +++ b/CLI/CliDocker.cs @@ -17,7 +17,7 @@ namespace BetterSecondBot LogFormater.Status("-> Getting config from Docker"); JsonConfig Config = MakeJsonConfig.FromENV(); Config = MakeJsonConfig.Http_config_check(Config); - new CliExitOnLogout(Config); + new CliExitOnLogout(Config,true); } } } diff --git a/CLI/CliExitOnLogout.cs b/CLI/CliExitOnLogout.cs index f6a6c66..2aa54ee 100644 --- a/CLI/CliExitOnLogout.cs +++ b/CLI/CliExitOnLogout.cs @@ -14,18 +14,22 @@ namespace BetterSecondBot { public class CliExitOnLogout { - public CliExitOnLogout(JsonConfig Config) + public CliExitOnLogout(JsonConfig Config, bool as_docker) { LogFormater.Status("Mode: Cli [Basic]"); if (helpers.botRequired(Config) == true) { SecondBot Bot = new SecondBot(); Bot.Setup(Config, AssemblyInfo.GetGitHash()); + if(as_docker == true) + { + Bot.AsDocker(); + } Bot.Start(); if (Config.Http_Enable == true) { SecondBotHttpServer my_http_server = new SecondBotHttpServer(); - my_http_server.StartHttpServer(Bot,Config); + my_http_server.StartHttpServer(Bot, Config); } while (Bot.KillMe == false) { @@ -33,7 +37,7 @@ namespace BetterSecondBot if (NewStatusMessage != Bot.LastStatusMessage) { NewStatusMessage = NewStatusMessage.Trim(); - if (NewStatusMessage.Replace(" ","") != "") + if (NewStatusMessage.Replace(" ", "") != "") { Bot.LastStatusMessage = NewStatusMessage; Bot.Log2File(LogFormater.Status(Bot.LastStatusMessage, false), ConsoleLogLogLevel.Status); diff --git a/CLI/CliHardware.cs b/CLI/CliHardware.cs index 84dbd0f..0f88b6f 100644 --- a/CLI/CliHardware.cs +++ b/CLI/CliHardware.cs @@ -77,7 +77,7 @@ namespace BetterSecondBot if(ok_to_try_start == true) { Config = MakeJsonConfig.Http_config_check(Config); - new CliExitOnLogout(Config); + new CliExitOnLogout(Config,false); } } } diff --git a/Core/bottypes/CommandsBot.cs b/Core/bottypes/CommandsBot.cs index 8113869..886e79f 100644 --- a/Core/bottypes/CommandsBot.cs +++ b/Core/bottypes/CommandsBot.cs @@ -6,6 +6,7 @@ using OpenMetaverse; using OpenMetaverse.Assets; using BetterSecondBotShared.Static; using BetterSecondBotShared.logs; +using System.Threading; namespace BSB.bottypes { @@ -15,6 +16,7 @@ namespace BSB.bottypes { CommandsInterface = new Commands.CoreCommandsInterface(this); } + protected List CommandHistory = new List(); protected int commandid = 1; public virtual string CommandHistoryAdd(string command, string arg, bool status,string infomessage) @@ -131,13 +133,49 @@ namespace BSB.bottypes } } + protected void custom_commands_loop(string command,string arg,UUID caller) + { + List customargs = arg.Split(new[] { "~#~" }, StringSplitOptions.RemoveEmptyEntries).ToList(); + while(customargs.Count < 5) + { + customargs.Add("notset"); + } + foreach (string A in custom_commands[command]) + { + List command_args_split = A.Split(new[] { "|||" }, StringSplitOptions.RemoveEmptyEntries).ToList(); + int loop = 1; + string args_passed = ""; + if (command_args_split.Count() == 2) + { + args_passed = command_args_split[1]; + while (loop <= 5) + { + args_passed = args_passed.Replace("[C_ARG_"+loop.ToString()+"]", customargs[loop-1]); + loop++; + } + } + CommandsInterface.Call(command_args_split[0], args_passed, caller, "~#~"); + } + } + protected virtual void CallCommandLib(string command, string arg) + { + CallCommandLib(command, arg, UUID.Zero); + } protected virtual void CallCommandLib(string command, string arg, UUID caller) { CallCommandLib(command, arg, caller, "~#~"); } protected virtual void CallCommandLib(string command, string arg, UUID caller,string signed_with) { - CommandsInterface.Call(command, arg, caller, signed_with); + if(custom_commands.ContainsKey(command) == false) + { + CommandsInterface.Call(command, arg, caller, signed_with); + } + else + { + Thread t = new Thread(() => custom_commands_loop(command, arg, caller)); + t.Start(); + } } protected override void AfterBotLoginHandler() diff --git a/Core/bottypes/Discord/Inbound.cs b/Core/bottypes/Discord/Inbound.cs index 1d44248..75955fc 100644 --- a/Core/bottypes/Discord/Inbound.cs +++ b/Core/bottypes/Discord/Inbound.cs @@ -5,6 +5,7 @@ using System; using System.Text; using System.Threading.Tasks; using System.Linq; +using System.Threading; namespace BSB.bottypes { @@ -90,16 +91,27 @@ namespace BSB.bottypes { string[] bits = message.Content.Split("|||"); string command = bits[0].ToLowerInvariant(); - if (CommandsInterface.GetCommandsList().Contains(command) == true) + bool known_command = CommandsInterface.GetCommandsList().Contains(command); + if(known_command == false) + { + known_command = custom_commands.ContainsKey(command); + } + if (known_command == true) { bool process_status = false; - if (bits.Length == 2) + if(bits.Length == 1) + { + bits = new string[] { bits[0], "" }; + } + if (custom_commands.ContainsKey(command) == false) { process_status = CommandsInterface.Call(command, bits[1], UUID.Zero); } else { - process_status = CommandsInterface.Call(command); + process_status = true; + Thread t = new Thread(() => custom_commands_loop(command, bits[1], UUID.Zero)); + t.Start(); } if (process_status == true) {