Add JavaScript GIF response. Change cooldown.

This commit is contained in:
West14
2022-01-03 20:58:26 +02:00
parent 6320b803df
commit 9f108d5021
3 changed files with 38 additions and 12 deletions
@@ -19,8 +19,8 @@ namespace West.Entertainment.Handler.Fun
private readonly ILogger _logger;
private const int Cooldown = 30;
private static TimeSpan Cooldown => TimeSpan.FromMinutes(new Random().Next(10, 1337));
protected Message? Message => RawUpdate.Message;
private User? RepliedUser => Message?.ReplyToMessage?.From;
@@ -53,20 +53,21 @@ namespace West.Entertainment.Handler.Fun
// ReSharper disable once MemberCanBeProtected.Global
public virtual async Task<bool> HandleAnimation()
{
var text = Message!.Text!.ToLower();
if (!AnimationMatch.IsMatch(text.ToLower()) || text.StartsWith("/set") || !CanHandle()) return false;
var animationFileId = (await GetAnimationFileOption()).GetValue(string.Empty);
var lastUseOption = await GetLastUseOption();
_logger.LogInformation("Animation: {Text}, ToLower: {ToLowerText}",
_logger.LogDebug("Animation: {Text}, ToLower: {ToLowerText}",
Message!.Text, Message!.Text!.ToLower());
if (animationFileId.IsNullOrEmpty()
|| !AnimationMatch.IsMatch(Message!.Text!.ToLower())
|| !CanHandle()) return false;
if (animationFileId.IsNullOrEmpty()) return false;
var nextUseDt = lastUseOption.GetValue<DateTime>();
if (nextUseDt > DateTime.Now)
{
_logger.LogInformation("{Name} is on cooldown for Chat {ChatTitle}. Will be available at {Time}",
_logger.LogInformation("{Name} is on cooldown for chat \"{ChatTitle}\". Will be available at {Time}",
GetAnimationName(), Chat.Title, nextUseDt);
return false;
@@ -75,7 +76,7 @@ namespace West.Entertainment.Handler.Fun
await Bot.SendAnimationAsync(Chat.Id, animationFileId,
replyToMessageId: GetMessageIdToReply() ?? Message?.MessageId);
lastUseOption.SetValue(DateTime.Now + TimeSpan.FromSeconds(Cooldown));
lastUseOption.SetValue(DateTime.Now + Cooldown);
_db.MarkAsModified(lastUseOption);
return true;
@@ -88,7 +89,7 @@ namespace West.Entertainment.Handler.Fun
private async Task<BotUserValue> GetLastUseOption()
{
return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}LastUse");
return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}NextUse");
}
private async Task<bool> CanPunish()