Add cooldown for animation replies.

This commit is contained in:
West14
2022-01-03 15:28:00 +02:00
parent 3261d613d0
commit 68deb1ec8b
@@ -1,5 +1,6 @@
#nullable enable #nullable enable
using System;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using BotFramework; using BotFramework;
@@ -15,6 +16,8 @@ namespace West.Entertainment.Handler.Fun
{ {
private readonly CoreContext _db; private readonly CoreContext _db;
private const int Cooldown = 30;
protected Message? Message => RawUpdate.Message; protected Message? Message => RawUpdate.Message;
private User? RepliedUser => Message?.ReplyToMessage?.From; private User? RepliedUser => Message?.ReplyToMessage?.From;
@@ -47,11 +50,20 @@ namespace West.Entertainment.Handler.Fun
public virtual async Task<bool> HandleAnimation() public virtual async Task<bool> HandleAnimation()
{ {
var animationFileId = (await GetAnimationFileOption()).GetValue(string.Empty); var animationFileId = (await GetAnimationFileOption()).GetValue(string.Empty);
if (animationFileId.IsNullOrEmpty() || !AnimationMatch.IsMatch(Message!.Text!) || !CanHandle()) return false; var lastUseOption = await GetLastUseOption();
if (animationFileId.IsNullOrEmpty()
|| !AnimationMatch.IsMatch(Message!.Text!)
|| !CanHandle()
|| lastUseOption.GetValue<DateTime>() > DateTime.Now) return false;
await Bot.SendAnimationAsync(Chat.Id, animationFileId, await Bot.SendAnimationAsync(Chat.Id, animationFileId,
replyToMessageId: GetMessageIdToReply() ?? Message?.MessageId); replyToMessageId: GetMessageIdToReply() ?? Message?.MessageId);
lastUseOption.SetValue(DateTime.Now + TimeSpan.FromSeconds(Cooldown));
_db.MarkAsModified(lastUseOption);
return true; return true;
} }
@@ -60,6 +72,11 @@ namespace West.Entertainment.Handler.Fun
return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}File"); return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}File");
} }
private async Task<BotUserValue> GetLastUseOption()
{
return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}LastUse");
}
private async Task<bool> CanPunish() private async Task<bool> CanPunish()
{ {
return RepliedUser != null && await Bot.CanPunishMember(Chat, From, RepliedUser); return RepliedUser != null && await Bot.CanPunishMember(Chat, From, RepliedUser);