Assorted improvements.

- Add UserService.
- Add "setrep" command.
- Rename confusing methods and variables in AbstractAnimationHandler.
- Change Bayan cooldown to Zero.
- Add crappy "cooldowns" command.
This commit is contained in:
West14
2022-01-04 17:45:06 +02:00
parent 8717ef9db9
commit 740527d827
7 changed files with 144 additions and 12 deletions
@@ -10,10 +10,6 @@
<ProjectReference Include="..\..\Core\Core.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Handler" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Delete Files="$(OutDir)\TelegramBot.dll" />
<Delete Files="$(OutDir)\TelegramBot.ChatManagement.dll" />
+60
View File
@@ -0,0 +1,60 @@
#nullable enable
using System;
using System.Threading.Tasks;
using BotFramework;
using BotFramework.Attributes;
using BotFramework.Enums;
using BotFramework.Utils;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Kruzya.TelegramBot.Core.Service;
using Microsoft.Extensions.Logging;
using Telegram.Bot;
using Telegram.Bot.Types.Enums;
namespace West.Entertainment.Handler
{
public class Common : BotEventHandler
{
private readonly UserService _userService;
private readonly ILogger _logger;
private readonly CoreContext _db;
public Common(CoreContext db, ILogger<Common> logger, UserService userService)
{
_db = db;
_logger = logger;
_userService = userService;
}
[Command(InChat.Public, "cooldowns", CommandParseMode.Both)]
public async Task HandleCooldowns()
{
if (!_userService.IsUserSuper(From))
{
_logger.LogDebug("{User} attempted to call /cooldowns in \"{Chat}\"", From, Chat.Title);
return;
}
var cds = new[] { "python", "javascript" };
var msg = new HtmlString().Bold("Кулдауны:").Br();
foreach (var cd in cds)
{
var opt = await _db.UserValues.FindOption(Chat.Id, $"{cd}NextUse");
var val = opt.GetValue<DateTime>();
if (val == default)
{
continue;
}
msg.Text($"{cd}: ")
.Code((val - DateTime.Now).Duration().ToString())
.Br();
}
await Bot.SendTextMessageAsync(Chat.Id, msg.ToString(), ParseMode.Html);
}
}
}
@@ -19,7 +19,7 @@ namespace West.Entertainment.Handler.Fun
private readonly ILogger _logger;
private static TimeSpan Cooldown => TimeSpan.FromMinutes(new Random().Next(10, 1337));
protected virtual TimeSpan Cooldown => TimeSpan.FromMinutes(new Random().Next(10, 1337));
protected Message? Message => RawUpdate.Message;
@@ -57,14 +57,14 @@ namespace West.Entertainment.Handler.Fun
if (!AnimationMatch.IsMatch(text.ToLower()) || text.StartsWith("/set") || !CanHandle()) return false;
var animationFileId = (await GetAnimationFileOption()).GetValue(string.Empty);
var lastUseOption = await GetLastUseOption();
var nextUseOption = await GetNextUseOption();
_logger.LogDebug("Animation: {Text}, ToLower: {ToLowerText}",
Message!.Text, Message!.Text!.ToLower());
if (animationFileId.IsNullOrEmpty()) return false;
var nextUseDt = lastUseOption.GetValue<DateTime>();
var nextUseDt = nextUseOption.GetValue<DateTime>();
if (nextUseDt > DateTime.Now)
{
_logger.LogInformation("{Name} is on cooldown for chat \"{ChatTitle}\". Will be available at {Time}",
@@ -76,8 +76,8 @@ namespace West.Entertainment.Handler.Fun
await Bot.SendAnimationAsync(Chat.Id, animationFileId,
replyToMessageId: GetMessageIdToReply() ?? Message?.MessageId);
lastUseOption.SetValue(DateTime.Now + Cooldown);
_db.MarkAsModified(lastUseOption);
nextUseOption.SetValue(DateTime.Now + Cooldown);
_db.MarkAsModified(nextUseOption);
return true;
}
@@ -87,7 +87,7 @@ namespace West.Entertainment.Handler.Fun
return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}File");
}
private async Task<BotUserValue> GetLastUseOption()
private async Task<BotUserValue> GetNextUseOption()
{
return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}NextUse");
}
@@ -1,5 +1,6 @@
#nullable enable
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using BotFramework.Attributes;
@@ -12,6 +13,8 @@ namespace West.Entertainment.Handler.Fun
{
public class Bayan : AbstractAnimationReply
{
protected override TimeSpan Cooldown => TimeSpan.Zero;
public Bayan(CoreContext db, ILogger<Bayan> logger) : base(db, logger) { }
protected override Regex AnimationMatch