From 6c20d6fa648b11a7c8a874fcfdcff47483318cdd Mon Sep 17 00:00:00 2001 From: Toastie <toastie@toastiet0ast.com> Date: Mon, 3 Feb 2025 02:21:03 +1300 Subject: [PATCH] Fix bot status disappearing after some time --- Commands/AdminCommands.cs | 1 + EventHandler.cs | 9 +-------- SupportChild.cs | 24 ++++++++++++++++++++++++ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/Commands/AdminCommands.cs b/Commands/AdminCommands.cs index f395aaf..ddf8b8a 100644 --- a/Commands/AdminCommands.cs +++ b/Commands/AdminCommands.cs @@ -131,5 +131,6 @@ public class AdminCommands Logger.Log("Reloading bot..."); SupportChild.Reload(); + SupportChild.RefreshBotActivity(); } } \ No newline at end of file diff --git a/EventHandler.cs b/EventHandler.cs index 6841a45..f12e327 100644 --- a/EventHandler.cs +++ b/EventHandler.cs @@ -24,14 +24,7 @@ public static class EventHandler { Logger.Log("Connected to Discord."); - // Checking activity type - if (!Enum.TryParse(Config.presenceType, true, out DiscordActivityType activityType)) - { - Logger.Log("Presence type '" + Config.presenceType + "' invalid, using 'Playing' instead."); - activityType = DiscordActivityType.Playing; - } - - client.UpdateStatusAsync(new DiscordActivity(Config.presenceText, activityType), DiscordUserStatus.Online); + SupportChild.RefreshBotActivity(); hasLoggedGuilds = true; return Task.CompletedTask; } diff --git a/SupportChild.cs b/SupportChild.cs index 8893e7c..13ee295 100644 --- a/SupportChild.cs +++ b/SupportChild.cs @@ -14,6 +14,7 @@ using Microsoft.Extensions.Logging; using SupportChild.Commands; using CommandLine; using DSharpPlus.Commands.Processors.SlashCommands; +using DSharpPlus.Entities; using DSharpPlus.Exceptions; using Microsoft.Extensions.DependencyInjection; @@ -22,6 +23,9 @@ namespace SupportChild; internal static class SupportChild { internal static DiscordClient client = null; + + private static Timer statusUpdateTimer; + public class CommandLineArguments { [Option('c', @@ -92,6 +96,9 @@ internal static class SupportChild return; } + // Create but don't start the timer, it will be started when the bot is connected. + statusUpdateTimer = new Timer(RefreshBotActivity, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); + if (!await Connect()) { Logger.Fatal("Aborting startup due to a fatal error when trying to connect to Discord."); @@ -252,6 +259,23 @@ internal static class SupportChild return true; } + + internal static void RefreshBotActivity(object state = null) + { + try + { + if (!Enum.TryParse(Config.presenceType, true, out DiscordActivityType activityType)) + { + Logger.Log("Presence type '" + Config.presenceType + "' invalid, using 'Playing' instead."); + activityType = DiscordActivityType.Playing; + } + client.UpdateStatusAsync(new DiscordActivity(Config.presenceText, activityType), DiscordUserStatus.Online); + } + finally + { + statusUpdateTimer.Change(TimeSpan.FromMinutes(30), Timeout.InfiniteTimeSpan); + } + } } internal class ErrorHandler : IClientErrorHandler