-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCooldownAttribute.cs
More file actions
58 lines (53 loc) · 2.41 KB
/
CooldownAttribute.cs
File metadata and controls
58 lines (53 loc) · 2.41 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using Ultz.Extensions.Commands.Built;
using Ultz.Extensions.Commands.Cooldown;
namespace Ultz.Extensions.Commands.Attributes.Commands.Cooldown
{
/// <summary>
/// Applies a <see cref="Cooldown" /> to the <see cref="Command" />.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class CooldownAttribute : Attribute
{
/// <summary>
/// Initialises a new <see cref="CooldownAttribute" /> with the specified <see cref="Cooldown" /> properties.
/// </summary>
/// <param name="amount"> The amount of uses per given window. </param>
/// <param name="per"> The bucket time window. </param>
/// <param name="cooldownMeasure"> The unit of time of the given window. </param>
/// <param name="bucketType"> The bucket type. Has to be an <see langword="enum" />. </param>
/// <exception cref="ArgumentOutOfRangeException">
/// Not a valid cooldown measure.
/// </exception>
public CooldownAttribute(int amount, double per, CooldownMeasure cooldownMeasure, object bucketType)
{
if (!(bucketType is Enum enumBucketType))
{
throw new ArgumentException("Bucket type must be an enum.", nameof(bucketType));
}
Amount = amount;
Per = cooldownMeasure switch
{
CooldownMeasure.Milliseconds => TimeSpan.FromMilliseconds(per),
CooldownMeasure.Seconds => TimeSpan.FromSeconds(per),
CooldownMeasure.Minutes => TimeSpan.FromMinutes(per),
CooldownMeasure.Hours => TimeSpan.FromHours(per),
CooldownMeasure.Days => TimeSpan.FromDays(per),
_ => throw new ArgumentOutOfRangeException(nameof(cooldownMeasure), "Invalid cooldown measure.")
};
BucketType = enumBucketType;
}
/// <summary>
/// Gets the amount of times the <see cref="Command" /> can be used in the given time window.
/// </summary>
public int Amount { get; }
/// <summary>
/// Gets the time window of this cooldown.
/// </summary>
public TimeSpan Per { get; }
/// <summary>
/// Gets the <see langword="enum" /> bucket type to use with the <see cref="CooldownBucketKeyGeneratorDelegate" />.
/// </summary>
public Enum BucketType { get; }
}
}