mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-10 09:18:27 -04:00
Killed history
This commit is contained in:
38
NadekoBot.Core/Common/Attributes/Ratelimit.cs
Normal file
38
NadekoBot.Core/Common/Attributes/Ratelimit.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using Discord.Commands;
|
||||
using NadekoBot.Core.Services;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace NadekoBot.Core.Common.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public sealed class RatelimitAttribute : PreconditionAttribute
|
||||
{
|
||||
public int Seconds { get; }
|
||||
|
||||
public RatelimitAttribute(int seconds)
|
||||
{
|
||||
if (seconds <= 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(seconds));
|
||||
|
||||
Seconds = seconds;
|
||||
}
|
||||
|
||||
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
|
||||
{
|
||||
if (Seconds == 0)
|
||||
return Task.FromResult(PreconditionResult.FromSuccess());
|
||||
|
||||
var cache = services.GetService<IDataCache>();
|
||||
var rem = cache.TryAddRatelimit(context.User.Id, command.Name, Seconds);
|
||||
|
||||
if(rem == null)
|
||||
return Task.FromResult(PreconditionResult.FromSuccess());
|
||||
|
||||
var msgContent = $"You can use this command again in {rem.Value.TotalSeconds:F1}s.";
|
||||
|
||||
return Task.FromResult(PreconditionResult.FromError(msgContent));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user