Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion MinecraftClient/ChatBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,34 @@ public enum DisconnectReason { InGameKick, LoginRejected, ConnectionLost };
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 */
Expand Down Expand Up @@ -84,11 +112,24 @@ public virtual void GetText(string text) { }
/// 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);
}

Expand Down
1 change: 1 addition & 0 deletions MinecraftClient/McTcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ public void OnUpdate()
try
{
bots[i].Update();
bots[i].ProcessQueuedText();
}
catch (Exception e)
{
Expand Down
3 changes: 3 additions & 0 deletions MinecraftClient/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static class Settings
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;
Expand Down Expand Up @@ -182,6 +183,7 @@ public static void LoadSettings(string settingsfile)
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();
Expand Down Expand Up @@ -406,6 +408,7 @@ public static void WriteDefaultSettings(string settingsfile)
+ "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"
Expand Down