adds support for custom commands (see readme)
This commit is contained in:
parent
f358c5bc2c
commit
aa0b06be5b
6 changed files with 149 additions and 10 deletions
|
|
@ -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<string, string[]> custom_commands = new Dictionary<string, string[]>();
|
||||
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<JsonCommandsfile>(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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<string> CommandHistory = new List<string>();
|
||||
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<string> customargs = arg.Split(new[] { "~#~" }, StringSplitOptions.RemoveEmptyEntries).ToList();
|
||||
while(customargs.Count < 5)
|
||||
{
|
||||
customargs.Add("notset");
|
||||
}
|
||||
foreach (string A in custom_commands[command])
|
||||
{
|
||||
List<string> 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()
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue