-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandCooldown.cs
More file actions
38 lines (34 loc) · 1.33 KB
/
Copy pathCommandCooldown.cs
File metadata and controls
38 lines (34 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System.Collections.Concurrent;
using Microsoft.Extensions.Options;
using RustPlusBot.Abstractions.Time;
namespace RustPlusBot.Features.Commands.Dispatching;
/// <summary>Per-(server, command) cooldown. Singleton; in-memory; clock-driven.</summary>
/// <param name="clock">The clock.</param>
/// <param name="options">The command options carrying the cooldown window.</param>
internal sealed class CommandCooldown(IClock clock, IOptions<CommandOptions> options)
{
private readonly TimeSpan _cooldown = options.Value.Cooldown;
private readonly ConcurrentDictionary<(Guid Server, string Name), DateTimeOffset> _last = new();
/// <summary>Returns true if the command may run now (and records the run); false if on cooldown.</summary>
/// <param name="serverId">The server id.</param>
/// <param name="name">The lowercase command name.</param>
public bool TryConsume(Guid serverId, string name)
{
var now = clock.UtcNow;
var key = (serverId, name);
var allowed = true;
_last.AddOrUpdate(
key,
now,
(_, previous) =>
{
if (now - previous < _cooldown)
{
allowed = false;
return previous;
}
return now;
});
return allowed;
}
}