Merge pull request #106 from Pokechu22/add-bot-ratelimit

Add an option to add a delay between bot messages
This commit is contained in:
ORelio 2016-01-30 10:59:06 +01:00
commit 73a1645f63
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 { get { return master != null ? master.Handler : _handler; } }
private McTcpClient _handler = null; private McTcpClient _handler = null;
private ChatBot master = 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 */ /* 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 /// Send text to the server. Can be anything such as chat messages or commands
/// </summary> /// </summary>
/// <param name="text">Text to send to the server</param> /// <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> /// <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 + "'"); LogToConsole("Sending '" + text + "'");
lastMessageSentTime = DateTime.Now;
return Handler.SendText(text); return Handler.SendText(text);
} }

View file

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

View file

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