From 9f108d502109238c29eaaa3a76c9292809dac4f2 Mon Sep 17 00:00:00 2001 From: West14 <30056636+West14@users.noreply.github.com> Date: Mon, 3 Jan 2022 20:58:26 +0200 Subject: [PATCH] Add JavaScript GIF response. Change cooldown. --- .../Handler/Fun/AbstractAnimationReply.cs | 19 +++++++------ .../Entertainment/Handler/Fun/JavaScript.cs | 28 +++++++++++++++++++ modules/Entertainment/Handler/Fun/Python.cs | 3 -- 3 files changed, 38 insertions(+), 12 deletions(-) create mode 100644 modules/Entertainment/Handler/Fun/JavaScript.cs diff --git a/modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs b/modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs index 507b44c..e120ce7 100644 --- a/modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs +++ b/modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs @@ -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 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(); 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 GetLastUseOption() { - return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}LastUse"); + return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}NextUse"); } private async Task CanPunish() diff --git a/modules/Entertainment/Handler/Fun/JavaScript.cs b/modules/Entertainment/Handler/Fun/JavaScript.cs new file mode 100644 index 0000000..31dc912 --- /dev/null +++ b/modules/Entertainment/Handler/Fun/JavaScript.cs @@ -0,0 +1,28 @@ +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using BotFramework.Attributes; +using BotFramework.Enums; +using Kruzya.TelegramBot.Core.Data; +using Microsoft.Extensions.Logging; + +namespace West.Entertainment.Handler.Fun +{ + public class JavaScript : AbstractAnimationReply + { + public JavaScript(CoreContext db, ILogger logger) : base(db, logger) { } + + protected override Regex AnimationMatch => new( + "жс|js|жаваскрипт|javascript|жопаскрипт|жиес|жиэс|v8|nodejs", + RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); + + protected override string GetAnimationName() => "javascript"; + + [Command(InChat.Public, "setjs", CommandParseMode.Both)] + public async Task HandleSetJs() + => await HandleSetAnimation(); + + [Message(InChat.Public, MessageFlag.HasText)] + public override async Task HandleAnimation() + => await base.HandleAnimation(); + } +} \ No newline at end of file diff --git a/modules/Entertainment/Handler/Fun/Python.cs b/modules/Entertainment/Handler/Fun/Python.cs index 0e75d1d..fbc2870 100644 --- a/modules/Entertainment/Handler/Fun/Python.cs +++ b/modules/Entertainment/Handler/Fun/Python.cs @@ -18,9 +18,6 @@ namespace West.Entertainment.Handler.Fun protected override string GetAnimationName() => "python"; - protected override bool CanHandle() - => !Message!.Text!.Contains("/setpython"); - [Command(InChat.Public, "setpython", CommandParseMode.Both)] public async Task HandleSetPython() => await HandleSetAnimation();