Fixed command cooldown calculation. Closes #387

This commit is contained in:
Kwoth
2022-10-25 01:57:58 +02:00
parent e68e948a80
commit 9d9e61fdfb

View File

@@ -1,5 +1,4 @@
#nullable disable using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Common.ModuleBehaviors; using NadekoBot.Common.ModuleBehaviors;
using NadekoBot.Db; using NadekoBot.Db;
@@ -53,7 +52,7 @@ public sealed class CmdCdService : IExecPreCommand, IReadyExecutor, INService
if (cooldowns.TryGetValue(user.Id, out var oldValue)) if (cooldowns.TryGetValue(user.Id, out var oldValue))
{ {
var diff = DateTime.UtcNow - oldValue; var diff = DateTime.UtcNow - oldValue;
if (diff.Seconds > cdSeconds) if (diff.TotalSeconds > cdSeconds)
{ {
if (cooldowns.TryUpdate(user.Id, DateTime.UtcNow, oldValue)) if (cooldowns.TryUpdate(user.Id, DateTime.UtcNow, oldValue))
return Task.FromResult(false); return Task.FromResult(false);
@@ -69,7 +68,6 @@ public sealed class CmdCdService : IExecPreCommand, IReadyExecutor, INService
while (await timer.WaitForNextTickAsync()) while (await timer.WaitForNextTickAsync())
{ {
var now = DateTime.UtcNow;
// once per hour delete expired entries // once per hour delete expired entries
foreach (var ((guildId, commandName), dict) in _activeCooldowns) foreach (var ((guildId, commandName), dict) in _activeCooldowns)
{ {
@@ -90,7 +88,7 @@ public sealed class CmdCdService : IExecPreCommand, IReadyExecutor, INService
private void Cleanup(ConcurrentDictionary<ulong, DateTime> dict, int cdSeconds) private void Cleanup(ConcurrentDictionary<ulong, DateTime> dict, int cdSeconds)
{ {
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
foreach (var (key, _) in dict.Where(x => (now - x.Value).Seconds > cdSeconds).ToArray()) foreach (var (key, _) in dict.Where(x => (now - x.Value).TotalSeconds > cdSeconds).ToArray())
{ {
dict.TryRemove(key, out _); dict.TryRemove(key, out _);
} }