mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
Add JavaScript GIF response. Change cooldown.
This commit is contained in:
@@ -19,8 +19,8 @@ namespace West.Entertainment.Handler.Fun
|
|||||||
|
|
||||||
private readonly ILogger _logger;
|
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;
|
protected Message? Message => RawUpdate.Message;
|
||||||
|
|
||||||
private User? RepliedUser => Message?.ReplyToMessage?.From;
|
private User? RepliedUser => Message?.ReplyToMessage?.From;
|
||||||
@@ -53,20 +53,21 @@ namespace West.Entertainment.Handler.Fun
|
|||||||
// ReSharper disable once MemberCanBeProtected.Global
|
// ReSharper disable once MemberCanBeProtected.Global
|
||||||
public virtual async Task<bool> HandleAnimation()
|
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 animationFileId = (await GetAnimationFileOption()).GetValue(string.Empty);
|
||||||
var lastUseOption = await GetLastUseOption();
|
var lastUseOption = await GetLastUseOption();
|
||||||
|
|
||||||
_logger.LogInformation("Animation: {Text}, ToLower: {ToLowerText}",
|
_logger.LogDebug("Animation: {Text}, ToLower: {ToLowerText}",
|
||||||
Message!.Text, Message!.Text!.ToLower());
|
Message!.Text, Message!.Text!.ToLower());
|
||||||
|
|
||||||
if (animationFileId.IsNullOrEmpty()
|
if (animationFileId.IsNullOrEmpty()) return false;
|
||||||
|| !AnimationMatch.IsMatch(Message!.Text!.ToLower())
|
|
||||||
|| !CanHandle()) return false;
|
|
||||||
|
|
||||||
var nextUseDt = lastUseOption.GetValue<DateTime>();
|
var nextUseDt = lastUseOption.GetValue<DateTime>();
|
||||||
if (nextUseDt > DateTime.Now)
|
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);
|
GetAnimationName(), Chat.Title, nextUseDt);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@@ -75,7 +76,7 @@ namespace West.Entertainment.Handler.Fun
|
|||||||
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));
|
lastUseOption.SetValue(DateTime.Now + Cooldown);
|
||||||
_db.MarkAsModified(lastUseOption);
|
_db.MarkAsModified(lastUseOption);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -88,7 +89,7 @@ namespace West.Entertainment.Handler.Fun
|
|||||||
|
|
||||||
private async Task<BotUserValue> GetLastUseOption()
|
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()
|
private async Task<bool> CanPunish()
|
||||||
|
|||||||
@@ -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<JavaScript> 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<bool> HandleAnimation()
|
||||||
|
=> await base.HandleAnimation();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,9 +18,6 @@ namespace West.Entertainment.Handler.Fun
|
|||||||
protected override string GetAnimationName()
|
protected override string GetAnimationName()
|
||||||
=> "python";
|
=> "python";
|
||||||
|
|
||||||
protected override bool CanHandle()
|
|
||||||
=> !Message!.Text!.Contains("/setpython");
|
|
||||||
|
|
||||||
[Command(InChat.Public, "setpython", CommandParseMode.Both)]
|
[Command(InChat.Public, "setpython", CommandParseMode.Both)]
|
||||||
public async Task HandleSetPython()
|
public async Task HandleSetPython()
|
||||||
=> await HandleSetAnimation();
|
=> await HandleSetAnimation();
|
||||||
|
|||||||
Reference in New Issue
Block a user