mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
Use file-scoped namespaces
This commit is contained in:
@@ -11,98 +11,97 @@ using Microsoft.Extensions.Logging;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace West.Entertainment.Handler.Fun
|
||||
namespace West.Entertainment.Handler.Fun;
|
||||
|
||||
public abstract class AbstractAnimationReply : BotEventHandler
|
||||
{
|
||||
public abstract class AbstractAnimationReply : BotEventHandler
|
||||
private readonly CoreContext _db;
|
||||
|
||||
private readonly ILogger _logger;
|
||||
private readonly ThreadSafeRandom _rng;
|
||||
|
||||
protected virtual TimeSpan Cooldown => TimeSpan.FromMinutes(_rng.Next(10, 1337));
|
||||
|
||||
protected Message? Message => RawUpdate.Message;
|
||||
|
||||
private User? RepliedUser => Message?.ReplyToMessage?.From;
|
||||
|
||||
protected abstract Regex AnimationMatch
|
||||
{
|
||||
private readonly CoreContext _db;
|
||||
|
||||
private readonly ILogger _logger;
|
||||
private readonly ThreadSafeRandom _rng;
|
||||
|
||||
protected virtual TimeSpan Cooldown => TimeSpan.FromMinutes(_rng.Next(10, 1337));
|
||||
|
||||
protected Message? Message => RawUpdate.Message;
|
||||
|
||||
private User? RepliedUser => Message?.ReplyToMessage?.From;
|
||||
|
||||
protected abstract Regex AnimationMatch
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
protected AbstractAnimationReply(CoreContext db, ILogger logger, ThreadSafeRandom rng)
|
||||
{
|
||||
_db = db;
|
||||
_logger = logger;
|
||||
_rng = rng;
|
||||
}
|
||||
|
||||
protected abstract string GetAnimationName();
|
||||
|
||||
protected virtual bool CanHandle() => true;
|
||||
|
||||
protected async Task HandleSetAnimation()
|
||||
{
|
||||
var animation = Message?.ReplyToMessage?.Animation;
|
||||
var isAdmin = await Bot.IsUserAdminAsync(Chat, From);
|
||||
|
||||
_logger.LogDebug("IsAdmin: {IsAdmin}. FileId: {FileId}", isAdmin, animation?.FileId);
|
||||
if (!isAdmin || animation == null) return;
|
||||
|
||||
var animationFileOption = await GetAnimationFileOption();
|
||||
animationFileOption.SetValue(animation.FileId);
|
||||
_db.AddOrUpdate(animationFileOption);
|
||||
|
||||
await Bot.SendTextMessageAsync(Chat.Id, "Animation has been set.", replyToMessageId: Message!.ReplyToMessage!.MessageId);
|
||||
}
|
||||
|
||||
protected 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 nextUseOption = await GetNextUseOption();
|
||||
|
||||
_logger.LogDebug("Animation: {Text}, ToLower: {ToLowerText}",
|
||||
Message!.Text, Message!.Text!.ToLower());
|
||||
_logger.LogDebug("AnimationFileId: {FileId}", animationFileId);
|
||||
|
||||
if (string.IsNullOrEmpty(animationFileId))
|
||||
{
|
||||
_logger.LogDebug("AnimationFileId is empty. Ignoring.");
|
||||
return false;
|
||||
}
|
||||
|
||||
var nextUseDt = nextUseOption.GetValue<DateTime>();
|
||||
if (nextUseDt > DateTime.Now)
|
||||
{
|
||||
_logger.LogInformation("{Name} is on cooldown for chat \"{ChatTitle}\". Will be available at {Time}",
|
||||
GetAnimationName(), Chat.Title, nextUseDt);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
await Bot.SendAnimationAsync(Chat.Id, new InputFileId(animationFileId),
|
||||
replyToMessageId: GetMessageIdToReply() ?? Message?.MessageId);
|
||||
|
||||
nextUseOption.SetValue(DateTime.Now + Cooldown);
|
||||
_db.AddOrUpdate(nextUseOption);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private async Task<BotUserValue> GetAnimationFileOption()
|
||||
{
|
||||
return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}File");
|
||||
}
|
||||
|
||||
private async Task<BotUserValue> GetNextUseOption()
|
||||
{
|
||||
return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}NextUse");
|
||||
}
|
||||
|
||||
protected virtual int? GetMessageIdToReply() => Message?.MessageId;
|
||||
get;
|
||||
}
|
||||
|
||||
protected AbstractAnimationReply(CoreContext db, ILogger logger, ThreadSafeRandom rng)
|
||||
{
|
||||
_db = db;
|
||||
_logger = logger;
|
||||
_rng = rng;
|
||||
}
|
||||
|
||||
protected abstract string GetAnimationName();
|
||||
|
||||
protected virtual bool CanHandle() => true;
|
||||
|
||||
protected async Task HandleSetAnimation()
|
||||
{
|
||||
var animation = Message?.ReplyToMessage?.Animation;
|
||||
var isAdmin = await Bot.IsUserAdminAsync(Chat, From);
|
||||
|
||||
_logger.LogDebug("IsAdmin: {IsAdmin}. FileId: {FileId}", isAdmin, animation?.FileId);
|
||||
if (!isAdmin || animation == null) return;
|
||||
|
||||
var animationFileOption = await GetAnimationFileOption();
|
||||
animationFileOption.SetValue(animation.FileId);
|
||||
_db.AddOrUpdate(animationFileOption);
|
||||
|
||||
await Bot.SendTextMessageAsync(Chat.Id, "Animation has been set.", replyToMessageId: Message!.ReplyToMessage!.MessageId);
|
||||
}
|
||||
|
||||
protected 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 nextUseOption = await GetNextUseOption();
|
||||
|
||||
_logger.LogDebug("Animation: {Text}, ToLower: {ToLowerText}",
|
||||
Message!.Text, Message!.Text!.ToLower());
|
||||
_logger.LogDebug("AnimationFileId: {FileId}", animationFileId);
|
||||
|
||||
if (string.IsNullOrEmpty(animationFileId))
|
||||
{
|
||||
_logger.LogDebug("AnimationFileId is empty. Ignoring.");
|
||||
return false;
|
||||
}
|
||||
|
||||
var nextUseDt = nextUseOption.GetValue<DateTime>();
|
||||
if (nextUseDt > DateTime.Now)
|
||||
{
|
||||
_logger.LogInformation("{Name} is on cooldown for chat \"{ChatTitle}\". Will be available at {Time}",
|
||||
GetAnimationName(), Chat.Title, nextUseDt);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
await Bot.SendAnimationAsync(Chat.Id, new InputFileId(animationFileId),
|
||||
replyToMessageId: GetMessageIdToReply() ?? Message?.MessageId);
|
||||
|
||||
nextUseOption.SetValue(DateTime.Now + Cooldown);
|
||||
_db.AddOrUpdate(nextUseOption);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private async Task<BotUserValue> GetAnimationFileOption()
|
||||
{
|
||||
return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}File");
|
||||
}
|
||||
|
||||
private async Task<BotUserValue> GetNextUseOption()
|
||||
{
|
||||
return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}NextUse");
|
||||
}
|
||||
|
||||
protected virtual int? GetMessageIdToReply() => Message?.MessageId;
|
||||
}
|
||||
@@ -10,42 +10,41 @@ using Kruzya.TelegramBot.Core.Data;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Telegram.Bot;
|
||||
|
||||
namespace West.Entertainment.Handler.Fun
|
||||
namespace West.Entertainment.Handler.Fun;
|
||||
|
||||
public class Bayan : AbstractAnimationReply
|
||||
{
|
||||
public class Bayan : AbstractAnimationReply
|
||||
protected override TimeSpan Cooldown => TimeSpan.Zero;
|
||||
|
||||
public Bayan(CoreContext db, ILogger<Bayan> logger, ThreadSafeRandom rng) : base(db, logger, rng) { }
|
||||
|
||||
protected override Regex AnimationMatch
|
||||
=> new("^баян$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
|
||||
|
||||
protected override string GetAnimationName()
|
||||
=> "bayan";
|
||||
|
||||
[InChat(InChat.Public)]
|
||||
[Command("setbayan", CommandParseMode.Both)]
|
||||
public async Task HandleSetBayan()
|
||||
=> await HandleSetAnimation();
|
||||
|
||||
[InChat(InChat.Public)]
|
||||
[Message(MessageFlag.HasText)]
|
||||
public async Task HandleBayan()
|
||||
{
|
||||
protected override TimeSpan Cooldown => TimeSpan.Zero;
|
||||
|
||||
public Bayan(CoreContext db, ILogger<Bayan> logger, ThreadSafeRandom rng) : base(db, logger, rng) { }
|
||||
|
||||
protected override Regex AnimationMatch
|
||||
=> new("^баян$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
|
||||
|
||||
protected override string GetAnimationName()
|
||||
=> "bayan";
|
||||
|
||||
[InChat(InChat.Public)]
|
||||
[Command("setbayan", CommandParseMode.Both)]
|
||||
public async Task HandleSetBayan()
|
||||
=> await HandleSetAnimation();
|
||||
|
||||
[InChat(InChat.Public)]
|
||||
[Message(MessageFlag.HasText)]
|
||||
public async Task HandleBayan()
|
||||
if (await HandleAnimation())
|
||||
{
|
||||
if (await HandleAnimation())
|
||||
{
|
||||
await Bot.DeleteMessageAsync(Chat.Id, Message!.MessageId);
|
||||
}
|
||||
await Bot.DeleteMessageAsync(Chat.Id, Message!.MessageId);
|
||||
}
|
||||
|
||||
protected override int? GetMessageIdToReply()
|
||||
{
|
||||
return Message!.ReplyToMessage!.MessageId;
|
||||
}
|
||||
|
||||
protected override bool CanHandle()
|
||||
=> Message?.ReplyToMessage != null;
|
||||
|
||||
}
|
||||
|
||||
protected override int? GetMessageIdToReply()
|
||||
{
|
||||
return Message!.ReplyToMessage!.MessageId;
|
||||
}
|
||||
|
||||
protected override bool CanHandle()
|
||||
=> Message?.ReplyToMessage != null;
|
||||
|
||||
}
|
||||
@@ -6,28 +6,27 @@ using Kruzya.TelegramBot.Core;
|
||||
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, ThreadSafeRandom rng) : base(db, logger, rng) { }
|
||||
namespace West.Entertainment.Handler.Fun;
|
||||
|
||||
protected override Regex AnimationMatch => new(
|
||||
@"((?<![\w])(жс|js|v8)(?![\w]))|жаваскрипт|javascript|жопаскрипт|жиес|жиэс|nodejs",
|
||||
public class JavaScript : AbstractAnimationReply
|
||||
{
|
||||
public JavaScript(CoreContext db, ILogger<JavaScript> logger, ThreadSafeRandom rng) : base(db, logger, rng) { }
|
||||
|
||||
protected override Regex AnimationMatch => new(
|
||||
@"((?<![\w])(жс|js|v8)(?![\w]))|жаваскрипт|javascript|жопаскрипт|жиес|жиэс|nodejs",
|
||||
RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
|
||||
|
||||
protected override string GetAnimationName() => "javascript";
|
||||
protected override string GetAnimationName() => "javascript";
|
||||
|
||||
[InChat(InChat.Public)]
|
||||
[Command("setjs", CommandParseMode.Both)]
|
||||
public async Task HandleSetJs()
|
||||
=> await HandleSetAnimation();
|
||||
[InChat(InChat.Public)]
|
||||
[Command("setjs", CommandParseMode.Both)]
|
||||
public async Task HandleSetJs()
|
||||
=> await HandleSetAnimation();
|
||||
|
||||
[InChat(InChat.Public)]
|
||||
[Message(MessageFlag.HasText)]
|
||||
public async Task HandleJs()
|
||||
{
|
||||
await HandleAnimation();
|
||||
}
|
||||
[InChat(InChat.Public)]
|
||||
[Message(MessageFlag.HasText)]
|
||||
public async Task HandleJs()
|
||||
{
|
||||
await HandleAnimation();
|
||||
}
|
||||
}
|
||||
@@ -8,29 +8,28 @@ using Kruzya.TelegramBot.Core;
|
||||
using Kruzya.TelegramBot.Core.Data;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace West.Entertainment.Handler.Fun
|
||||
namespace West.Entertainment.Handler.Fun;
|
||||
|
||||
public class Python : AbstractAnimationReply
|
||||
{
|
||||
public class Python : AbstractAnimationReply
|
||||
protected override Regex AnimationMatch =>
|
||||
new("питон|пайтон|python|путхон|питухон|петухон",
|
||||
RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
|
||||
|
||||
protected override string GetAnimationName()
|
||||
=> "python";
|
||||
|
||||
[InChat(InChat.Public)]
|
||||
[Command("setpython", CommandParseMode.Both)]
|
||||
public async Task HandleSetPython()
|
||||
=> await HandleSetAnimation();
|
||||
|
||||
[InChat(InChat.Public)]
|
||||
[Message(MessageFlag.HasText)]
|
||||
public async Task HandlePython()
|
||||
{
|
||||
protected override Regex AnimationMatch =>
|
||||
new("питон|пайтон|python|путхон|питухон|петухон",
|
||||
RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
|
||||
|
||||
protected override string GetAnimationName()
|
||||
=> "python";
|
||||
|
||||
[InChat(InChat.Public)]
|
||||
[Command("setpython", CommandParseMode.Both)]
|
||||
public async Task HandleSetPython()
|
||||
=> await HandleSetAnimation();
|
||||
|
||||
[InChat(InChat.Public)]
|
||||
[Message(MessageFlag.HasText)]
|
||||
public async Task HandlePython()
|
||||
{
|
||||
await HandleAnimation();
|
||||
}
|
||||
|
||||
public Python(CoreContext db, ILogger<Python> logger, ThreadSafeRandom rng) : base(db, logger, rng) { }
|
||||
await HandleAnimation();
|
||||
}
|
||||
|
||||
public Python(CoreContext db, ILogger<Python> logger, ThreadSafeRandom rng) : base(db, logger, rng) { }
|
||||
}
|
||||
Reference in New Issue
Block a user