Add special ratelimit for award (1 in 1 minute)

This commit is contained in:
Basique Evangelist 2019-12-09 20:54:10 +00:00
parent 72fbee9c46
commit 00dd51cea9
Signed by untrusted user: BasiqueEvangelist
GPG Key ID: B370219149301706
3 changed files with 30 additions and 10 deletions

View File

@ -21,6 +21,16 @@ namespace JetKarmaBot.Commands
var currentLocale = ctx.GetFeature<Locale>();
var awarder = ctx.EventArgs.Message.From;
if (Timeout.TimeoutCache[awarder.Id].PreviousAwardDate.AddSeconds(Config.Timeout.AwardTimeSeconds) > DateTime.Now)
{
ctx.GetFeature<TimeoutManager.Feature>().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";

View File

@ -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;

View File

@ -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<int, TimeoutStats> TimeoutCache = new Dictionary<int, TimeoutStats>();
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<Locale>();
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<ChatCommandRouter.Feature>();
await ApplyCost(getTypeName(routerFeature.CommandType), routerFeature.Succeded, uid, db);
await ApplyCost(getTypeName(routerFeature.CommandType), routerFeature.Succeded, uid, db, feature);
}
private string getTypeName(Type t)
{