Add an option to add a delay between bot message

Because bots can send several messages quickly, this adds an option to slow down the rate at which messages are produced (to avoid issues with antispam plugins).  This should help solve part of the troubles @mobdon was having in #105.

Right now the default time is 2 seconds per message.  However, messages are sent imediately if the bot doesn't need to delay (so if it's a bot that only outputs one or two messages, those will still happen imediately).  Also, note that it's limited per-bot right now.

I also added an optional parameter to the SendText method so that bots can avoid this behavior if they need to.  In some cases, they'll want to send multiple messages.
This commit is contained in:
Pokechu22 2016-01-29 16:11:26 -08:00
parent ba41268aca
commit 207732cd86
3 changed files with 46 additions and 1 deletions

View file

@ -41,6 +41,34 @@ namespace MinecraftClient
private McTcpClient Handler { get { return master != null ? master.Handler : _handler; } }
private McTcpClient _handler = null;
private ChatBot master = null;
private Queue<string> chatQueue = new Queue<string>();
private DateTime? lastMessageSentTime = null;
private bool CanSendTextNow
{
get
{
return lastMessageSentTime != null
? DateTime.Now > lastMessageSentTime.Value + Settings.botMessageDelay
: true;
}
}
/// <summary>
/// Processes the current chat message queue, displaying a message after enough time passes.
/// </summary>
internal void ProcessQueuedText()
{
if (chatQueue.Count > 0)
{
if (CanSendTextNow)
{
string text = chatQueue.Dequeue();
LogToConsole("Sending '" + text + "'");
lastMessageSentTime = DateTime.Now;
Handler.SendText(text);
}
}
}
/* ================================================== */
/* Main methods to override for creating your bot */
@ -84,11 +112,24 @@ namespace MinecraftClient
/// Send text to the server. Can be anything such as chat messages or commands
/// </summary>
/// <param name="text">Text to send to the server</param>
/// <param name="sendImmediately">Whether the message should be sent immediately rather than being queued to avoid chat spam</param>
/// <returns>True if the text was sent with no error</returns>
protected bool SendText(string text)
protected bool SendText(string text, bool sendImmediately = false)
{
if (Settings.botMessageDelay.TotalSeconds > 0 && !sendImmediately)
{
if (!CanSendTextNow)
{
chatQueue.Enqueue(text);
// TODO: We don't know whether there was an error at this point, so we assume there isn't.
// Might not be the best idea.
return true;
}
}
LogToConsole("Sending '" + text + "'");
lastMessageSentTime = DateTime.Now;
return Handler.SendText(text);
}

View file

@ -459,6 +459,7 @@ namespace MinecraftClient
try
{
bots[i].Update();
bots[i].ProcessQueuedText();
}
catch (Exception e)
{

View file

@ -44,6 +44,7 @@ namespace MinecraftClient
public static string TranslationsFile_Website_Download = "http://resources.download.minecraft.net";
public static TimeSpan splitMessageDelay = TimeSpan.FromSeconds(2);
public static List<string> Bots_Owners = new List<string>();
public static TimeSpan botMessageDelay = TimeSpan.FromSeconds(2);
public static string Language = "en_GB";
public static bool chatTimeStamps = false;
public static bool interactiveMode = true;
@ -182,6 +183,7 @@ namespace MinecraftClient
case "showxpbarmessages": DisplayXPBarMessages = str2bool(argValue); break;
case "terrainandmovements": TerrainAndMovements = str2bool(argValue); break;
case "privatemsgscmdname": PrivateMsgsCmdName = argValue.ToLower().Trim(); break;
case "botmessagedelay": botMessageDelay = TimeSpan.FromSeconds(str2int(argValue)); break;
case "botowners":
Bots_Owners.Clear();
@ -406,6 +408,7 @@ namespace MinecraftClient
+ "consoletitle=%username%@%serverip% - Minecraft Console Client\r\n"
+ "internalcmdchar=slash #use 'none', 'slash' or 'backslash'\r\n"
+ "splitmessagedelay=2 #seconds between each part of a long message\r\n"
+ "botmessagedelay=2 #seconds to delay between message a bot makes to avoid accidental spam\n\n"
+ "mcversion=auto #use 'auto' or '1.X.X' values\r\n"
+ "brandinfo=mcc #use 'mcc','vanilla', or 'none'\r\n"
+ "chatbotlogfile= #leave empty for no logfile\r\n"