2022-12-14 11:40:27 +02:00
|
|
|
using BotFramework.Attributes;
|
|
|
|
|
using BotFramework.Enums;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System;
|
2022-12-14 12:31:53 +02:00
|
|
|
using System.Linq;
|
2022-12-14 11:40:27 +02:00
|
|
|
using Kruzya.TelegramBot.Core;
|
|
|
|
|
using Kruzya.TelegramBot.Core.Data;
|
|
|
|
|
using Kruzya.TelegramBot.Core.Extensions;
|
|
|
|
|
using Kruzya.TelegramBot.Core.Service;
|
2022-12-14 12:31:53 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
2022-12-14 11:40:27 +02:00
|
|
|
|
2022-12-14 11:50:02 +02:00
|
|
|
namespace West.Entertainment.Handler;
|
2022-12-14 11:40:27 +02:00
|
|
|
|
|
|
|
|
public class Roll : CommonHandler
|
|
|
|
|
{
|
2022-12-14 12:31:53 +02:00
|
|
|
private readonly ILogger<Roll> _logger;
|
2022-12-14 11:40:27 +02:00
|
|
|
private readonly IReputation _reputation;
|
2022-12-14 12:31:53 +02:00
|
|
|
private readonly UserService _userService;
|
2022-12-14 11:40:27 +02:00
|
|
|
|
2022-12-16 16:35:45 +02:00
|
|
|
// [Command(InChat.Public, "roll", CommandParseMode.Both)]
|
2022-12-14 11:40:27 +02:00
|
|
|
public async Task HandleRoll()
|
|
|
|
|
{
|
|
|
|
|
var cdOption = await Db.UserValues.FindOrCreateOption(Chat.Id, From.Id, "rollNextUse");
|
|
|
|
|
var cd = cdOption.GetValue<DateTime>();
|
|
|
|
|
|
|
|
|
|
if (cd > DateTime.Now)
|
|
|
|
|
{
|
|
|
|
|
await Reply($"Следующее использование через {cd - DateTime.Now:hh\\:mm\\:ss}.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-14 11:50:02 +02:00
|
|
|
var newRep = new Random().Next(-50, 50);
|
2022-12-14 11:40:27 +02:00
|
|
|
|
|
|
|
|
await Reply($"Вы получили {newRep} кармы.");
|
|
|
|
|
await _reputation.IncrementReputationAsync(Chat, From, newRep);
|
|
|
|
|
|
2022-12-14 11:50:02 +02:00
|
|
|
cdOption.SetValue(DateTime.Now.AddDays(1));
|
2022-12-14 11:40:27 +02:00
|
|
|
Db.AddOrUpdate(cdOption);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 16:35:45 +02:00
|
|
|
// [Command(InChat.Public, "reset_roll", CommandParseMode.Both)]
|
2022-12-14 12:31:53 +02:00
|
|
|
public async Task HandleResetRoll()
|
|
|
|
|
{
|
|
|
|
|
if (!_userService.IsUserSuper(From))
|
|
|
|
|
{
|
2022-12-14 12:58:12 +02:00
|
|
|
_logger.LogWarning("{User} attempted to use super-user command /reset_roll in {Chat}",
|
|
|
|
|
From.ToLog(),
|
|
|
|
|
Chat.ToLog()
|
|
|
|
|
);
|
2022-12-14 12:31:53 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var cooldownList = Db.UserValues
|
|
|
|
|
.Where(q => q.BotUser.ChatId == Chat.Id && q.Name == "rollNextUse");
|
|
|
|
|
|
|
|
|
|
Db.UserValues.RemoveRange(cooldownList);
|
|
|
|
|
await Db.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
await Reply($"Кулдаун /roll сброшен для чата \"{Chat.Title}\".");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Roll(CoreContext db, IReputation reputation, UserService userService, ILogger<Roll> logger) : base(db)
|
2022-12-14 11:40:27 +02:00
|
|
|
{
|
|
|
|
|
_reputation = reputation;
|
2022-12-14 12:31:53 +02:00
|
|
|
_userService = userService;
|
|
|
|
|
_logger = logger;
|
2022-12-14 11:40:27 +02:00
|
|
|
}
|
|
|
|
|
}
|