Skip to content

Commit

Permalink
Use OnValueChanged for configuration values
Browse files Browse the repository at this point in the history
  • Loading branch information
Palladinium committed Dec 1, 2024
1 parent 725218c commit eb21153
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions Content.Server/Administration/Managers/WatchlistWebhookManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,28 @@ public sealed class WatchlistWebhookManager : IWatchlistWebhookManager

private ISawmill _sawmill = default!;

private WebhookIdentifier? _webhookIdentifier;
private List<WatchlistConnection> watchlistConnections = new();
private string _webhookUrl = default!;
private TimeSpan _bufferTime;

private List<WatchlistConnection> watchlistConnections = new();
private TimeSpan? _bufferStartTime;

public void Initialize()
{
_sawmill = Logger.GetSawmill("discord");
_cfg.OnValueChanged(CCVars.DiscordWatchlistConnectionBufferTime, SetBufferTime, true);
_cfg.OnValueChanged(CCVars.DiscordWatchlistConnectionWebhook, SetWebhookUrl, true);
_playerManager.PlayerStatusChanged += OnPlayerStatusChanged;
}

var bufferTimeSeconds = _cfg.GetCVar(CCVars.DiscordWatchlistConnectionBufferTime);
private void SetBufferTime(float bufferTimeSeconds)
{
_bufferTime = TimeSpan.FromSeconds(bufferTimeSeconds);
}

var webhook = _cfg.GetCVar(CCVars.DiscordWatchlistConnectionWebhook);
if (!string.IsNullOrWhiteSpace(webhook))
{
_discord.GetWebhook(webhook, data => _webhookIdentifier = data.ToIdentifier());
_playerManager.PlayerStatusChanged += OnPlayerStatusChanged;
}
private void SetWebhookUrl(string webhookUrl)
{
_webhookUrl = webhookUrl;
}

private async void OnPlayerStatusChanged(object? sender, SessionStatusEventArgs e)
Expand All @@ -68,35 +72,32 @@ private async void OnPlayerStatusChanged(object? sender, SessionStatusEventArgs
}
else
{
FlushConnections();
SendDiscordMessage();
}
}

public void Update()
{
if (_bufferStartTime != null && _gameTiming.RealTime > (_bufferStartTime + _bufferTime))
{
FlushConnections();
SendDiscordMessage();
_bufferStartTime = null;
}
}

private void FlushConnections()
{
SendDiscordMessage();

// Clear the buffered list regardless of whether the message is sent successfully
// This prevents infinitely buffering connections if we fail to send a message
watchlistConnections.Clear();
}

private async void SendDiscordMessage()
{
try
{
if (_webhookIdentifier == null)
if (string.IsNullOrWhiteSpace(_webhookUrl))
return;

var webhookData = await _discord.GetWebhook(_webhookUrl);
if (webhookData == null)
return;

var webhookIdentifier = webhookData.Value.ToIdentifier();

var messageBuilder = new StringBuilder(Loc.GetString("discord-watchlist-connection-header",
("players", watchlistConnections.Count),
("serverName", _baseServer.ServerName)));
Expand All @@ -116,12 +117,16 @@ private async void SendDiscordMessage()

var payload = new WebhookPayload { Content = messageBuilder.ToString() };

await _discord.CreateMessage(_webhookIdentifier.Value, payload);
await _discord.CreateMessage(webhookIdentifier, payload);
}
catch (Exception e)
{
_sawmill.Error($"Error while sending discord watchlist connection message:\n{e}");
}

// Clear the buffered list regardless of whether the message is sent successfully
// This prevents infinitely buffering connections if we fail to send a message
watchlistConnections.Clear();
}

private sealed class WatchlistConnection
Expand Down

0 comments on commit eb21153

Please sign in to comment.