Skip to content

Commit

Permalink
Fix bot status disappearing after some time
Browse files Browse the repository at this point in the history
  • Loading branch information
KarlOfDuty committed Dec 15, 2024
1 parent 9eb23d4 commit 6d8c22e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions Commands/AdminCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,6 @@ await command.RespondAsync(new DiscordEmbedBuilder

Logger.Log("Reloading bot...");
SupportBoi.Reload();
SupportBoi.RefreshBotActivity();
}
}
9 changes: 1 addition & 8 deletions EventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@ public static Task OnReady(DiscordClient client, GuildDownloadCompletedEventArgs
{
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);
SupportBoi.RefreshBotActivity();
hasLoggedGuilds = true;
return Task.CompletedTask;
}
Expand Down
25 changes: 25 additions & 0 deletions SupportBoi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using SupportBoi.Commands;
using CommandLine;
using DSharpPlus.Commands.Processors.SlashCommands;
using DSharpPlus.Entities;
using DSharpPlus.Exceptions;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -22,6 +23,9 @@ namespace SupportBoi;
internal static class SupportBoi
{
internal static DiscordClient client = null;

private static Timer statusUpdateTimer;

public class CommandLineArguments
{
[Option('c',
Expand Down Expand Up @@ -92,6 +96,9 @@ private static async Task MainAsync()
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.");
Expand Down Expand Up @@ -252,6 +259,24 @@ private static async Task<bool> Connect()

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
Expand Down

0 comments on commit 6d8c22e

Please sign in to comment.