mirror of
https://github.com/Jetsparrow/karmabot.git
synced 2026-01-21 09:06:09 +03:00
Add special ratelimit for award (1 in 1 minute)
This commit is contained in:
parent
8796c099de
commit
f11529a4cb
@ -21,6 +21,16 @@ namespace JetKarmaBot.Commands
|
|||||||
var currentLocale = ctx.GetFeature<Locale>();
|
var currentLocale = ctx.GetFeature<Locale>();
|
||||||
|
|
||||||
var awarder = ctx.EventArgs.Message.From;
|
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;
|
string awardTypeText = null;
|
||||||
int recipientId = default(int);
|
int recipientId = default(int);
|
||||||
foreach (string arg in ctx.Command.Parameters)
|
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);
|
var response = message + "\n" + String.Format(currentLocale["jetkarmabot.award.statustext"], recUserName, prevCount + (awarding ? 1 : -1), awardType.Symbol);
|
||||||
|
|
||||||
await ctx.SendMessage(response);
|
await ctx.SendMessage(response);
|
||||||
|
Timeout.TimeoutCache[awarder.Id].PreviousAwardDate = DateTime.Now;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,6 +136,8 @@ namespace JetKarmaBot.Commands
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Inject] Localization Locale { get; set; }
|
[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 Description => "Awards/revokes an award to a user.";
|
||||||
public string DescriptionID => "jetkarmabot.award.help";
|
public string DescriptionID => "jetkarmabot.award.help";
|
||||||
|
|||||||
@ -32,6 +32,7 @@ namespace JetKarmaBot
|
|||||||
{"Default", 60*5}
|
{"Default", 60*5}
|
||||||
};
|
};
|
||||||
public int SaveIntervalSeconds { get; private set; } = 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 TimeoutConfig Timeout { get; private set; } = new TimeoutConfig();
|
||||||
public bool SqlDebug { get; private set; } = false;
|
public bool SqlDebug { get; private set; } = false;
|
||||||
|
|||||||
@ -12,6 +12,10 @@ namespace JetKarmaBot.Services.Handling
|
|||||||
[Singleton]
|
[Singleton]
|
||||||
public class TimeoutManager : IRequestHandler
|
public class TimeoutManager : IRequestHandler
|
||||||
{
|
{
|
||||||
|
public class Feature
|
||||||
|
{
|
||||||
|
public double Multiplier = 1;
|
||||||
|
}
|
||||||
public class PreDbThrowout : IRequestHandler
|
public class PreDbThrowout : IRequestHandler
|
||||||
{
|
{
|
||||||
public TimeoutManager Timeout { get; }
|
public TimeoutManager Timeout { get; }
|
||||||
@ -32,18 +36,21 @@ namespace JetKarmaBot.Services.Handling
|
|||||||
await next(ctx);
|
await next(ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public struct TimeoutStats
|
public class TimeoutStats
|
||||||
{
|
{
|
||||||
public DateTime CooldownDate;
|
public DateTime CooldownDate;
|
||||||
public bool TimeoutMessaged;
|
public bool TimeoutMessaged;
|
||||||
|
public DateTime PreviousAwardDate;
|
||||||
}
|
}
|
||||||
[Inject] private KarmaContextFactory Db;
|
[Inject] private KarmaContextFactory Db;
|
||||||
[Inject] private Config cfg;
|
[Inject] private Config cfg;
|
||||||
[Inject] private Localization Locale;
|
[Inject] private Localization Locale;
|
||||||
[Inject] private Logger log;
|
[Inject] private Logger log;
|
||||||
public Dictionary<int, TimeoutStats> TimeoutCache = new Dictionary<int, TimeoutStats>();
|
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 + (succeded ? " (OK)" : "(ERR)"), out var costSeconds))
|
||||||
if (!cfg.Timeout.CommandCostsSeconds.TryGetValue(name, out costSeconds))
|
if (!cfg.Timeout.CommandCostsSeconds.TryGetValue(name, out costSeconds))
|
||||||
if (!cfg.Timeout.CommandCostsSeconds.TryGetValue("Default", out costSeconds))
|
if (!cfg.Timeout.CommandCostsSeconds.TryGetValue("Default", out costSeconds))
|
||||||
@ -55,12 +62,9 @@ namespace JetKarmaBot.Services.Handling
|
|||||||
if (TimeoutCache[uid].CooldownDate >= debtLimit)
|
if (TimeoutCache[uid].CooldownDate >= debtLimit)
|
||||||
//Programming error
|
//Programming error
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
TimeoutCache[uid] = new TimeoutStats()
|
TimeoutCache[uid].CooldownDate = (TimeoutCache[uid].CooldownDate <= DateTime.Now ? DateTime.Now : TimeoutCache[uid].CooldownDate)
|
||||||
{
|
.AddSeconds(feature.Multiplier * costSeconds);
|
||||||
CooldownDate = (TimeoutCache[uid].CooldownDate <= DateTime.Now ? DateTime.Now : TimeoutCache[uid].CooldownDate).AddSeconds(costSeconds),
|
TimeoutCache[uid].TimeoutMessaged = false;
|
||||||
TimeoutMessaged = false
|
|
||||||
};
|
|
||||||
TimeoutCache[uid] = TimeoutCache[uid];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task PopulateStats(int uid, KarmaContext db)
|
private async Task PopulateStats(int uid, KarmaContext db)
|
||||||
@ -108,15 +112,17 @@ namespace JetKarmaBot.Services.Handling
|
|||||||
{
|
{
|
||||||
Locale currentLocale = ctx.GetFeature<Locale>();
|
Locale currentLocale = ctx.GetFeature<Locale>();
|
||||||
await ctx.SendMessage(currentLocale["jetkarmabot.ratelimit"]);
|
await ctx.SendMessage(currentLocale["jetkarmabot.ratelimit"]);
|
||||||
TimeoutCache[uid] = new TimeoutStats() { TimeoutMessaged = true, CooldownDate = TimeoutCache[uid].CooldownDate };
|
TimeoutCache[uid].TimeoutMessaged = true;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Feature feature = new Feature();
|
||||||
|
ctx.Features.Add(feature);
|
||||||
|
|
||||||
await next(ctx);
|
await next(ctx);
|
||||||
|
|
||||||
var routerFeature = ctx.GetFeature<ChatCommandRouter.Feature>();
|
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)
|
private string getTypeName(Type t)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user