Added a feature to replay buffered messages while chat is offline (up to a limit of the 10 latest by default) and changed the sent message tracking to pass-by-reference instead of exclusively using GUIDs. The GUIDs still exist for scenarios where you can't pass a reference to the object around.

This commit is contained in:
barelyprofessional
2024-09-15 23:00:26 +08:00
parent 37f161663b
commit 04e73c8d5f
4 changed files with 51 additions and 14 deletions

View File

@@ -152,8 +152,22 @@ public class ChatBot
// Reset value to 0 as we've now successfully joined
if (_joinFailures > 0) _joinFailures = 0;
var settings = Helpers.GetMultipleValues([BuiltIn.Keys.GambaSeshDetectEnabled,
BuiltIn.Keys.GambaSeshUserId, BuiltIn.Keys.KiwiFarmsUsername])
BuiltIn.Keys.GambaSeshUserId, BuiltIn.Keys.KiwiFarmsUsername, BuiltIn.Keys.BotDisconnectReplayLimit])
.Result;
// Send messages if there are any to replay (Assuming we DC'd, and it's now the message flood)
foreach (var replayMsg in _sentMessages.Where(msg => msg.Status == SentMessageTrackerStatus.ChatDisconnected)
.TakeLast(settings[BuiltIn.Keys.BotDisconnectReplayLimit].ToType<int>()))
{
// Bypass the helpful method we have for sending messages so we don't create new sent message items for them
// The validation of whether to send based on GambaSesh's presence etc. has already been performed for msgs here
KfClient.SendMessage(replayMsg.Message);
replayMsg.Status = SentMessageTrackerStatus.WaitingForResponse;
replayMsg.SentAt = DateTimeOffset.UtcNow;
}
foreach(var lostMsg in _sentMessages.Where(msg => msg.Status == SentMessageTrackerStatus.ChatDisconnected))
{
lostMsg.Status = SentMessageTrackerStatus.Lost;
}
_logger.Debug($"Received {messages.Count} message(s)");
foreach (var message in messages)
{
@@ -203,7 +217,7 @@ public class ChatBot
}
}
public string SendChatMessage(string message, bool bypassSeshDetect = false)
public SentMessageTrackerModel SendChatMessage(string message, bool bypassSeshDetect = false)
{
var settings = Helpers
.GetMultipleValues([BuiltIn.Keys.KiwiFarmsSuppressChatMessages, BuiltIn.Keys.GambaSeshDetectEnabled])
@@ -221,20 +235,28 @@ public class ChatBot
_logger.Info($"Message was: {message}");
messageTracker.Status = SentMessageTrackerStatus.NotSending;
_sentMessages.Add(messageTracker);
return reference;
return messageTracker;
}
if (GambaSeshPresent && settings[BuiltIn.Keys.GambaSeshDetectEnabled].ToBoolean() && !bypassSeshDetect)
{
_logger.Info($"Not sending message '{message}' as GambaSesh is present");
messageTracker.Status = SentMessageTrackerStatus.NotSending;
_sentMessages.Add(messageTracker);
return reference;
return messageTracker;
}
if (!KfClient.IsConnected())
{
_logger.Info($"Not sending message '{message}' as Sneedchat is not connected");
messageTracker.Status = SentMessageTrackerStatus.ChatDisconnected;
_sentMessages.Add(messageTracker);
return messageTracker;
}
messageTracker.Status = SentMessageTrackerStatus.WaitingForResponse;
messageTracker.SentAt = DateTimeOffset.UtcNow;
_sentMessages.Add(messageTracker);
KfClient.SendMessage(message);
return reference;
return messageTracker;
}
public SentMessageTrackerModel GetSentMessageStatus(string reference)

View File

@@ -25,14 +25,15 @@ public class EditTestCommand : ICommand
var i = 0;
var delay = 1000;
var reference = botInstance.SendChatMessage($"{msg} {i}", true);
while (botInstance.GetSentMessageStatus(reference).Status == SentMessageTrackerStatus.WaitingForResponse)
while (reference.Status == SentMessageTrackerStatus.WaitingForResponse)
{
await Task.Delay(100, ctx);
}
var status = botInstance.GetSentMessageStatus(reference);
if (status.Status == SentMessageTrackerStatus.NotSending || status.Status == SentMessageTrackerStatus.Unknown ||
status.ChatMessageId == null)
if (reference.Status == SentMessageTrackerStatus.NotSending ||
reference.Status == SentMessageTrackerStatus.Unknown ||
reference.Status == SentMessageTrackerStatus.ChatDisconnected ||
reference.ChatMessageId == null)
{
logger.Error("Either message refused to send due to bot settings or something fucked up getting the message ID");
return;
@@ -41,13 +42,13 @@ public class EditTestCommand : ICommand
{
i++;
await Task.Delay(delay, ctx);
botInstance.KfClient.EditMessage(status.ChatMessageId!.Value, $"{msg} {i}");
botInstance.KfClient.EditMessage(reference.ChatMessageId!.Value, $"{msg} {i}");
}
await Task.Delay(delay, ctx);
botInstance.KfClient.EditMessage(status.ChatMessageId!.Value, "This message will self destruct in 1 second");
botInstance.KfClient.EditMessage(reference.ChatMessageId!.Value, "This message will self destruct in 1 second");
await Task.Delay(delay, ctx);
botInstance.KfClient.DeleteMessage(status.ChatMessageId!.Value);
botInstance.KfClient.DeleteMessage(reference.ChatMessageId!.Value);
}
}

View File

@@ -19,5 +19,9 @@ public enum SentMessageTrackerStatus
// If the bot is blocked from sending the message, e.g. due to suppress chat messages being enabled
NotSending,
// Shouldn't happen normally, it's just set before the bot has made a decision on whether to send or not,
Unknown
Unknown,
// Means the chat was disconnected when you attempted to send the message
ChatDisconnected,
// Was held in the replay buffer due to a disconnect, but there were too many messages ahead of it and so was culled
Lost
}

View File

@@ -516,6 +516,15 @@ public static class BuiltIn
Default = "https://kiwifarms.st/attachments/winmanjack_bgr-png.6414050/",
IsSecret = false,
CacheDuration = TimeSpan.FromHours(1)
},
new BuiltInSettingsModel
{
Key = Keys.BotDisconnectReplayLimit,
Regex = @"\d+",
Description = "Limit of messages which could not be sent while bot was disconnected to replay on connect",
Default = "10",
IsSecret = false,
CacheDuration = TimeSpan.FromHours(1)
}
];
@@ -565,5 +574,6 @@ public static class BuiltIn
public static string CrackedZalgoFuckUpMode = "Cracked.ZalgoFuckUpMode";
public static string CrackedZalgoFuckUpPosition = "Cracked.ZalgoFuckUpPosition";
public static string WinmanjackImgUrl = "Winmanjack.ImgUrl";
public static string BotDisconnectReplayLimit = "Bot.DisconnectReplayLimit";
}
}