Fix bot status disappearing after some time

This commit is contained in:
Toastie 2025-02-03 02:21:03 +13:00
parent e7377cdece
commit 6c20d6fa64
Signed by: toastie_t0ast
GPG key ID: 0861BE54AD481DC7
3 changed files with 26 additions and 8 deletions

View file

@ -131,5 +131,6 @@ public class AdminCommands
Logger.Log("Reloading bot...");
SupportChild.Reload();
SupportChild.RefreshBotActivity();
}
}

View file

@ -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;
}

View file

@ -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