-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the command 'purge'
- Loading branch information
Showing
9 changed files
with
88 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Threading.Tasks; | ||
using Discord.Commands; | ||
|
||
namespace Pootis_Bot.Preconditions | ||
{ | ||
public class CooldownAttribute : PreconditionAttribute | ||
{ | ||
TimeSpan CooldownLength { get; set; } | ||
readonly ConcurrentDictionary<CooldownInfo, DateTime> _cooldowns = new ConcurrentDictionary<CooldownInfo, DateTime>(); | ||
|
||
public CooldownAttribute(int seconds) | ||
{ | ||
CooldownLength = TimeSpan.FromSeconds(seconds); | ||
} | ||
|
||
public struct CooldownInfo | ||
{ | ||
public ulong UserId { get; } | ||
public int CommandHashCode { get; } | ||
|
||
public CooldownInfo(ulong userId, int commandHashCode) | ||
{ | ||
UserId = userId; | ||
CommandHashCode = commandHashCode; | ||
} | ||
} | ||
|
||
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) | ||
{ | ||
var key = new CooldownInfo(context.User.Id, command.GetHashCode()); | ||
|
||
if (_cooldowns.TryGetValue(key, out DateTime endsAt)) | ||
{ | ||
var difference = endsAt.Subtract(DateTime.Now); | ||
if (difference.Ticks > 0) | ||
{ | ||
return Task.FromResult(PreconditionResult.FromError($"Please wait {difference.ToString(@"ss")} seconds before trying again!")); | ||
} | ||
|
||
var time = DateTime.Now.Add(CooldownLength); | ||
_cooldowns.TryUpdate(key, time, endsAt); | ||
} | ||
else | ||
{ | ||
_cooldowns.TryAdd(key, DateTime.Now.Add(CooldownLength)); | ||
} | ||
|
||
return Task.FromResult(PreconditionResult.FromSuccess()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters