Skip to content

Commit

Permalink
fix broken stats channel timer (v0.11.2)
Browse files Browse the repository at this point in the history
  • Loading branch information
XHyperDEVX committed Dec 28, 2024
1 parent df2e18b commit 32d0430
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
43 changes: 17 additions & 26 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public async Task Startup()
LogOnlineUsersAsync();

//Start Stats Channel
UpdateStatsChannel();
UpdateStatsChannel_Timer();

Console.WriteLine($"Liebo started successfull.");
await Task.Delay(-1);
Expand Down Expand Up @@ -221,7 +221,7 @@ public static void HealthCheck()
}

//Bot Functions
private async void LogOnlineUsersAsync()
private async void UpdateStatsChannel_Timer()
{
while (true)
{
Expand All @@ -231,35 +231,26 @@ private async void LogOnlineUsersAsync()

await Task.Delay(delay);

using (var writer = new StreamWriter("onlinehistory.csv", true))
{
var onlineCount = guild.Users.Count(user => user.Status != UserStatus.Offline && !user.IsBot);
await writer.WriteLineAsync($"{new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds()},{onlineCount}");
}

//delete entries that are older than 24 hours
var lines = await File.ReadAllLinesAsync("onlinehistory.csv");
var filteredLines = lines.Where(line =>
{
var timestamp = long.Parse(line.Split(',')[0]);
return DateTime.UtcNow.AddHours(-24) < DateTimeOffset.FromUnixTimeSeconds(timestamp).UtcDateTime;
});
await File.WriteAllLinesAsync("onlinehistory.csv", filteredLines);
await LogOnlineUsersAsync();
await UpdateStatsChannel();
}
}

private async void UpdateStatsChannel_Timer()
private async Task LogOnlineUsersAsync()
{
while (true)
using (var writer = new StreamWriter("onlinehistory.csv", true))
{
var now = DateTime.UtcNow;
var nextRun = now.AddMinutes(5 - (now.Minute % 5)).AddSeconds(-now.Second).AddMilliseconds(-now.Millisecond);
var delay = nextRun - now;

await Task.Delay(delay);

await UpdateStatsChannel();
var onlineCount = guild.Users.Count(user => user.Status != UserStatus.Offline && !user.IsBot);
await writer.WriteLineAsync($"{new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds()},{onlineCount}");
}

//delete entries that are older than 24 hours
var lines = await File.ReadAllLinesAsync("onlinehistory.csv");
var filteredLines = lines.Where(line =>
{
var timestamp = long.Parse(line.Split(',')[0]);
return DateTime.UtcNow.AddHours(-24) < DateTimeOffset.FromUnixTimeSeconds(timestamp).UtcDateTime;
});
await File.WriteAllLinesAsync("onlinehistory.csv", filteredLines);
}
private async Task UpdateStatsChannel()
{
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.11.1
0.11.2

0 comments on commit 32d0430

Please sign in to comment.