Skip to content

Commit

Permalink
Add DiscordRichPresence Disable
Browse files Browse the repository at this point in the history
  • Loading branch information
BigBang1112 committed Feb 10, 2023
1 parent bc1a6db commit 687c041
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
1 change: 1 addition & 0 deletions Src/RandomizerTMF.Logic/DiscordRichPresenceConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

public class DiscordRichPresenceConfig
{
public bool Disable { get; set; }
public bool DisableMapThumbnail { get; set; }
}
44 changes: 27 additions & 17 deletions Src/RandomizerTMF.Logic/Services/DiscordRichPresence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ public interface IDiscordRichPresence : IDisposable

internal class DiscordRichPresence : IDiscordRichPresence
{
private readonly DiscordRpcClient client;
private readonly DiscordRpcClient? client;
private readonly IRandomizerConfig config;

public DiscordRichPresence(DiscordRpcLogger discordLogger, IRandomizerConfig config)
{
this.config = config;

if (config.DiscordRichPresence.Disable)
{
return;
}

client = new DiscordRpcClient("1048435107494637618", logger: discordLogger);
client.Initialize();

this.config = config;
}

public void InDashboard()
Expand All @@ -46,30 +51,35 @@ public void Idle()
}

public void SessionDetails(string details)
{
client.UpdateDetails(details);
{
client?.UpdateDetails(details);
}

public void SessionStart(DateTime start)
{
client.UpdateStartTime(start);
client?.UpdateStartTime(start);
}

public void SessionPredictEnd(DateTime end)
{
client.UpdateEndTime(end);
client?.UpdateEndTime(end);
}

public void AddToSessionPredictEnd(TimeSpan addition)
{
if (client.CurrentPresence.Timestamps.End.HasValue)
if (client?.CurrentPresence.Timestamps.End.HasValue == true)
{
SessionPredictEnd(client.CurrentPresence.Timestamps.End.Value + addition);
}
}

public void SessionMap(string mapName, string imageUrl, string env)
{
if (config.DiscordRichPresence.Disable)
{
return;
}

var envRemap = env.ToLower();

switch (envRemap)
Expand All @@ -80,15 +90,15 @@ public void SessionMap(string mapName, string imageUrl, string env)

if (!config.DiscordRichPresence.DisableMapThumbnail)
{
client.UpdateLargeAsset(imageUrl, mapName);
client?.UpdateLargeAsset(imageUrl, mapName);
}

client.UpdateSmallAsset(envRemap, env);
client?.UpdateSmallAsset(envRemap, env);
}

public void SessionState(int atCount = 0, int goldCount = 0, int skipCount = 0)
{
client.UpdateState(BuildState(atCount, goldCount, skipCount));
client?.UpdateState(BuildState(atCount, goldCount, skipCount));
}

internal static string BuildState(int atCount, int goldCount, int skipCount)
Expand Down Expand Up @@ -126,13 +136,13 @@ internal static string BuildState(int atCount, int goldCount, int skipCount)

public void SessionDefaultAsset()
{
client.UpdateLargeAsset("primary", "");
client.UpdateSmallAsset();
client?.UpdateLargeAsset("primary", "");
client?.UpdateSmallAsset();
}

private void Default(string details)
{
client.SetPresence(new RichPresence()
client?.SetPresence(new RichPresence()
{
Details = details,
Timestamps = new Timestamps(DateTime.UtcNow),
Expand All @@ -145,8 +155,8 @@ private void Default(string details)

public void Dispose()
{
client.ClearPresence();
client.Deinitialize();
client.Dispose();
client?.ClearPresence();
client?.Deinitialize();
client?.Dispose();
}
}

0 comments on commit 687c041

Please sign in to comment.