command history update

This commit is contained in:
Madpeter 2020-04-27 02:27:09 +01:00
parent 0a1a065092
commit 913ab57565
3 changed files with 56 additions and 38 deletions

View file

@ -19,9 +19,10 @@ namespace BSB.Commands
Command = set_cmd; Command = set_cmd;
args = set_args.ToList(); args = set_args.ToList();
} }
public bool Callnow() public KeyValuePair<bool,string> Callnow()
{ {
return Command.CallFunction(args.ToArray()); bool result = Command.CallFunction(args.ToArray());
return new KeyValuePair<bool, string>(result, Command.GetInfoBlob);
} }
public bool needs_lookup() public bool needs_lookup()
{ {
@ -205,7 +206,7 @@ namespace BSB.Commands
return Call(command, "",UUID.Zero); return Call(command, "",UUID.Zero);
} }
protected bool CallCommand(string command, string arg, UUID caller) protected KeyValuePair<bool,string> CallCommand(string command, string arg, UUID caller)
{ {
command = command.ToLowerInvariant(); command = command.ToLowerInvariant();
CoreCommand cmd = (CoreCommand)GetCommand(command); CoreCommand cmd = (CoreCommand)GetCommand(command);
@ -219,7 +220,6 @@ namespace BSB.Commands
} }
if ((cmd.ArgTypes.Contains("Avatar") == true) || (cmd.ArgTypes.Contains("Smart") == true)) if ((cmd.ArgTypes.Contains("Avatar") == true) || (cmd.ArgTypes.Contains("Smart") == true))
{ {
if (cmd.MinArgs <= args.Length) if (cmd.MinArgs <= args.Length)
{ {
pending_avatar_lookup pending = new pending_avatar_lookup(this, cmd, args); pending_avatar_lookup pending = new pending_avatar_lookup(this, cmd, args);
@ -232,7 +232,7 @@ namespace BSB.Commands
{ {
next_lookup_id = 0; next_lookup_id = 0;
} }
return true; return new KeyValuePair<bool, string>(true,"Lookup underway");
} }
else else
{ {
@ -241,12 +241,13 @@ namespace BSB.Commands
} }
else else
{ {
return false; return new KeyValuePair<bool, string>(false, "Required args count not sent! expected: "+ cmd.MinArgs.ToString()+" but got "+ args.Length.ToString()+ "");
} }
} }
else else
{ {
return cmd.CallFunction(args); bool result = cmd.CallFunction(args);
return new KeyValuePair<bool, string>(result, cmd.GetInfoBlob);
} }
} }
else else
@ -254,16 +255,16 @@ namespace BSB.Commands
if (suppress_warnings.Contains(command) == false) if (suppress_warnings.Contains(command) == false)
{ {
suppress_warnings.Add(command); suppress_warnings.Add(command);
ConsoleLog.Debug("[CMD/Failed] " + command + " : I have no fucking idea what you are talking about"); return new KeyValuePair<bool, string>(false, "unknown");
} }
return false;
} }
return new KeyValuePair<bool, string>(false, "--");
} }
public bool Call(string command, string arg,UUID caller) public bool Call(string command, string arg,UUID caller)
{ {
bool result = CallCommand(command, arg, caller); KeyValuePair<bool, string> result = CallCommand(command, arg, caller);
bot.CommandHistoryAdd(command, arg, result); bot.CommandHistoryAdd(command, arg, result.Key,result.Value);
return result; return result.Key;
} }
} }
public abstract class CoreCommand : API_interface public abstract class CoreCommand : API_interface
@ -281,11 +282,11 @@ namespace BSB.Commands
} }
public virtual void Callback(string[] args, EventArgs e,bool status) public virtual void Callback(string[] args, EventArgs e,bool status)
{ {
bot.CommandHistoryAdd("Callback finished-> " + this.GetType().Name, String.Join(",", args), status); bot.CommandHistoryAdd("Callback finished-> " + this.GetType().Name, String.Join(",", args), status, GetInfoBlob);
} }
public virtual void Callback(string[] args, EventArgs e) public virtual void Callback(string[] args, EventArgs e)
{ {
bot.CommandHistoryAdd("Callback finished-> " + this.GetType().Name, String.Join(",", args), true); bot.CommandHistoryAdd("Callback finished-> " + this.GetType().Name, String.Join(",", args), true, GetInfoBlob);
} }
public virtual bool CallFunction(string[] args) public virtual bool CallFunction(string[] args)
{ {
@ -314,7 +315,7 @@ namespace BSB.Commands
} }
public bool Failed(string why_failed) public bool Failed(string why_failed)
{ {
ConsoleLog.Warn("[CMD/Failed] " + CommandName + ": " + why_failed); InfoBlob = why_failed;
return false; return false;
} }
} }

View file

@ -11,22 +11,45 @@ namespace BSB.bottypes
{ {
public abstract class CommandsBot : AVstorageBot public abstract class CommandsBot : AVstorageBot
{ {
Dictionary<string, long> CommandHistory = new Dictionary<string, long>(); List<string> CommandHistory = new List<string>();
protected int commandid = 1; protected int commandid = 1;
public virtual void CommandHistoryAdd(string command, string arg, bool status) public virtual string CommandHistoryAdd(string command, string arg, bool status,string infomessage)
{ {
string message = "(" + commandid.ToString() + "): " + status.ToString() + " -> " + command + " [" + arg + "]"; string message = "";
CommandHistory.Add(message, helpers.UnixTimeNow()); if (infomessage != "--")
if(CommandHistory.Count() > myconfig.MaxCommandHistory)
{ {
CommandHistory.Remove(CommandHistory.ElementAt(0).Key); StringBuilder sb = new StringBuilder();
sb.Append(commandid.ToString());
sb.Append(" - ");
sb.Append(command);
sb.Append(" / [" + arg + "]");
if (status == false)
{
sb.Append(" (FAILED) ");
}
else
{
sb.Append(" (OK) ");
}
if (helpers.notempty(infomessage) == true)
{
sb.Append("[Info: ");
sb.Append(infomessage);
sb.Append("]");
}
CommandHistory.Add(sb.ToString());
if (CommandHistory.Count() > myconfig.MaxCommandHistory)
{
CommandHistory.RemoveAt(0);
}
if (myconfig.CommandsToConsole == true)
{
ConsoleLog.Info(sb.ToString());
}
commandid++;
if (commandid >= myconfig.MaxCommandHistory) commandid = 1;
} }
if (myconfig.CommandsToConsole == true) return message;
{
ConsoleLog.Info(message);
}
commandid++;
if (commandid >= myconfig.MaxCommandHistory) commandid = 1;
} }
public string[] GetLastCommands(int amount=10) public string[] GetLastCommands(int amount=10)
{ {
@ -34,7 +57,7 @@ namespace BSB.bottypes
int loop = 0; int loop = 0;
while((loop < CommandHistory.Count) && (loop < amount)) while((loop < CommandHistory.Count) && (loop < amount))
{ {
commands.Add(CommandHistory.Keys.ElementAt(CommandHistory.Count - loop - 1)); commands.Add(CommandHistory.ElementAt(CommandHistory.Count - loop - 1));
loop++; loop++;
} }
return commands.ToArray(); return commands.ToArray();

View file

@ -549,17 +549,11 @@ namespace BSB.bottypes
await DiscordBotChatControler(message, sender_name, sender_uuid, avatar, group, group_uuid, localchat, fromme); await DiscordBotChatControler(message, sender_name, sender_uuid, avatar, group, group_uuid, localchat, fromme);
} }
public override void CommandHistoryAdd(string command, string arg, bool status) public override string CommandHistoryAdd(string command, string arg, bool status,string WhyFailed)
{ {
base.CommandHistoryAdd(command, arg, status); string output = base.CommandHistoryAdd(command, arg, status, WhyFailed);
if (status == true) _ = SendMessageToChannelAsync("interface", output, "bot", UUID.Zero, myconfig.userName, "bot");
{ return output;
_ = SendMessageToChannelAsync("interface", "Command running: " + command + " ["+arg+"]", "bot", UUID.Zero, myconfig.userName, "bot");
}
else
{
_ = SendMessageToChannelAsync("interface", "Command rejected: " + command + " [" + arg + "]", "bot", UUID.Zero, myconfig.userName, "bot");
}
} }
public async override void sendIM(UUID avatar, string message) public async override void sendIM(UUID avatar, string message)