From 00dd51cea9b462d4b908613f0cca4283c2edb8aa Mon Sep 17 00:00:00 2001 From: Basique Evangelist Date: Mon, 9 Dec 2019 20:54:10 +0000 Subject: [PATCH] Add special ratelimit for award (1 in 1 minute) --- JetKarmaBot/Commands/AwardCommand.cs | 13 ++++++++++ JetKarmaBot/Config.cs | 1 + .../Services/Handling/TimeoutManager.cs | 26 ++++++++++++------- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/JetKarmaBot/Commands/AwardCommand.cs b/JetKarmaBot/Commands/AwardCommand.cs index 0c76edf..86de14b 100644 --- a/JetKarmaBot/Commands/AwardCommand.cs +++ b/JetKarmaBot/Commands/AwardCommand.cs @@ -21,6 +21,16 @@ namespace JetKarmaBot.Commands var currentLocale = ctx.GetFeature(); var awarder = ctx.EventArgs.Message.From; + + if (Timeout.TimeoutCache[awarder.Id].PreviousAwardDate.AddSeconds(Config.Timeout.AwardTimeSeconds) > DateTime.Now) + { + ctx.GetFeature().Multiplier = 0; // Doesn't count as success or failure + if (!Timeout.TimeoutCache[awarder.Id].TimeoutMessaged) + await ctx.SendMessage(currentLocale["jetkarmabot.ratelimit"]); + Timeout.TimeoutCache[awarder.Id].TimeoutMessaged = true; + return false; + } + string awardTypeText = null; int recipientId = default(int); foreach (string arg in ctx.Command.Parameters) @@ -109,6 +119,7 @@ namespace JetKarmaBot.Commands var response = message + "\n" + String.Format(currentLocale["jetkarmabot.award.statustext"], recUserName, prevCount + (awarding ? 1 : -1), awardType.Symbol); await ctx.SendMessage(response); + Timeout.TimeoutCache[awarder.Id].PreviousAwardDate = DateTime.Now; return true; } @@ -125,6 +136,8 @@ namespace JetKarmaBot.Commands } [Inject] Localization Locale { get; set; } + [Inject] TimeoutManager Timeout { get; set; } + [Inject] Config Config { get; set; } public string Description => "Awards/revokes an award to a user."; public string DescriptionID => "jetkarmabot.award.help"; diff --git a/JetKarmaBot/Config.cs b/JetKarmaBot/Config.cs index 39af916..66f0cd1 100644 --- a/JetKarmaBot/Config.cs +++ b/JetKarmaBot/Config.cs @@ -32,6 +32,7 @@ namespace JetKarmaBot {"Default", 60*5} }; public int SaveIntervalSeconds { get; private set; } = 60 * 5; + public double AwardTimeSeconds { get; private set; } = 60; } public TimeoutConfig Timeout { get; private set; } = new TimeoutConfig(); public bool SqlDebug { get; private set; } = false; diff --git a/JetKarmaBot/Services/Handling/TimeoutManager.cs b/JetKarmaBot/Services/Handling/TimeoutManager.cs index 8a5891a..638168c 100644 --- a/JetKarmaBot/Services/Handling/TimeoutManager.cs +++ b/JetKarmaBot/Services/Handling/TimeoutManager.cs @@ -12,6 +12,10 @@ namespace JetKarmaBot.Services.Handling [Singleton] public class TimeoutManager : IRequestHandler { + public class Feature + { + public double Multiplier = 1; + } public class PreDbThrowout : IRequestHandler { public TimeoutManager Timeout { get; } @@ -32,18 +36,21 @@ namespace JetKarmaBot.Services.Handling await next(ctx); } } - public struct TimeoutStats + public class TimeoutStats { public DateTime CooldownDate; public bool TimeoutMessaged; + public DateTime PreviousAwardDate; } [Inject] private KarmaContextFactory Db; [Inject] private Config cfg; [Inject] private Localization Locale; [Inject] private Logger log; public Dictionary TimeoutCache = new Dictionary(); - private async Task ApplyCost(string name, bool succeded, int uid, KarmaContext db) + private async Task ApplyCost(string name, bool succeded, int uid, KarmaContext db, Feature feature) { + if (feature.Multiplier == 0) + return; if (!cfg.Timeout.CommandCostsSeconds.TryGetValue(name + (succeded ? " (OK)" : "(ERR)"), out var costSeconds)) if (!cfg.Timeout.CommandCostsSeconds.TryGetValue(name, out costSeconds)) if (!cfg.Timeout.CommandCostsSeconds.TryGetValue("Default", out costSeconds)) @@ -55,12 +62,9 @@ namespace JetKarmaBot.Services.Handling if (TimeoutCache[uid].CooldownDate >= debtLimit) //Programming error throw new NotImplementedException(); - TimeoutCache[uid] = new TimeoutStats() - { - CooldownDate = (TimeoutCache[uid].CooldownDate <= DateTime.Now ? DateTime.Now : TimeoutCache[uid].CooldownDate).AddSeconds(costSeconds), - TimeoutMessaged = false - }; - TimeoutCache[uid] = TimeoutCache[uid]; + TimeoutCache[uid].CooldownDate = (TimeoutCache[uid].CooldownDate <= DateTime.Now ? DateTime.Now : TimeoutCache[uid].CooldownDate) + .AddSeconds(feature.Multiplier * costSeconds); + TimeoutCache[uid].TimeoutMessaged = false; } private async Task PopulateStats(int uid, KarmaContext db) @@ -108,15 +112,17 @@ namespace JetKarmaBot.Services.Handling { Locale currentLocale = ctx.GetFeature(); await ctx.SendMessage(currentLocale["jetkarmabot.ratelimit"]); - TimeoutCache[uid] = new TimeoutStats() { TimeoutMessaged = true, CooldownDate = TimeoutCache[uid].CooldownDate }; + TimeoutCache[uid].TimeoutMessaged = true; } return; } + Feature feature = new Feature(); + ctx.Features.Add(feature); await next(ctx); var routerFeature = ctx.GetFeature(); - await ApplyCost(getTypeName(routerFeature.CommandType), routerFeature.Succeded, uid, db); + await ApplyCost(getTypeName(routerFeature.CommandType), routerFeature.Succeded, uid, db, feature); } private string getTypeName(Type t) {