Skip to content

Commit

Permalink
Force plugin to resend bot activity if 10 minutes have passed
Browse files Browse the repository at this point in the history
  • Loading branch information
KarlOfDuty committed Dec 11, 2024
1 parent 5537406 commit a8bb9da
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
8 changes: 4 additions & 4 deletions SCPDiscordBot/MessageScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public static async Task Init()
continue;
}

// Clean old interactions from cache
interactionCache.RemoveAll(x => x.Interaction.Id.GetSnowflakeTime() < DateTimeOffset.Now - TimeSpan.FromSeconds(30));

try
{
// Clean old interactions from cache
interactionCache.RemoveAll(x => x.Interaction.Id.GetSnowflakeTime() < DateTimeOffset.Now - TimeSpan.FromSeconds(30));

foreach (KeyValuePair<ulong, ConcurrentQueue<string>> channelQueue in messageQueues)
{
StringBuilder finalMessage = new StringBuilder();
Expand Down Expand Up @@ -77,7 +77,7 @@ public static async Task Init()
}
catch (Exception e)
{
Console.WriteLine(e);
Logger.Error("Message scheduler error: ", e);
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions SCPDiscordPlugin/NetworkSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ public ProcessEmbedMessageByIDAsync(EmbedMessage embed, string messagePath, Dict

public static class NetworkSystem
{
private const int ACTIVITY_UPDATE_RATE_MS = 10000;
private static Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
public static NetworkStream networkStream { get; private set; } = null;
private static readonly List<MessageWrapper> messageQueue = new List<MessageWrapper>();
Expand Down Expand Up @@ -331,17 +330,16 @@ public static void QueueMessage(MessageWrapper message)

private static void RefreshBotStatus()
{
if (activityUpdateTimer.ElapsedMilliseconds < ACTIVITY_UPDATE_RATE_MS && activityUpdateTimer.IsRunning) return;

activityUpdateTimer.Reset();
activityUpdateTimer.Start();
if (activityUpdateTimer.Elapsed < TimeSpan.FromSeconds(10) && activityUpdateTimer.IsRunning) return;

// Skip if the player count hasn't changed
if (previousActivityPlayerCount == Player.Count)
if (previousActivityPlayerCount == Player.Count && activityUpdateTimer.Elapsed < TimeSpan.FromMinutes(10))
{
return;
}

activityUpdateTimer.Restart();

// Don't block updates if the server hasn't loaded the max player count yet
if (Server.MaxPlayers <= 0)
{
Expand Down

0 comments on commit a8bb9da

Please sign in to comment.